Class AutoValueOrOneOfProcessor

  • All Implemented Interfaces:
    javax.annotation.processing.Processor
    Direct Known Subclasses:
    AutoOneOfProcessor, AutoValueProcessor

    abstract class AutoValueOrOneOfProcessor
    extends javax.annotation.processing.AbstractProcessor
    Shared code between AutoValueProcessor and AutoOneOfProcessor.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.String annotationClassName  
      private javax.lang.model.element.TypeElement annotationType
      The annotation we are processing, AutoValue or AutoOneOf.
      private java.util.List<java.lang.String> deferredTypeNames
      Qualified names of @AutoValue or AutoOneOf 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 errorReporter  
      private java.lang.String simpleAnnotationName
      The simple name of annotationType.
      • Fields inherited from class javax.annotation.processing.AbstractProcessor

        processingEnv
    • Constructor Summary

      Constructors 
      Constructor Description
      AutoValueOrOneOfProcessor​(java.lang.String annotationClassName)  
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) static com.google.common.collect.ImmutableSet<javax.lang.model.element.ExecutableElement> abstractMethodsIn​(com.google.common.collect.ImmutableSet<javax.lang.model.element.ExecutableElement> methods)
      Returns the subset of all abstract methods in the given set of methods.
      (package private) com.google.common.collect.ImmutableList<javax.lang.model.element.AnnotationMirror> annotationsToCopy​(javax.lang.model.element.Element autoValueType, javax.lang.model.element.Element typeOrMethod, java.util.Set<java.lang.String> excludedAnnotations)
      Implements the semantics of AutoValue.CopyAnnotations; see its javadoc.
      (package private) static com.google.common.collect.ImmutableList<java.lang.String> annotationStrings​(java.util.List<? extends javax.lang.model.element.AnnotationMirror> annotations)
      Returns the spelling to be used in the generated code for the given list of annotations.
      private boolean annotationVisibleFrom​(javax.lang.model.element.AnnotationMirror annotation, javax.lang.model.element.Element from)  
      (package private) void checkModifiersIfNested​(javax.lang.model.element.TypeElement type)
      Checks that, if the given @AutoValue or @AutoOneOf class is nested, it is static and not private.
      (package private) void checkReturnType​(javax.lang.model.element.TypeElement autoValueClass, javax.lang.model.element.ExecutableElement getter)
      Checks that the return type of the given property method is allowed.
      private com.google.common.collect.ImmutableList<java.lang.String> copiedClassAnnotations​(javax.lang.model.element.TypeElement type)  
      private com.google.common.collect.ImmutableList<java.lang.String> copyAnnotations​(javax.lang.model.element.Element autoValueType, javax.lang.model.element.Element typeOrMethod, java.util.Set<java.lang.String> excludedAnnotations)
      Implements the semantics of AutoValue.CopyAnnotations; see its javadoc.
      (package private) void defineSharedVarsForType​(javax.lang.model.element.TypeElement type, com.google.common.collect.ImmutableSet<javax.lang.model.element.ExecutableElement> methods, AutoValueOrOneOfTemplateVars vars)
      Defines the template variables that are shared by AutoValue and AutoOneOf.
      private static java.util.Map<AutoValueOrOneOfProcessor.ObjectMethod,​javax.lang.model.element.ExecutableElement> determineObjectMethodsToGenerate​(java.util.Set<javax.lang.model.element.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.
      private static java.lang.String disambiguate​(java.lang.String name, java.util.Collection<java.lang.String> existingNames)  
      (package private) javax.lang.model.util.Elements elementUtils()  
      (package private) static java.lang.String equalsParameterType​(java.util.Map<AutoValueOrOneOfProcessor.ObjectMethod,​javax.lang.model.element.ExecutableElement> methodsToGenerate)
      Returns the encoded parameter type of the equals(Object) method that is to be generated, or an empty string if the method is not being generated.
      (package private) ErrorReporter errorReporter()  
      (package private) static void fixReservedIdentifiers​(java.util.Map<?,​java.lang.String> methodToIdentifier)
      Modifies the values of the given map to avoid reserved words.
      (package private) static java.lang.String generatedClassName​(javax.lang.model.element.TypeElement type, java.lang.String prefix)
      Returns the name of the generated @AutoValue or @AutoOneOf class, for example AutoOneOf_TaskResult or $$AutoValue_SimpleMethod.
      private static java.lang.String getAnnotationFqName​(javax.lang.model.element.AnnotationMirror annotation)
      Returns the fully-qualified name of an annotation-mirror, e.g.
      (package private) static java.util.Optional<javax.lang.model.element.AnnotationMirror> getAnnotationMirror​(javax.lang.model.element.Element element, java.lang.String annotationName)  
      private static java.util.Set<java.lang.String> getAnnotationsMarkedWithInherited​(javax.lang.model.element.Element element)  
      private java.util.Set<java.lang.String> getExcludedClasses​(javax.lang.model.element.Element element)
      Returns the contents of the AutoValue.CopyAnnotations.exclude element, as a set of strings that are fully-qualified class names.
      (package private) java.lang.String getSerialVersionUID​(javax.lang.model.element.TypeElement type)
      Returns a string like "1234L" if type instanceof Serializable and defines serialVersionUID = 1234L; otherwise "".
      javax.lang.model.SourceVersion getSupportedSourceVersion()  
      private static boolean gettersAllPrefixed​(java.util.Set<javax.lang.model.element.ExecutableElement> methods)  
      (package private) static boolean hasAnnotationMirror​(javax.lang.model.element.Element element, java.lang.String annotationName)  
      void init​(javax.annotation.processing.ProcessingEnvironment processingEnv)  
      private boolean isInAutoValuePackage​(java.lang.String className)
      True if the given class name is in the com.google.auto.value package or a subpackage.
      private static boolean isJavaLangObject​(javax.lang.model.element.TypeElement type)  
      private static java.lang.String nameWithoutPrefix​(java.lang.String name)
      Returns the name of the property defined by the given getter.
      (package private) abstract java.util.Optional<java.lang.String> nullableAnnotationForMethod​(javax.lang.model.element.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 objectMethodToOverride​(javax.lang.model.element.ExecutableElement method)
      Determines which of the three public non-final methods from java.lang.Object, if any, is overridden by the given method.
      (package private) static com.google.common.collect.ImmutableSet<javax.lang.model.element.ExecutableElement> prefixedGettersIn​(java.lang.Iterable<javax.lang.model.element.ExecutableElement> methods)
      Returns the subset of the given zero-arg methods whose names begin with get.
      boolean process​(java.util.Set<? extends javax.lang.model.element.TypeElement> annotations, javax.annotation.processing.RoundEnvironment roundEnv)  
      (package private) abstract void processType​(javax.lang.model.element.TypeElement type)
      Analyzes a single @AutoValue or @AutoOneOf class, and outputs the corresponding implementation class or classes.
      (package private) com.google.common.collect.ImmutableListMultimap<javax.lang.model.element.ExecutableElement,​javax.lang.model.element.AnnotationMirror> propertyMethodAnnotationMap​(javax.lang.model.element.TypeElement type, com.google.common.collect.ImmutableSet<javax.lang.model.element.ExecutableElement> propertyMethods)  
      private com.google.common.collect.ImmutableList<javax.lang.model.element.AnnotationMirror> propertyMethodAnnotations​(javax.lang.model.element.TypeElement type, javax.lang.model.element.ExecutableElement method)  
      (package private) static com.google.common.collect.ImmutableSet<javax.lang.model.element.ExecutableElement> propertyMethodsIn​(java.util.Set<javax.lang.model.element.ExecutableElement> abstractMethods)
      Returns the subset of property methods in the given set of abstract methods.
      (package private) com.google.common.collect.ImmutableBiMap<java.lang.String,​javax.lang.model.element.ExecutableElement> propertyNameToMethodMap​(java.util.Set<javax.lang.model.element.ExecutableElement> propertyMethods)
      Returns a bi-map between property names and the corresponding abstract property methods.
      (package private) com.google.common.collect.ImmutableSet<AutoValueOrOneOfProcessor.Property> propertySet​(javax.lang.model.element.TypeElement type, com.google.common.collect.ImmutableSet<javax.lang.model.element.ExecutableElement> propertyMethods, com.google.common.collect.ImmutableListMultimap<javax.lang.model.element.ExecutableElement,​javax.lang.model.element.AnnotationMirror> annotatedPropertyMethods)
      Returns the ordered set of AutoValueOrOneOfProcessor.Property definitions for the given @AutoValue or AutoOneOf type.
      (package private) javax.lang.model.util.Types typeUtils()  
      private void warnAboutPrimitiveArrays​(javax.lang.model.element.TypeElement autoValueClass, javax.lang.model.element.ExecutableElement getter)  
      private static java.lang.String wildcardTypeParametersString​(javax.lang.model.element.TypeElement type)
      Returns the @AutoValue or @AutoOneOf type parameters, with a ? for every type.
      (package private) void writeSourceFile​(java.lang.String className, java.lang.String text, javax.lang.model.element.TypeElement originatingType)  
      • Methods inherited from class javax.annotation.processing.AbstractProcessor

        getCompletions, getSupportedAnnotationTypes, getSupportedOptions, isInitialized
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • annotationClassName

        private final java.lang.String annotationClassName
      • deferredTypeNames

        private final java.util.List<java.lang.String> deferredTypeNames
        Qualified names of @AutoValue or AutoOneOf 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

        private javax.lang.model.element.TypeElement annotationType
        The annotation we are processing, AutoValue or AutoOneOf.
      • simpleAnnotationName

        private java.lang.String simpleAnnotationName
        The simple name of annotationType.
    • Constructor Detail

      • AutoValueOrOneOfProcessor

        AutoValueOrOneOfProcessor​(java.lang.String annotationClassName)
    • Method Detail

      • init

        public void init​(javax.annotation.processing.ProcessingEnvironment processingEnv)
        Specified by:
        init in interface javax.annotation.processing.Processor
        Overrides:
        init in class javax.annotation.processing.AbstractProcessor
      • typeUtils

        final javax.lang.model.util.Types typeUtils()
      • elementUtils

        final javax.lang.model.util.Elements elementUtils()
      • getSupportedSourceVersion

        public final javax.lang.model.SourceVersion getSupportedSourceVersion()
        Specified by:
        getSupportedSourceVersion in interface javax.annotation.processing.Processor
        Overrides:
        getSupportedSourceVersion in class javax.annotation.processing.AbstractProcessor
      • process

        public final boolean process​(java.util.Set<? extends javax.lang.model.element.TypeElement> annotations,
                                     javax.annotation.processing.RoundEnvironment roundEnv)
        Specified by:
        process in interface javax.annotation.processing.Processor
        Specified by:
        process in class javax.annotation.processing.AbstractProcessor
      • processType

        abstract void processType​(javax.lang.model.element.TypeElement type)
        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

        abstract java.util.Optional<java.lang.String> nullableAnnotationForMethod​(javax.lang.model.element.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. The annotation in question is on the method, not its return type. If instead the return type is @Nullable, this method returns Optional.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​(javax.lang.model.element.TypeElement type,
                                                                                                     com.google.common.collect.ImmutableSet<javax.lang.model.element.ExecutableElement> propertyMethods,
                                                                                                     com.google.common.collect.ImmutableListMultimap<javax.lang.model.element.ExecutableElement,​javax.lang.model.element.AnnotationMirror> annotatedPropertyMethods)
        Returns the ordered set of AutoValueOrOneOfProcessor.Property definitions for the given @AutoValue or AutoOneOf 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 by AutoValue.CopyAnnotations also do not appear here.
      • defineSharedVarsForType

        final void defineSharedVarsForType​(javax.lang.model.element.TypeElement type,
                                           com.google.common.collect.ImmutableSet<javax.lang.model.element.ExecutableElement> methods,
                                           AutoValueOrOneOfTemplateVars vars)
        Defines the template variables that are shared by AutoValue and AutoOneOf.
      • annotationStrings

        static com.google.common.collect.ImmutableList<java.lang.String> annotationStrings​(java.util.List<? extends javax.lang.model.element.AnnotationMirror> annotations)
        Returns the spelling to be used in the generated code for the given list of annotations.
      • generatedClassName

        static java.lang.String generatedClassName​(javax.lang.model.element.TypeElement type,
                                                   java.lang.String prefix)
        Returns the name of the generated @AutoValue or @AutoOneOf class, for example AutoOneOf_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

        private static boolean isJavaLangObject​(javax.lang.model.element.TypeElement type)
      • objectMethodToOverride

        static AutoValueOrOneOfProcessor.ObjectMethod objectMethodToOverride​(javax.lang.model.element.ExecutableElement method)
        Determines which of the three public non-final methods from java.lang.Object, if any, is overridden by the given method.
      • propertyNameToMethodMap

        final com.google.common.collect.ImmutableBiMap<java.lang.String,​javax.lang.model.element.ExecutableElement> propertyNameToMethodMap​(java.util.Set<javax.lang.model.element.ExecutableElement> propertyMethods)
        Returns a bi-map between property names and the corresponding abstract property methods.
      • gettersAllPrefixed

        private static boolean gettersAllPrefixed​(java.util.Set<javax.lang.model.element.ExecutableElement> methods)
      • prefixedGettersIn

        static com.google.common.collect.ImmutableSet<javax.lang.model.element.ExecutableElement> prefixedGettersIn​(java.lang.Iterable<javax.lang.model.element.ExecutableElement> methods)
        Returns the subset of the given zero-arg methods whose names begin with get. Also includes isFoo methods if they return boolean. This corresponds to JavaBeans conventions.
      • nameWithoutPrefix

        private static java.lang.String nameWithoutPrefix​(java.lang.String name)
        Returns the name of the property defined by the given getter. A getter called getFoo() or isFoo() defines a property called foo. For consistency with JavaBeans, a getter called getHTMLPage() defines a property called HTMLPage. The rule is: the name of the property is the part after get or is, with the first letter lowercased unless the first two letters are uppercase. This works well for the HTMLPage example, but in these more enlightened times we use HtmlPage anyway, so the special behaviour is not useful, and of course it behaves poorly with examples like OAuth.
      • checkModifiersIfNested

        final void checkModifiersIfNested​(javax.lang.model.element.TypeElement type)
        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

        static void fixReservedIdentifiers​(java.util.Map<?,​java.lang.String> methodToIdentifier)
        Modifies the values of the given map to avoid reserved words. If we have a getter called getPackage() then we can't use the identifier package to represent its value since that's a reserved word.
      • disambiguate

        private static java.lang.String disambiguate​(java.lang.String name,
                                                     java.util.Collection<java.lang.String> existingNames)
      • determineObjectMethodsToGenerate

        private static java.util.Map<AutoValueOrOneOfProcessor.ObjectMethod,​javax.lang.model.element.ExecutableElement> determineObjectMethodsToGenerate​(java.util.Set<javax.lang.model.element.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 in Object or an abstract method in the @AutoValue class or an ancestor.
      • equalsParameterType

        static java.lang.String equalsParameterType​(java.util.Map<AutoValueOrOneOfProcessor.ObjectMethod,​javax.lang.model.element.ExecutableElement> methodsToGenerate)
        Returns the encoded parameter type of the equals(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<javax.lang.model.element.ExecutableElement> abstractMethodsIn​(com.google.common.collect.ImmutableSet<javax.lang.model.element.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<javax.lang.model.element.ExecutableElement> propertyMethodsIn​(java.util.Set<javax.lang.model.element.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 not hashCode() or toString().
      • checkReturnType

        final void checkReturnType​(javax.lang.model.element.TypeElement autoValueClass,
                                   javax.lang.model.element.ExecutableElement getter)
        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

        private void warnAboutPrimitiveArrays​(javax.lang.model.element.TypeElement autoValueClass,
                                              javax.lang.model.element.ExecutableElement getter)
      • getSerialVersionUID

        final java.lang.String getSerialVersionUID​(javax.lang.model.element.TypeElement type)
        Returns a string like "1234L" if type instanceof Serializable and defines serialVersionUID = 1234L; otherwise "".
      • annotationsToCopy

        com.google.common.collect.ImmutableList<javax.lang.model.element.AnnotationMirror> annotationsToCopy​(javax.lang.model.element.Element autoValueType,
                                                                                                             javax.lang.model.element.Element typeOrMethod,
                                                                                                             java.util.Set<java.lang.String> excludedAnnotations)
        Implements the semantics of AutoValue.CopyAnnotations; see its javadoc.
      • isInAutoValuePackage

        private boolean isInAutoValuePackage​(java.lang.String className)
        True if the given class name is in the com.google.auto.value package or a subpackage. False if the class name contains Test, since many AutoValue tests under com.google.auto.value define their own annotations.
      • copiedClassAnnotations

        private com.google.common.collect.ImmutableList<java.lang.String> copiedClassAnnotations​(javax.lang.model.element.TypeElement type)
      • copyAnnotations

        private com.google.common.collect.ImmutableList<java.lang.String> copyAnnotations​(javax.lang.model.element.Element autoValueType,
                                                                                          javax.lang.model.element.Element typeOrMethod,
                                                                                          java.util.Set<java.lang.String> excludedAnnotations)
        Implements the semantics of AutoValue.CopyAnnotations; see its javadoc.
      • getExcludedClasses

        private java.util.Set<java.lang.String> getExcludedClasses​(javax.lang.model.element.Element element)
        Returns the contents of the AutoValue.CopyAnnotations.exclude element, as a set of strings that are fully-qualified class names.
      • getAnnotationsMarkedWithInherited

        private static java.util.Set<java.lang.String> getAnnotationsMarkedWithInherited​(javax.lang.model.element.Element element)
      • getAnnotationFqName

        private static java.lang.String getAnnotationFqName​(javax.lang.model.element.AnnotationMirror annotation)
        Returns the fully-qualified name of an annotation-mirror, e.g. "com.google.auto.value.AutoValue".
      • propertyMethodAnnotationMap

        final com.google.common.collect.ImmutableListMultimap<javax.lang.model.element.ExecutableElement,​javax.lang.model.element.AnnotationMirror> propertyMethodAnnotationMap​(javax.lang.model.element.TypeElement type,
                                                                                                                                                                                      com.google.common.collect.ImmutableSet<javax.lang.model.element.ExecutableElement> propertyMethods)
      • propertyMethodAnnotations

        private com.google.common.collect.ImmutableList<javax.lang.model.element.AnnotationMirror> propertyMethodAnnotations​(javax.lang.model.element.TypeElement type,
                                                                                                                             javax.lang.model.element.ExecutableElement method)
      • annotationVisibleFrom

        private boolean annotationVisibleFrom​(javax.lang.model.element.AnnotationMirror annotation,
                                              javax.lang.model.element.Element from)
      • wildcardTypeParametersString

        private static java.lang.String wildcardTypeParametersString​(javax.lang.model.element.TypeElement type)
        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

        static java.util.Optional<javax.lang.model.element.AnnotationMirror> getAnnotationMirror​(javax.lang.model.element.Element element,
                                                                                                 java.lang.String annotationName)
      • hasAnnotationMirror

        static boolean hasAnnotationMirror​(javax.lang.model.element.Element element,
                                           java.lang.String annotationName)
      • writeSourceFile

        final void writeSourceFile​(java.lang.String className,
                                   java.lang.String text,
                                   javax.lang.model.element.TypeElement originatingType)