Class AutoAnnotationProcessor

  • All Implemented Interfaces:
    javax.annotation.processing.Processor

    @AutoService(javax.annotation.processing.Processor.class)
    @SupportedAnnotationTypes("com.google.auto.value.AutoAnnotation")
    public class AutoAnnotationProcessor
    extends javax.annotation.processing.AbstractProcessor
    Javac annotation processor (compiler plugin) to generate annotation implementations. User code never references this class.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private javax.lang.model.util.Elements elementUtils  
      private javax.lang.model.util.Types typeUtils  
      • Fields inherited from class javax.annotation.processing.AbstractProcessor

        processingEnv
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private AbortProcessingException abortWithError​(java.lang.String msg, javax.lang.model.element.Element e)
      Issue a compilation error and return an exception that, when thrown, will cause the processing of this class to be abandoned.
      private boolean compatibleTypes​(javax.lang.model.type.TypeMirror parameterType, javax.lang.model.type.TypeMirror memberType)
      Returns true if parameterType can be used to provide the value of an annotation member of type memberType.
      private static java.lang.String fullyQualifiedName​(java.lang.String pkg, java.lang.String cls)  
      private java.lang.String generatedClassName​(javax.lang.model.element.ExecutableElement method)  
      private javax.lang.model.element.TypeElement getAnnotationReturnType​(javax.lang.model.element.ExecutableElement method)  
      private com.google.common.collect.ImmutableMap<java.lang.String,​javax.lang.model.element.AnnotationValue> getDefaultValues​(javax.lang.model.element.TypeElement annotationElement)  
      private java.lang.String getGeneratedTypeName()  
      private com.google.common.collect.ImmutableMap<java.lang.String,​javax.lang.model.element.ExecutableElement> getMemberMethods​(javax.lang.model.element.TypeElement annotationElement)  
      private com.google.common.collect.ImmutableMap<java.lang.String,​AutoAnnotationProcessor.Member> getMembers​(javax.lang.model.element.Element context, com.google.common.collect.ImmutableMap<java.lang.String,​javax.lang.model.element.ExecutableElement> memberMethods)  
      private com.google.common.collect.ImmutableMap<java.lang.String,​AutoAnnotationProcessor.Parameter> getParameters​(javax.lang.model.element.TypeElement annotationElement, javax.lang.model.element.ExecutableElement method, java.util.Map<java.lang.String,​AutoAnnotationProcessor.Member> members)  
      javax.lang.model.SourceVersion getSupportedSourceVersion()  
      private javax.lang.model.type.TypeMirror getTypeMirror​(java.lang.Class<?> c)  
      private static java.util.Optional<java.lang.Integer> invariableHash​(java.util.List<? extends javax.lang.model.element.AnnotationValue> annotationValues)  
      private static java.util.Optional<java.lang.Integer> invariableHash​(javax.lang.model.element.AnnotationValue annotationValue)
      Returns the hashCode of the given AnnotationValue, if that hashCode is guaranteed to be always the same.
      private static com.google.common.collect.ImmutableMap<java.lang.String,​java.lang.Integer> invariableHashes​(com.google.common.collect.ImmutableMap<java.lang.String,​AutoAnnotationProcessor.Member> members, com.google.common.collect.ImmutableSet<java.lang.String> parameters)
      Returns a map from the names of members with invariable hashCodes to the values of those hashCodes.
      private static boolean isGwtCompatible​(javax.lang.model.element.TypeElement annotationElement)  
      private boolean methodsAreOverloaded​(java.util.List<javax.lang.model.element.ExecutableElement> methods)  
      boolean process​(java.util.Set<? extends javax.lang.model.element.TypeElement> annotations, javax.annotation.processing.RoundEnvironment roundEnv)  
      private void process​(javax.annotation.processing.RoundEnvironment roundEnv)  
      private void processMethod​(javax.lang.model.element.ExecutableElement method)  
      private void reportError​(javax.lang.model.element.Element e, java.lang.String msg, java.lang.Object... msgParams)
      Issue a compilation error.
      private void validateParameters​(javax.lang.model.element.TypeElement annotationElement, javax.lang.model.element.ExecutableElement method, com.google.common.collect.ImmutableMap<java.lang.String,​AutoAnnotationProcessor.Member> members, com.google.common.collect.ImmutableMap<java.lang.String,​AutoAnnotationProcessor.Parameter> parameters, com.google.common.collect.ImmutableMap<java.lang.String,​javax.lang.model.element.AnnotationValue> defaultValues)  
      private java.util.Set<java.lang.Class<?>> wrapperTypesUsedInCollections​(javax.lang.model.element.ExecutableElement method)
      Returns the wrapper types (Integer.class etc) that are used in collection parameters like List<Integer>.
      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, init, isInitialized
      • Methods inherited from class java.lang.Object

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

      • elementUtils

        private javax.lang.model.util.Elements elementUtils
      • typeUtils

        private javax.lang.model.util.Types typeUtils
    • Constructor Detail

      • AutoAnnotationProcessor

        public AutoAnnotationProcessor()
    • Method Detail

      • getSupportedSourceVersion

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

        private void reportError​(javax.lang.model.element.Element e,
                                 java.lang.String msg,
                                 java.lang.Object... msgParams)
        Issue a compilation error. This method does not throw an exception, since we want to continue processing and perhaps report other errors.
      • abortWithError

        private AbortProcessingException abortWithError​(java.lang.String msg,
                                                        javax.lang.model.element.Element e)
        Issue a compilation error and return an exception that, when thrown, will cause the processing of this class to be abandoned. This does not prevent the processing of other classes.
      • process

        public 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
      • process

        private void process​(javax.annotation.processing.RoundEnvironment roundEnv)
      • processMethod

        private void processMethod​(javax.lang.model.element.ExecutableElement method)
      • getGeneratedTypeName

        private java.lang.String getGeneratedTypeName()
      • invariableHash

        private static java.util.Optional<java.lang.Integer> invariableHash​(javax.lang.model.element.AnnotationValue annotationValue)
        Returns the hashCode of the given AnnotationValue, if that hashCode is guaranteed to be always the same. The hashCode of a String or primitive type never changes. The hashCode of a Class or an enum constant does potentially change in different runs of the same program. The hashCode of an array doesn't change if the hashCodes of its elements don't. Although we could have a similar rule for nested annotation values, we currently don't.
      • invariableHash

        private static java.util.Optional<java.lang.Integer> invariableHash​(java.util.List<? extends javax.lang.model.element.AnnotationValue> annotationValues)
      • invariableHashes

        private static com.google.common.collect.ImmutableMap<java.lang.String,​java.lang.Integer> invariableHashes​(com.google.common.collect.ImmutableMap<java.lang.String,​AutoAnnotationProcessor.Member> members,
                                                                                                                         com.google.common.collect.ImmutableSet<java.lang.String> parameters)
        Returns a map from the names of members with invariable hashCodes to the values of those hashCodes.
      • methodsAreOverloaded

        private boolean methodsAreOverloaded​(java.util.List<javax.lang.model.element.ExecutableElement> methods)
      • generatedClassName

        private java.lang.String generatedClassName​(javax.lang.model.element.ExecutableElement method)
      • getAnnotationReturnType

        private javax.lang.model.element.TypeElement getAnnotationReturnType​(javax.lang.model.element.ExecutableElement method)
      • getMemberMethods

        private com.google.common.collect.ImmutableMap<java.lang.String,​javax.lang.model.element.ExecutableElement> getMemberMethods​(javax.lang.model.element.TypeElement annotationElement)
      • getMembers

        private com.google.common.collect.ImmutableMap<java.lang.String,​AutoAnnotationProcessor.Member> getMembers​(javax.lang.model.element.Element context,
                                                                                                                         com.google.common.collect.ImmutableMap<java.lang.String,​javax.lang.model.element.ExecutableElement> memberMethods)
      • getDefaultValues

        private com.google.common.collect.ImmutableMap<java.lang.String,​javax.lang.model.element.AnnotationValue> getDefaultValues​(javax.lang.model.element.TypeElement annotationElement)
      • getParameters

        private com.google.common.collect.ImmutableMap<java.lang.String,​AutoAnnotationProcessor.Parameter> getParameters​(javax.lang.model.element.TypeElement annotationElement,
                                                                                                                               javax.lang.model.element.ExecutableElement method,
                                                                                                                               java.util.Map<java.lang.String,​AutoAnnotationProcessor.Member> members)
      • validateParameters

        private void validateParameters​(javax.lang.model.element.TypeElement annotationElement,
                                        javax.lang.model.element.ExecutableElement method,
                                        com.google.common.collect.ImmutableMap<java.lang.String,​AutoAnnotationProcessor.Member> members,
                                        com.google.common.collect.ImmutableMap<java.lang.String,​AutoAnnotationProcessor.Parameter> parameters,
                                        com.google.common.collect.ImmutableMap<java.lang.String,​javax.lang.model.element.AnnotationValue> defaultValues)
      • compatibleTypes

        private boolean compatibleTypes​(javax.lang.model.type.TypeMirror parameterType,
                                        javax.lang.model.type.TypeMirror memberType)
        Returns true if parameterType can be used to provide the value of an annotation member of type memberType. They must either be the same type, or the member type must be an array and the parameter type must be a collection of a compatible type.
      • wrapperTypesUsedInCollections

        private java.util.Set<java.lang.Class<?>> wrapperTypesUsedInCollections​(javax.lang.model.element.ExecutableElement method)
        Returns the wrapper types (Integer.class etc) that are used in collection parameters like List<Integer>. This is needed because we will emit a helper method for each such type, for example to convert Collection<Integer> into int[].
      • getTypeMirror

        private javax.lang.model.type.TypeMirror getTypeMirror​(java.lang.Class<?> c)
      • isGwtCompatible

        private static boolean isGwtCompatible​(javax.lang.model.element.TypeElement annotationElement)
      • fullyQualifiedName

        private static java.lang.String fullyQualifiedName​(java.lang.String pkg,
                                                           java.lang.String cls)
      • writeSourceFile

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