Package com.google.auto.value.processor
Class BuilderMethodClassifier
java.lang.Object
com.google.auto.value.processor.BuilderMethodClassifier
Classifies methods inside builder types, based on their names and parameter and return types.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final TypeElementprivate final Map<String, BuilderSpec.PropertyGetter> private final TypeElementprivate final Set<ExecutableElement> private final EclipseHackprivate final Elementsprivate final ErrorReporterprivate final com.google.common.collect.ImmutableMap<String, ExecutableElement> private final com.google.common.collect.ImmutableBiMap<ExecutableElement, String> private final com.google.common.collect.Multimap<String, ExecutableElement> private final Map<String, PropertyBuilderClassifier.PropertyBuilder> private final com.google.common.collect.Multimap<String, ExecutableElement> private booleanprivate static final com.google.common.base.Equivalence<TypeMirror> private final Types -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateBuilderMethodClassifier(ErrorReporter errorReporter, ProcessingEnvironment processingEnv, TypeElement autoValueClass, TypeElement builderType, com.google.common.collect.ImmutableBiMap<ExecutableElement, String> getterToPropertyName) -
Method Summary
Modifier and TypeMethodDescription(package private) com.google.common.collect.ImmutableMap<String, BuilderSpec.PropertyGetter> Returns the set of properties that have getters in the builder.(package private) TypeMirrorbuilderMethodReturnType(ExecutableElement builderMethod) Returns the return type of the given method from the builder.(package private) Set<ExecutableElement> Returns the methods that were identified asbuild()methods.private booleancanMakeCopyUsing(com.google.common.collect.ImmutableList<ExecutableElement> copyOfMethods, ExecutableElement valueGetter, ExecutableElement setter) Checks that the given setter method has a parameter type that can be copied to the return type of the given getter using one of the givencopyOfmethods.private booleancanMakeCopyUsing(ExecutableElement copyOfMethod, TypeMirror targetType, TypeMirror parameterType) Returns true ifcopyOfMethodcan be used to copy theparameterTypeto thetargetType.private voidcheckForFailedJavaBean(ExecutableElement rejectedSetter) private booleancheckSetterParameter(ExecutableElement valueGetter, ExecutableElement setter) Checks that the given setter method has a parameter type that is compatible with the return type of the given getter.(package private) static Optional<BuilderMethodClassifier> classify(Iterable<ExecutableElement> methods, ErrorReporter errorReporter, ProcessingEnvironment processingEnv, TypeElement autoValueClass, TypeElement builderType, com.google.common.collect.ImmutableBiMap<ExecutableElement, String> getterToPropertyName, boolean autoValueHasToBuilder) Classifies the given methods from a builder type and its ancestors.private booleanclassifyGetter(ExecutableElement builderGetter, ExecutableElement originalGetter) private booleanclassifyMethod(ExecutableElement method) Classifies a method and update the state of this object based on what is found.private booleanClassifies a method given that it has no arguments.private booleanClassifies a method given that it has one argument.private booleanclassifyMethods(Iterable<ExecutableElement> methods, boolean autoValueHasToBuilder) Classifies the given methods and sets the state of this object based on what is found.private com.google.common.collect.ImmutableList<ExecutableElement> copyOfMethods(TypeMirror targetType) ReturnscopyOfmethods from the given type.private static Stringdecapitalize(String propertyName) private static StringprefixWithSet(String propertyName) (package private) Map<String, PropertyBuilderClassifier.PropertyBuilder> (package private) com.google.common.collect.ImmutableMultimap<String, ExecutableElement> Returns a multimap from the name of a property to the methods that set it.private String
-
Field Details
-
TYPE_EQUIVALENCE
-
errorReporter
-
typeUtils
-
elementUtils
-
autoValueClass
-
builderType
-
getterToPropertyName
private final com.google.common.collect.ImmutableBiMap<ExecutableElement,String> getterToPropertyName -
getterNameToGetter
-
buildMethods
-
builderGetters
-
propertyNameToPropertyBuilder
-
propertyNameToPrefixedSetters
private final com.google.common.collect.Multimap<String,ExecutableElement> propertyNameToPrefixedSetters -
propertyNameToUnprefixedSetters
private final com.google.common.collect.Multimap<String,ExecutableElement> propertyNameToUnprefixedSetters -
eclipseHack
-
settersPrefixed
private boolean settersPrefixed
-
-
Constructor Details
-
BuilderMethodClassifier
private BuilderMethodClassifier(ErrorReporter errorReporter, ProcessingEnvironment processingEnv, TypeElement autoValueClass, TypeElement builderType, com.google.common.collect.ImmutableBiMap<ExecutableElement, String> getterToPropertyName)
-
-
Method Details
-
classify
static Optional<BuilderMethodClassifier> classify(Iterable<ExecutableElement> methods, ErrorReporter errorReporter, ProcessingEnvironment processingEnv, TypeElement autoValueClass, TypeElement builderType, com.google.common.collect.ImmutableBiMap<ExecutableElement, String> getterToPropertyName, boolean autoValueHasToBuilder) Classifies the given methods from a builder type and its ancestors.- Parameters:
methods- the methods inbuilderTypeand its ancestors.errorReporter- where to report errors.processingEnv- the ProcessingEnvironment for annotation processing.autoValueClass- theAutoValueclass containing the builder.builderType- the builder class or interface withinautoValueClass.getterToPropertyName- a map from getter methods to the properties they get.autoValueHasToBuilder- true if the containing@AutoValueclass has atoBuilder()method.- Returns:
- an
Optionalthat contains the results of the classification if it was successful or nothing if it was not.
-
propertyNameToSetters
com.google.common.collect.ImmutableMultimap<String,ExecutableElement> propertyNameToSetters()Returns a multimap from the name of a property to the methods that set it. If the property is defined by an abstract method in the@AutoValueclass calledfoo()orgetFoo()then the name of the property isfooand there will be an entry in the map where the key is"foo"and the value is a method in the builder calledfooorsetFoo. -
propertyNameToPropertyBuilder
Map<String,PropertyBuilderClassifier.PropertyBuilder> propertyNameToPropertyBuilder() -
builderGetters
com.google.common.collect.ImmutableMap<String,BuilderSpec.PropertyGetter> builderGetters()Returns the set of properties that have getters in the builder. If a property is defined by an abstract method in the@AutoValueclass calledfoo()orgetFoo()then the name of the property isfoo, If the builder also has a method of the same name (foo()orgetFoo()) then the set returned here will containfoo. -
buildMethods
Set<ExecutableElement> buildMethods()Returns the methods that were identified asbuild()methods. These are methods that have no parameters and return the@AutoValuetype, conventionally calledbuild(). -
classifyMethods
Classifies the given methods and sets the state of this object based on what is found. -
classifyMethod
Classifies a method and update the state of this object based on what is found.- Returns:
- true if the method was successfully classified, false if an error has been reported.
-
classifyMethodNoArgs
Classifies a method given that it has no arguments. Currently a method with no arguments can be abuild()method, meaning that its return type must be the@AutoValueclass; it can be a getter, with the same signature as one of the property getters in the@AutoValueclass; or it can be a property builder, likeImmutableList.Builder<String> foosBuilder()for the property defined byImmutableList<String> foos()orgetFoos().- Returns:
- true if the method was successfully classified, false if an error has been reported.
-
classifyGetter
-
classifyMethodOneArg
Classifies a method given that it has one argument. Currently, a method with one argument can only be a setter, meaning that it must look likefoo(T)orsetFoo(T), where theAutoValueclass has a property calledfooof typeT.- Returns:
- true if the method was successfully classified, false if an error has been reported.
-
checkForFailedJavaBean
-
checkSetterParameter
Checks that the given setter method has a parameter type that is compatible with the return type of the given getter. Compatible means either that it is the same, or that it is a type that can be copied using a method likeImmutableList.copyOforOptional.of.- Returns:
- true if the types correspond, false if an error has been reported.
-
canMakeCopyUsing
private boolean canMakeCopyUsing(com.google.common.collect.ImmutableList<ExecutableElement> copyOfMethods, ExecutableElement valueGetter, ExecutableElement setter) Checks that the given setter method has a parameter type that can be copied to the return type of the given getter using one of the givencopyOfmethods.- Returns:
- true if the copy can be made, false if an error has been reported.
-
canMakeCopyUsing
private boolean canMakeCopyUsing(ExecutableElement copyOfMethod, TypeMirror targetType, TypeMirror parameterType) Returns true ifcopyOfMethodcan be used to copy theparameterTypeto thetargetType. -
copyOfMethods
private com.google.common.collect.ImmutableList<ExecutableElement> copyOfMethods(TypeMirror targetType) ReturnscopyOfmethods from the given type. These are static methods calledcopyOfwith a single parameter. All of Guava's concrete immutable collection types have at least one such method, but we will also accept other classes with an appropriate method, such asEnumSet. -
builderMethodReturnType
Returns the return type of the given method from the builder. This should be the final type of the method when any bound type variables are substituted. Consider this example:
If the builder isabstract static class ParentBuilder<B extends ParentBuilder> { B setFoo(String s); } abstract static class ChildBuilder extends ParentBuilder<ChildBuilder> { ... }ChildBuilderthen the return type ofsetFoois alsoChildBuilder, and notBas itsgetReturnType()method would claim.If the caller is in a version of Eclipse with this bug then the
asMemberOfcall will fail if the method is inherited from an interface. We work around that for methods in the@AutoValueclass usingEclipseHack.methodReturnTypes(java.util.Set<javax.lang.model.element.ExecutableElement>, javax.lang.model.type.DeclaredType)but we don't try to do so here because it should be much less likely. You might need to changeParentBuilderfrom an interface to an abstract class to make it work, but you'll often need to do that anyway. -
prefixWithSet
-
decapitalize
-
typeParamsString
-