Class AutoServiceProcessor

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

    @SupportedOptions({"debug","verify"})
    public class AutoServiceProcessor
    extends javax.annotation.processing.AbstractProcessor
    Processes AutoService annotations and generates the service provider configuration files described in ServiceLoader.

    Processor Options:

    • -Adebug - turns on debug statements
    • -Averify=true - turns on extra verification
    • Field Summary

      Fields 
      Modifier and Type Field Description
      (package private) static java.lang.String MISSING_SERVICES_ERROR  
      private com.google.common.collect.Multimap<java.lang.String,​java.lang.String> providers
      Maps the class names of service provider interfaces to the class names of the concrete classes which implement them.
      • 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 boolean checkImplementer​(javax.lang.model.element.TypeElement providerImplementer, javax.lang.model.element.TypeElement providerType, javax.lang.model.element.AnnotationMirror annotationMirror)
      Verifies ServiceProvider constraints on the concrete provider class.
      private void error​(java.lang.String msg, javax.lang.model.element.Element element, javax.lang.model.element.AnnotationMirror annotation)  
      private void fatalError​(java.lang.String msg)  
      private void generateConfigFiles()  
      private java.lang.String getBinaryName​(javax.lang.model.element.TypeElement element)
      Returns the binary name of a reference type.
      private java.lang.String getBinaryNameImpl​(javax.lang.model.element.TypeElement element, java.lang.String className)  
      com.google.common.collect.ImmutableSet<java.lang.String> getSupportedAnnotationTypes()  
      javax.lang.model.SourceVersion getSupportedSourceVersion()  
      private com.google.common.collect.ImmutableSet<javax.lang.model.type.DeclaredType> getValueFieldOfClasses​(javax.lang.model.element.AnnotationMirror annotationMirror)
      Returns the contents of a Class[]-typed "value" field in a given annotationMirror.
      private void log​(java.lang.String msg)  
      boolean process​(java.util.Set<? extends javax.lang.model.element.TypeElement> annotations, javax.annotation.processing.RoundEnvironment roundEnv)
      For each class annotated with AutoService Verify the AutoService interface value is correct Categorize the class by its service interface For each AutoService interface Create a file named META-INF/services/<interface> For each AutoService annotated class for this interface Create an entry in the file
      private void processAnnotations​(java.util.Set<? extends javax.lang.model.element.TypeElement> annotations, javax.annotation.processing.RoundEnvironment roundEnv)  
      private void processImpl​(java.util.Set<? extends javax.lang.model.element.TypeElement> annotations, javax.annotation.processing.RoundEnvironment roundEnv)  
      private static boolean rawTypesSuppressed​(javax.lang.model.element.Element element)  
      private void warning​(java.lang.String msg, javax.lang.model.element.Element element, javax.lang.model.element.AnnotationMirror annotation)  
      • Methods inherited from class javax.annotation.processing.AbstractProcessor

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

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

      • MISSING_SERVICES_ERROR

        static final java.lang.String MISSING_SERVICES_ERROR
        See Also:
        Constant Field Values
      • providers

        private final com.google.common.collect.Multimap<java.lang.String,​java.lang.String> providers
        Maps the class names of service provider interfaces to the class names of the concrete classes which implement them.

        For example, "com.google.apphosting.LocalRpcService" -> "com.google.apphosting.datastore.LocalDatastoreService"

    • Constructor Detail

      • AutoServiceProcessor

        public AutoServiceProcessor()
    • Method Detail

      • getSupportedAnnotationTypes

        public com.google.common.collect.ImmutableSet<java.lang.String> getSupportedAnnotationTypes()
        Specified by:
        getSupportedAnnotationTypes in interface javax.annotation.processing.Processor
        Overrides:
        getSupportedAnnotationTypes in class javax.annotation.processing.AbstractProcessor
      • getSupportedSourceVersion

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

        public boolean process​(java.util.Set<? extends javax.lang.model.element.TypeElement> annotations,
                               javax.annotation.processing.RoundEnvironment roundEnv)
        1. For each class annotated with AutoService
          • Verify the AutoService interface value is correct
          • Categorize the class by its service interface
        2. For each AutoService interface
          • Create a file named META-INF/services/<interface>
          • For each AutoService annotated class for this interface
            • Create an entry in the file
        Specified by:
        process in interface javax.annotation.processing.Processor
        Specified by:
        process in class javax.annotation.processing.AbstractProcessor
      • processImpl

        private void processImpl​(java.util.Set<? extends javax.lang.model.element.TypeElement> annotations,
                                 javax.annotation.processing.RoundEnvironment roundEnv)
      • processAnnotations

        private void processAnnotations​(java.util.Set<? extends javax.lang.model.element.TypeElement> annotations,
                                        javax.annotation.processing.RoundEnvironment roundEnv)
      • generateConfigFiles

        private void generateConfigFiles()
      • checkImplementer

        private boolean checkImplementer​(javax.lang.model.element.TypeElement providerImplementer,
                                         javax.lang.model.element.TypeElement providerType,
                                         javax.lang.model.element.AnnotationMirror annotationMirror)
        Verifies ServiceProvider constraints on the concrete provider class. Note that these constraints are enforced at runtime via the ServiceLoader, we're just checking them at compile time to be extra nice to our users.
      • rawTypesSuppressed

        private static boolean rawTypesSuppressed​(javax.lang.model.element.Element element)
      • getBinaryName

        private java.lang.String getBinaryName​(javax.lang.model.element.TypeElement element)
        Returns the binary name of a reference type. For example, com.google.Foo$Bar, instead of com.google.Foo.Bar.
      • getBinaryNameImpl

        private java.lang.String getBinaryNameImpl​(javax.lang.model.element.TypeElement element,
                                                   java.lang.String className)
      • getValueFieldOfClasses

        private com.google.common.collect.ImmutableSet<javax.lang.model.type.DeclaredType> getValueFieldOfClasses​(javax.lang.model.element.AnnotationMirror annotationMirror)
        Returns the contents of a Class[]-typed "value" field in a given annotationMirror.
      • log

        private void log​(java.lang.String msg)
      • warning

        private void warning​(java.lang.String msg,
                             javax.lang.model.element.Element element,
                             javax.lang.model.element.AnnotationMirror annotation)
      • error

        private void error​(java.lang.String msg,
                           javax.lang.model.element.Element element,
                           javax.lang.model.element.AnnotationMirror annotation)
      • fatalError

        private void fatalError​(java.lang.String msg)