Package com.google.auto.value.processor
Class BuilderMethodClassifier
- java.lang.Object
-
- com.google.auto.value.processor.BuilderMethodClassifier
-
class BuilderMethodClassifier extends java.lang.Object
Classifies methods inside builder types, based on their names and parameter and return types.
-
-
Field Summary
Fields Modifier and Type Field Description private javax.lang.model.element.TypeElement
autoValueClass
private java.util.Map<java.lang.String,BuilderSpec.PropertyGetter>
builderGetters
private javax.lang.model.element.TypeElement
builderType
private java.util.Set<javax.lang.model.element.ExecutableElement>
buildMethods
private EclipseHack
eclipseHack
private javax.lang.model.util.Elements
elementUtils
private ErrorReporter
errorReporter
private com.google.common.collect.ImmutableMap<java.lang.String,javax.lang.model.element.ExecutableElement>
getterNameToGetter
private com.google.common.collect.ImmutableBiMap<javax.lang.model.element.ExecutableElement,java.lang.String>
getterToPropertyName
private com.google.common.collect.Multimap<java.lang.String,javax.lang.model.element.ExecutableElement>
propertyNameToPrefixedSetters
private java.util.Map<java.lang.String,PropertyBuilderClassifier.PropertyBuilder>
propertyNameToPropertyBuilder
private com.google.common.collect.Multimap<java.lang.String,javax.lang.model.element.ExecutableElement>
propertyNameToUnprefixedSetters
private boolean
settersPrefixed
private static com.google.common.base.Equivalence<javax.lang.model.type.TypeMirror>
TYPE_EQUIVALENCE
private javax.lang.model.util.Types
typeUtils
-
Constructor Summary
Constructors Modifier Constructor Description private
BuilderMethodClassifier(ErrorReporter errorReporter, javax.annotation.processing.ProcessingEnvironment processingEnv, javax.lang.model.element.TypeElement autoValueClass, javax.lang.model.element.TypeElement builderType, com.google.common.collect.ImmutableBiMap<javax.lang.model.element.ExecutableElement,java.lang.String> getterToPropertyName)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) com.google.common.collect.ImmutableMap<java.lang.String,BuilderSpec.PropertyGetter>
builderGetters()
Returns the set of properties that have getters in the builder.(package private) javax.lang.model.type.TypeMirror
builderMethodReturnType(javax.lang.model.element.ExecutableElement builderMethod)
Returns the return type of the given method from the builder.(package private) java.util.Set<javax.lang.model.element.ExecutableElement>
buildMethods()
Returns the methods that were identified asbuild()
methods.private boolean
canMakeCopyUsing(com.google.common.collect.ImmutableList<javax.lang.model.element.ExecutableElement> copyOfMethods, javax.lang.model.element.ExecutableElement valueGetter, javax.lang.model.element.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 givencopyOf
methods.private boolean
canMakeCopyUsing(javax.lang.model.element.ExecutableElement copyOfMethod, javax.lang.model.type.TypeMirror targetType, javax.lang.model.type.TypeMirror parameterType)
Returns true ifcopyOfMethod
can be used to copy theparameterType
to thetargetType
.private void
checkForFailedJavaBean(javax.lang.model.element.ExecutableElement rejectedSetter)
private boolean
checkSetterParameter(javax.lang.model.element.ExecutableElement valueGetter, javax.lang.model.element.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 java.util.Optional<BuilderMethodClassifier>
classify(java.lang.Iterable<javax.lang.model.element.ExecutableElement> methods, ErrorReporter errorReporter, javax.annotation.processing.ProcessingEnvironment processingEnv, javax.lang.model.element.TypeElement autoValueClass, javax.lang.model.element.TypeElement builderType, com.google.common.collect.ImmutableBiMap<javax.lang.model.element.ExecutableElement,java.lang.String> getterToPropertyName, boolean autoValueHasToBuilder)
Classifies the given methods from a builder type and its ancestors.private boolean
classifyGetter(javax.lang.model.element.ExecutableElement builderGetter, javax.lang.model.element.ExecutableElement originalGetter)
private boolean
classifyMethod(javax.lang.model.element.ExecutableElement method)
Classifies a method and update the state of this object based on what is found.private boolean
classifyMethodNoArgs(javax.lang.model.element.ExecutableElement method)
Classifies a method given that it has no arguments.private boolean
classifyMethodOneArg(javax.lang.model.element.ExecutableElement method)
Classifies a method given that it has one argument.private boolean
classifyMethods(java.lang.Iterable<javax.lang.model.element.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<javax.lang.model.element.ExecutableElement>
copyOfMethods(javax.lang.model.type.TypeMirror targetType)
ReturnscopyOf
methods from the given type.private static java.lang.String
decapitalize(java.lang.String propertyName)
private static java.lang.String
prefixWithSet(java.lang.String propertyName)
(package private) java.util.Map<java.lang.String,PropertyBuilderClassifier.PropertyBuilder>
propertyNameToPropertyBuilder()
(package private) com.google.common.collect.ImmutableMultimap<java.lang.String,javax.lang.model.element.ExecutableElement>
propertyNameToSetters()
Returns a multimap from the name of a property to the methods that set it.private java.lang.String
typeParamsString()
-
-
-
Field Detail
-
TYPE_EQUIVALENCE
private static final com.google.common.base.Equivalence<javax.lang.model.type.TypeMirror> TYPE_EQUIVALENCE
-
errorReporter
private final ErrorReporter errorReporter
-
typeUtils
private final javax.lang.model.util.Types typeUtils
-
elementUtils
private final javax.lang.model.util.Elements elementUtils
-
autoValueClass
private final javax.lang.model.element.TypeElement autoValueClass
-
builderType
private final javax.lang.model.element.TypeElement builderType
-
getterToPropertyName
private final com.google.common.collect.ImmutableBiMap<javax.lang.model.element.ExecutableElement,java.lang.String> getterToPropertyName
-
getterNameToGetter
private final com.google.common.collect.ImmutableMap<java.lang.String,javax.lang.model.element.ExecutableElement> getterNameToGetter
-
buildMethods
private final java.util.Set<javax.lang.model.element.ExecutableElement> buildMethods
-
builderGetters
private final java.util.Map<java.lang.String,BuilderSpec.PropertyGetter> builderGetters
-
propertyNameToPropertyBuilder
private final java.util.Map<java.lang.String,PropertyBuilderClassifier.PropertyBuilder> propertyNameToPropertyBuilder
-
propertyNameToPrefixedSetters
private final com.google.common.collect.Multimap<java.lang.String,javax.lang.model.element.ExecutableElement> propertyNameToPrefixedSetters
-
propertyNameToUnprefixedSetters
private final com.google.common.collect.Multimap<java.lang.String,javax.lang.model.element.ExecutableElement> propertyNameToUnprefixedSetters
-
eclipseHack
private final EclipseHack eclipseHack
-
settersPrefixed
private boolean settersPrefixed
-
-
Constructor Detail
-
BuilderMethodClassifier
private BuilderMethodClassifier(ErrorReporter errorReporter, javax.annotation.processing.ProcessingEnvironment processingEnv, javax.lang.model.element.TypeElement autoValueClass, javax.lang.model.element.TypeElement builderType, com.google.common.collect.ImmutableBiMap<javax.lang.model.element.ExecutableElement,java.lang.String> getterToPropertyName)
-
-
Method Detail
-
classify
static java.util.Optional<BuilderMethodClassifier> classify(java.lang.Iterable<javax.lang.model.element.ExecutableElement> methods, ErrorReporter errorReporter, javax.annotation.processing.ProcessingEnvironment processingEnv, javax.lang.model.element.TypeElement autoValueClass, javax.lang.model.element.TypeElement builderType, com.google.common.collect.ImmutableBiMap<javax.lang.model.element.ExecutableElement,java.lang.String> getterToPropertyName, boolean autoValueHasToBuilder)
Classifies the given methods from a builder type and its ancestors.- Parameters:
methods
- the methods inbuilderType
and its ancestors.errorReporter
- where to report errors.processingEnv
- the ProcessingEnvironment for annotation processing.autoValueClass
- theAutoValue
class 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@AutoValue
class has atoBuilder()
method.- Returns:
- an
Optional
that contains the results of the classification if it was successful or nothing if it was not.
-
propertyNameToSetters
com.google.common.collect.ImmutableMultimap<java.lang.String,javax.lang.model.element.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@AutoValue
class calledfoo()
orgetFoo()
then the name of the property isfoo
and there will be an entry in the map where the key is"foo"
and the value is a method in the builder calledfoo
orsetFoo
.
-
propertyNameToPropertyBuilder
java.util.Map<java.lang.String,PropertyBuilderClassifier.PropertyBuilder> propertyNameToPropertyBuilder()
-
builderGetters
com.google.common.collect.ImmutableMap<java.lang.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@AutoValue
class 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
java.util.Set<javax.lang.model.element.ExecutableElement> buildMethods()
Returns the methods that were identified asbuild()
methods. These are methods that have no parameters and return the@AutoValue
type, conventionally calledbuild()
.
-
classifyMethods
private boolean classifyMethods(java.lang.Iterable<javax.lang.model.element.ExecutableElement> methods, boolean autoValueHasToBuilder)
Classifies the given methods and sets the state of this object based on what is found.
-
classifyMethod
private boolean classifyMethod(javax.lang.model.element.ExecutableElement method)
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
private boolean classifyMethodNoArgs(javax.lang.model.element.ExecutableElement method)
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@AutoValue
class; it can be a getter, with the same signature as one of the property getters in the@AutoValue
class; 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
private boolean classifyGetter(javax.lang.model.element.ExecutableElement builderGetter, javax.lang.model.element.ExecutableElement originalGetter)
-
classifyMethodOneArg
private boolean classifyMethodOneArg(javax.lang.model.element.ExecutableElement method)
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 theAutoValue
class has a property calledfoo
of typeT
.- Returns:
- true if the method was successfully classified, false if an error has been reported.
-
checkForFailedJavaBean
private void checkForFailedJavaBean(javax.lang.model.element.ExecutableElement rejectedSetter)
-
checkSetterParameter
private boolean checkSetterParameter(javax.lang.model.element.ExecutableElement valueGetter, javax.lang.model.element.ExecutableElement setter)
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.copyOf
orOptional.of
.- Returns:
- true if the types correspond, false if an error has been reported.
-
canMakeCopyUsing
private boolean canMakeCopyUsing(com.google.common.collect.ImmutableList<javax.lang.model.element.ExecutableElement> copyOfMethods, javax.lang.model.element.ExecutableElement valueGetter, javax.lang.model.element.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 givencopyOf
methods.- Returns:
- true if the copy can be made, false if an error has been reported.
-
canMakeCopyUsing
private boolean canMakeCopyUsing(javax.lang.model.element.ExecutableElement copyOfMethod, javax.lang.model.type.TypeMirror targetType, javax.lang.model.type.TypeMirror parameterType)
Returns true ifcopyOfMethod
can be used to copy theparameterType
to thetargetType
.
-
copyOfMethods
private com.google.common.collect.ImmutableList<javax.lang.model.element.ExecutableElement> copyOfMethods(javax.lang.model.type.TypeMirror targetType)
ReturnscopyOf
methods from the given type. These are static methods calledcopyOf
with 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
javax.lang.model.type.TypeMirror builderMethodReturnType(javax.lang.model.element.ExecutableElement builderMethod)
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> { ... }
ChildBuilder
then the return type ofsetFoo
is alsoChildBuilder
, and notB
as itsgetReturnType()
method would claim.If the caller is in a version of Eclipse with this bug then the
asMemberOf
call will fail if the method is inherited from an interface. We work around that for methods in the@AutoValue
class 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 changeParentBuilder
from an interface to an abstract class to make it work, but you'll often need to do that anyway.
-
prefixWithSet
private static java.lang.String prefixWithSet(java.lang.String propertyName)
-
decapitalize
private static java.lang.String decapitalize(java.lang.String propertyName)
-
typeParamsString
private java.lang.String typeParamsString()
-
-