Package com.google.auto.value.processor
Class AutoValueOrOneOfProcessor
java.lang.Object
javax.annotation.processing.AbstractProcessor
com.google.auto.value.processor.AutoValueOrOneOfProcessor
- All Implemented Interfaces:
Processor
- Direct Known Subclasses:
AutoOneOfProcessor
,AutoValueProcessor
Shared code between AutoValueProcessor and AutoOneOfProcessor.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static class
(package private) static enum
static class
A property of an@AutoValue
or@AutoOneOf
class, defined by one of its abstract methods. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final String
private TypeElement
The annotation we are processing,AutoValue
orAutoOneOf
.Qualified names of@AutoValue
orAutoOneOf
classes that we attempted to process but had to abandon because we needed other types that they referenced and those other types were missing.private ErrorReporter
private String
The simple name ofannotationType
.Fields inherited from class javax.annotation.processing.AbstractProcessor
processingEnv
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) static com.google.common.collect.ImmutableSet<ExecutableElement>
abstractMethodsIn
(com.google.common.collect.ImmutableSet<ExecutableElement> methods) Returns the subset of all abstract methods in the given set of methods.(package private) com.google.common.collect.ImmutableList<AnnotationMirror>
annotationsToCopy
(Element autoValueType, Element typeOrMethod, Set<String> excludedAnnotations) Implements the semantics ofAutoValue.CopyAnnotations
; see its javadoc.(package private) static com.google.common.collect.ImmutableList<String>
annotationStrings
(List<? extends AnnotationMirror> annotations) Returns the spelling to be used in the generated code for the given list of annotations.private boolean
annotationVisibleFrom
(AnnotationMirror annotation, Element from) (package private) final void
Checks that, if the given@AutoValue
or@AutoOneOf
class is nested, it is static and not private.(package private) final void
checkReturnType
(TypeElement autoValueClass, ExecutableElement getter) Checks that the return type of the given property method is allowed.private com.google.common.collect.ImmutableList<String>
private com.google.common.collect.ImmutableList<String>
copyAnnotations
(Element autoValueType, Element typeOrMethod, Set<String> excludedAnnotations) Implements the semantics ofAutoValue.CopyAnnotations
; see its javadoc.(package private) final void
defineSharedVarsForType
(TypeElement type, com.google.common.collect.ImmutableSet<ExecutableElement> methods, AutoValueOrOneOfTemplateVars vars) Defines the template variables that are shared by AutoValue and AutoOneOf.private static Map<AutoValueOrOneOfProcessor.ObjectMethod,
ExecutableElement> Given a list of all methods defined in or inherited by a class, returns a map indicating which of equals, hashCode, and toString should be generated.private static String
disambiguate
(String name, Collection<String> existingNames) (package private) final Elements
(package private) static String
equalsParameterType
(Map<AutoValueOrOneOfProcessor.ObjectMethod, ExecutableElement> methodsToGenerate) Returns the encoded parameter type of theequals(Object)
method that is to be generated, or an empty string if the method is not being generated.(package private) final ErrorReporter
(package private) static void
fixReservedIdentifiers
(Map<?, String> methodToIdentifier) Modifies the values of the given map to avoid reserved words.(package private) static String
generatedClassName
(TypeElement type, String prefix) Returns the name of the generated@AutoValue
or@AutoOneOf
class, for exampleAutoOneOf_TaskResult
or$$AutoValue_SimpleMethod
.private static String
getAnnotationFqName
(AnnotationMirror annotation) Returns the fully-qualified name of an annotation-mirror, e.g.(package private) static Optional<AnnotationMirror>
getAnnotationMirror
(Element element, String annotationName) getAnnotationsMarkedWithInherited
(Element element) getExcludedClasses
(Element element) Returns the contents of theAutoValue.CopyAnnotations.exclude
element, as a set of strings that are fully-qualified class names.(package private) final String
Returns a string like"1234L"
iftype instanceof Serializable
and definesserialVersionUID = 1234L
; otherwise""
.final SourceVersion
private static boolean
gettersAllPrefixed
(Set<ExecutableElement> methods) (package private) static boolean
hasAnnotationMirror
(Element element, String annotationName) void
init
(ProcessingEnvironment processingEnv) private boolean
isInAutoValuePackage
(String className) True if the given class name is in the com.google.auto.value package or a subpackage.private static boolean
isJavaLangObject
(TypeElement type) private static String
nameWithoutPrefix
(String name) Returns the name of the property defined by the given getter.nullableAnnotationForMethod
(ExecutableElement propertyMethod) Returns the appropriate@Nullable
annotation to put on the implementation of the given property method, and indicates whether the property is in fact nullable.(package private) static AutoValueOrOneOfProcessor.ObjectMethod
Determines which of the three public non-final methods fromjava.lang.Object
, if any, is overridden by the given method.(package private) static com.google.common.collect.ImmutableSet<ExecutableElement>
prefixedGettersIn
(Iterable<ExecutableElement> methods) Returns the subset of the given zero-arg methods whose names begin withget
.final boolean
process
(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) (package private) abstract void
processType
(TypeElement type) Analyzes a single@AutoValue
or@AutoOneOf
class, and outputs the corresponding implementation class or classes.(package private) final com.google.common.collect.ImmutableListMultimap<ExecutableElement,
AnnotationMirror> propertyMethodAnnotationMap
(TypeElement type, com.google.common.collect.ImmutableSet<ExecutableElement> propertyMethods) private com.google.common.collect.ImmutableList<AnnotationMirror>
propertyMethodAnnotations
(TypeElement type, ExecutableElement method) (package private) static com.google.common.collect.ImmutableSet<ExecutableElement>
propertyMethodsIn
(Set<ExecutableElement> abstractMethods) Returns the subset of property methods in the given set of abstract methods.(package private) final com.google.common.collect.ImmutableBiMap<String,
ExecutableElement> propertyNameToMethodMap
(Set<ExecutableElement> propertyMethods) Returns a bi-map between property names and the corresponding abstract property methods.(package private) final com.google.common.collect.ImmutableSet<AutoValueOrOneOfProcessor.Property>
propertySet
(TypeElement type, com.google.common.collect.ImmutableSet<ExecutableElement> propertyMethods, com.google.common.collect.ImmutableListMultimap<ExecutableElement, AnnotationMirror> annotatedPropertyMethods) Returns the ordered set ofAutoValueOrOneOfProcessor.Property
definitions for the given@AutoValue
orAutoOneOf
type.(package private) final Types
private void
warnAboutPrimitiveArrays
(TypeElement autoValueClass, ExecutableElement getter) private static String
Returns the@AutoValue
or@AutoOneOf
type parameters, with a ? for every type.(package private) final void
writeSourceFile
(String className, String text, TypeElement originatingType) Methods inherited from class javax.annotation.processing.AbstractProcessor
getCompletions, getSupportedAnnotationTypes, getSupportedOptions, isInitialized
-
Field Details
-
annotationClassName
-
deferredTypeNames
Qualified names of@AutoValue
orAutoOneOf
classes that we attempted to process but had to abandon because we needed other types that they referenced and those other types were missing. -
annotationType
The annotation we are processing,AutoValue
orAutoOneOf
. -
simpleAnnotationName
The simple name ofannotationType
. -
errorReporter
-
-
Constructor Details
-
AutoValueOrOneOfProcessor
AutoValueOrOneOfProcessor(String annotationClassName)
-
-
Method Details
-
init
- Specified by:
init
in interfaceProcessor
- Overrides:
init
in classAbstractProcessor
-
errorReporter
-
typeUtils
-
elementUtils
-
getSupportedSourceVersion
- Specified by:
getSupportedSourceVersion
in interfaceProcessor
- Overrides:
getSupportedSourceVersion
in classAbstractProcessor
-
process
- Specified by:
process
in interfaceProcessor
- Specified by:
process
in classAbstractProcessor
-
processType
Analyzes a single@AutoValue
or@AutoOneOf
class, and outputs the corresponding implementation class or classes.- Parameters:
type
- the class with the@AutoValue
or@AutoOneOf
annotation.
-
nullableAnnotationForMethod
Returns the appropriate@Nullable
annotation to put on the implementation of the given property method, and indicates whether the property is in fact nullable. The annotation in question is on the method, not its return type. If instead the return type is@Nullable
, this method returnsOptional.of("")
, to indicate that the property is nullable but the method isn't. The@Nullable
annotation will instead appear when the return type of the method is spelled out in the implementation. -
propertySet
final com.google.common.collect.ImmutableSet<AutoValueOrOneOfProcessor.Property> propertySet(TypeElement type, com.google.common.collect.ImmutableSet<ExecutableElement> propertyMethods, com.google.common.collect.ImmutableListMultimap<ExecutableElement, AnnotationMirror> annotatedPropertyMethods) Returns the ordered set ofAutoValueOrOneOfProcessor.Property
definitions for the given@AutoValue
orAutoOneOf
type.- Parameters:
annotatedPropertyMethods
- a map from property methods to the method annotations that should go on the implementation of those methods. These annotations are method annotations specifically. Type annotations do not appear because they are considered part of the return type and will appear when that is spelled out. Annotations that are excluded byAutoValue.CopyAnnotations
also do not appear here.
-
annotationStrings
static com.google.common.collect.ImmutableList<String> annotationStrings(List<? extends AnnotationMirror> annotations) Returns the spelling to be used in the generated code for the given list of annotations. -
generatedClassName
Returns the name of the generated@AutoValue
or@AutoOneOf
class, for exampleAutoOneOf_TaskResult
or$$AutoValue_SimpleMethod
.- Parameters:
type
- the name of the type bearing the@AutoValue
or@AutoOneOf
annotation.prefix
- the prefix to use in the generated class. This may start with one or more dollar signs, for an@AutoValue
implementation where there are AutoValue extensions.
-
isJavaLangObject
-
objectMethodToOverride
Determines which of the three public non-final methods fromjava.lang.Object
, if any, is overridden by the given method. -
propertyNameToMethodMap
final com.google.common.collect.ImmutableBiMap<String,ExecutableElement> propertyNameToMethodMap(Set<ExecutableElement> propertyMethods) Returns a bi-map between property names and the corresponding abstract property methods. -
gettersAllPrefixed
-
prefixedGettersIn
static com.google.common.collect.ImmutableSet<ExecutableElement> prefixedGettersIn(Iterable<ExecutableElement> methods) Returns the subset of the given zero-arg methods whose names begin withget
. Also includesisFoo
methods if they returnboolean
. This corresponds to JavaBeans conventions. -
nameWithoutPrefix
Returns the name of the property defined by the given getter. A getter calledgetFoo()
orisFoo()
defines a property calledfoo
. For consistency with JavaBeans, a getter calledgetHTMLPage()
defines a property calledHTMLPage
. The rule is: the name of the property is the part afterget
oris
, with the first letter lowercased unless the first two letters are uppercase. This works well for theHTMLPage
example, but in these more enlightened times we useHtmlPage
anyway, so the special behaviour is not useful, and of course it behaves poorly with examples likeOAuth
. -
checkModifiersIfNested
Checks that, if the given@AutoValue
or@AutoOneOf
class is nested, it is static and not private. This check is not necessary for correctness, since the generated code would not compile if the check fails, but it produces better error messages for the user. -
fixReservedIdentifiers
Modifies the values of the given map to avoid reserved words. If we have a getter calledgetPackage()
then we can't use the identifierpackage
to represent its value since that's a reserved word. -
disambiguate
-
determineObjectMethodsToGenerate
private static Map<AutoValueOrOneOfProcessor.ObjectMethod,ExecutableElement> determineObjectMethodsToGenerate(Set<ExecutableElement> methods) Given a list of all methods defined in or inherited by a class, returns a map indicating which of equals, hashCode, and toString should be generated. Each value in the map is the method that will be overridden by the generated method, which might be a method inObject
or an abstract method in the@AutoValue
class or an ancestor. -
equalsParameterType
static String equalsParameterType(Map<AutoValueOrOneOfProcessor.ObjectMethod, ExecutableElement> methodsToGenerate) Returns the encoded parameter type of theequals(Object)
method that is to be generated, or an empty string if the method is not being generated. The parameter type includes any type annotations, for example@Nullable
. -
abstractMethodsIn
static com.google.common.collect.ImmutableSet<ExecutableElement> abstractMethodsIn(com.google.common.collect.ImmutableSet<ExecutableElement> methods) Returns the subset of all abstract methods in the given set of methods. A given method signature is only mentioned once, even if it is inherited on more than one path. -
propertyMethodsIn
static com.google.common.collect.ImmutableSet<ExecutableElement> propertyMethodsIn(Set<ExecutableElement> abstractMethods) Returns the subset of property methods in the given set of abstract methods. A property method has no arguments, is not void, and is nothashCode()
ortoString()
. -
checkReturnType
Checks that the return type of the given property method is allowed. Currently, this means that it cannot be an array, unless it is a primitive array. -
warnAboutPrimitiveArrays
-
getSerialVersionUID
Returns a string like"1234L"
iftype instanceof Serializable
and definesserialVersionUID = 1234L
; otherwise""
. -
annotationsToCopy
com.google.common.collect.ImmutableList<AnnotationMirror> annotationsToCopy(Element autoValueType, Element typeOrMethod, Set<String> excludedAnnotations) Implements the semantics ofAutoValue.CopyAnnotations
; see its javadoc. -
isInAutoValuePackage
True if the given class name is in the com.google.auto.value package or a subpackage. False if the class name containsTest
, since many AutoValue tests under com.google.auto.value define their own annotations. -
copiedClassAnnotations
-
copyAnnotations
private com.google.common.collect.ImmutableList<String> copyAnnotations(Element autoValueType, Element typeOrMethod, Set<String> excludedAnnotations) Implements the semantics ofAutoValue.CopyAnnotations
; see its javadoc. -
getExcludedClasses
Returns the contents of theAutoValue.CopyAnnotations.exclude
element, as a set of strings that are fully-qualified class names. -
getAnnotationsMarkedWithInherited
-
getAnnotationFqName
Returns the fully-qualified name of an annotation-mirror, e.g. "com.google.auto.value.AutoValue". -
propertyMethodAnnotationMap
final com.google.common.collect.ImmutableListMultimap<ExecutableElement,AnnotationMirror> propertyMethodAnnotationMap(TypeElement type, com.google.common.collect.ImmutableSet<ExecutableElement> propertyMethods) -
propertyMethodAnnotations
private com.google.common.collect.ImmutableList<AnnotationMirror> propertyMethodAnnotations(TypeElement type, ExecutableElement method) -
annotationVisibleFrom
-
wildcardTypeParametersString
Returns the@AutoValue
or@AutoOneOf
type parameters, with a ? for every type. If we have@AutoValue abstract class Foo<T extends Something>
then this method will return just<?>
. -
getAnnotationMirror
-
hasAnnotationMirror
-
writeSourceFile
-