protected final void extractFromConstructors() { if (builderType == null) { // struct class must have a valid constructor addConstructors(structType); } else { // builder class must have a valid constructor addConstructors(builderType); // builder class must have a build method annotated with @ThriftConstructor addBuilderMethods(); // verify struct class does not have @ThriftConstructors for (Constructor<?> constructor : getStructClass().getConstructors()) { if (constructor.isAnnotationPresent(ThriftConstructor.class)) { metadataErrors.addWarning("Thrift class '%s' has a builder class, but constructor '%s' annotated with @ThriftConstructor", getStructClass().getName(), constructor); } } } }
protected final void extractFromFields() { if (builderType == null) { // struct fields are readable and writable addFields(getStructClass(), true, true); } else { // builder fields are writable addFields(getBuilderClass(), false, true); // struct fields are readable addFields(getStructClass(), true, false); } }
protected final void extractFromMethods() { if (builderType != null) { // builder methods are writable addMethods(builderType, false, true); // struct methods are readable addMethods(structType, true, false); } else { // struct methods are readable and writable addMethods(structType, true, true); } }
protected AbstractThriftMetadataBuilder(ThriftCatalog catalog, Type structType) { this.catalog = checkNotNull(catalog, "catalog is null"); this.structType = checkNotNull(structType, "structType is null"); this.metadataErrors = new MetadataErrors(catalog.getMonitor()); // assign the struct name from the annotation or from the Java class structName = extractName(); // get the builder type from the annotation or from the Java class builderType = extractBuilderType(); // grab any documentation from the annotation or saved JavaDocs documentation = ThriftCatalog.getThriftDocumentation(getStructClass()); // extract all of the annotated constructor and report an error if // there is more than one or none // also extract thrift fields from the annotated parameters and verify extractFromConstructors(); // extract thrift fields from the annotated fields and verify extractFromFields(); // extract thrift fields from the annotated methods (and parameters) and verify extractFromMethods(); }
protected final void addMethods(Type type, boolean allowReaders, boolean allowWriters) { Class<?> clazz = TypeToken.of(type).getRawType(); for (Method fieldMethod : findAnnotatedMethods(clazz, ThriftField.class)) { addMethod(type, fieldMethod, allowReaders, allowWriters); } // find invalid methods not skipped by findAnnotatedMethods() for (Method method : getAllDeclaredMethods(clazz)) { if (method.isAnnotationPresent(ThriftField.class) || hasThriftFieldAnnotation(method)) { if (!Modifier.isPublic(method.getModifiers())) { metadataErrors.addError("@ThriftField method '%s' is not public", method.toGenericString()); } if (Modifier.isStatic(method.getModifiers())) { metadataErrors.addError("@ThriftField method '%s' is static", method.toGenericString()); } } } }
private Type extractBuilderType() { Class<?> builderClass = extractBuilderClass(); if (builderClass == null) { return null; } if (builderClass.getTypeParameters().length == 0) { return builderClass; } if (!(structType instanceof ParameterizedType)) { metadataErrors.addError("Builder class '%s' may only be generic if the type it builds ('%s') is also generic", builderClass.getName(), getStructClass().getName()); return builderClass; } if (builderClass.getTypeParameters().length != getStructClass().getTypeParameters().length) { metadataErrors.addError("Generic builder class '%s' must have the same number of type parameters as the type it builds ('%s')", builderClass.getName(), getStructClass().getName()); return builderClass; } ParameterizedType parameterizedStructType = (ParameterizedType) structType; return new MoreTypes.ParameterizedTypeImpl(builderClass.getEnclosingClass(), builderClass, parameterizedStructType.getActualTypeArguments()); }
protected final ThriftMethodInjection buildBuilderConstructorInjections() { ThriftMethodInjection builderMethodInjection = null; if (builderType != null) { MethodInjection builderMethod = builderMethodInjections.get(0); builderMethodInjection = new ThriftMethodInjection(builderMethod.getMethod(), buildParameterInjections(builderMethod.getParameters())); } return builderMethodInjection; }
@Override public ThriftFieldMetadata apply(Collection<FieldMetadata> input) { checkArgument(!input.isEmpty(), "input is empty"); return buildField(input); } });
protected final void addFields(Class<?> clazz, boolean allowReaders, boolean allowWriters) { for (Field fieldField : ReflectionHelper.findAnnotatedFields(clazz, ThriftField.class)) { addField(fieldField, allowReaders, allowWriters); } // find invalid fields not skipped by findAnnotatedFields() for (Field field : getAllDeclaredFields(clazz)) { if (field.isAnnotationPresent(ThriftField.class)) { if (!Modifier.isPublic(field.getModifiers())) { metadataErrors.addError("@ThriftField field '%s' is not public", field.toGenericString()); } if (Modifier.isStatic(field.getModifiers())) { metadataErrors.addError("@ThriftField field '%s' is static", field.toGenericString()); } } } }
protected AbstractThriftMetadataBuilder(ThriftCatalog catalog, Type structType) { this.catalog = checkNotNull(catalog, "catalog is null"); this.structType = checkNotNull(structType, "structType is null"); this.metadataErrors = new MetadataErrors(catalog.getMonitor()); // assign the struct name from the annotation or from the Java class structName = extractName(); // get the builder type from the annotation or from the Java class builderType = extractBuilderType(); // grab any documentation from the annotation or saved JavaDocs documentation = ThriftCatalog.getThriftDocumentation(getStructClass()); // extract all of the annotated constructor and report an error if // there is more than one or none // also extract thrift fields from the annotated parameters and verify extractFromConstructors(); // extract thrift fields from the annotated fields and verify extractFromFields(); // extract thrift fields from the annotated methods (and parameters) and verify extractFromMethods(); }
protected final void addMethods(Type type, boolean allowReaders, boolean allowWriters) { Class<?> clazz = TypeToken.of(type).getRawType(); for (Method fieldMethod : findAnnotatedMethods(clazz, ThriftField.class)) { addMethod(type, fieldMethod, allowReaders, allowWriters); } // find invalid methods not skipped by findAnnotatedMethods() for (Method method : getAllDeclaredMethods(clazz)) { if (method.isAnnotationPresent(ThriftField.class) || hasThriftFieldAnnotation(method)) { if (!Modifier.isPublic(method.getModifiers())) { metadataErrors.addError("@ThriftField method '%s' is not public", method.toGenericString()); } if (Modifier.isStatic(method.getModifiers())) { metadataErrors.addError("@ThriftField method '%s' is static", method.toGenericString()); } } } }
private Type extractBuilderType() { Class<?> builderClass = extractBuilderClass(); if (builderClass == null) { return null; } if (builderClass.getTypeParameters().length == 0) { return builderClass; } if (!(structType instanceof ParameterizedType)) { metadataErrors.addError("Builder class '%s' may only be generic if the type it builds ('%s') is also generic", builderClass.getName(), getStructClass().getName()); return builderClass; } if (builderClass.getTypeParameters().length != getStructClass().getTypeParameters().length) { metadataErrors.addError("Generic builder class '%s' must have the same number of type parameters as the type it builds ('%s')", builderClass.getName(), getStructClass().getName()); return builderClass; } ParameterizedType parameterizedStructType = (ParameterizedType) structType; return new MoreTypes.ParameterizedTypeImpl(builderClass.getEnclosingClass(), builderClass, parameterizedStructType.getActualTypeArguments()); }
protected final ThriftMethodInjection buildBuilderConstructorInjections() { ThriftMethodInjection builderMethodInjection = null; if (builderType != null) { MethodInjection builderMethod = builderMethodInjections.get(0); builderMethodInjection = new ThriftMethodInjection(builderMethod.getMethod(), buildParameterInjections(builderMethod.getParameters())); } return builderMethodInjection; }
@Override public ThriftFieldMetadata apply(Collection<FieldMetadata> input) { checkArgument(!input.isEmpty(), "input is empty"); return buildField(input); } });
protected final void addFields(Class<?> clazz, boolean allowReaders, boolean allowWriters) { for (Field fieldField : ReflectionHelper.findAnnotatedFields(clazz, ThriftField.class)) { addField(fieldField, allowReaders, allowWriters); } // find invalid fields not skipped by findAnnotatedFields() for (Field field : getAllDeclaredFields(clazz)) { if (field.isAnnotationPresent(ThriftField.class)) { if (!Modifier.isPublic(field.getModifiers())) { metadataErrors.addError("@ThriftField field '%s' is not public", field.toGenericString()); } if (Modifier.isStatic(field.getModifiers())) { metadataErrors.addError("@ThriftField field '%s' is static", field.toGenericString()); } } } }
protected final void extractFromFields() { if (builderType == null) { // struct fields are readable and writable addFields(getStructClass(), true, true); } else { // builder fields are writable addFields(getBuilderClass(), false, true); // struct fields are readable addFields(getStructClass(), true, false); } }
protected final void extractFromConstructors() { if (builderType == null) { // struct class must have a valid constructor addConstructors(structType); } else { // builder class must have a valid constructor addConstructors(builderType); // builder class must have a build method annotated with @ThriftConstructor addBuilderMethods(); // verify struct class does not have @ThriftConstructors for (Constructor<?> constructor : getStructClass().getConstructors()) { if (constructor.isAnnotationPresent(ThriftConstructor.class)) { metadataErrors.addWarning("Thrift class '%s' has a builder class, but constructor '%s' annotated with @ThriftConstructor", getStructClass().getName(), constructor); } } } }
@Override public ThriftMethodInjection apply(MethodInjection injection) { return new ThriftMethodInjection(injection.getMethod(), buildParameterInjections(injection.getParameters())); } });
protected final void extractFromMethods() { if (builderType != null) { // builder methods are writable addMethods(builderType, false, true); // struct methods are readable addMethods(structType, true, false); } else { // struct methods are readable and writable addMethods(structType, true, true); } }
@Override public ThriftMethodInjection apply(MethodInjection injection) { return new ThriftMethodInjection(injection.getMethod(), buildParameterInjections(injection.getParameters())); } });