Class BasicAnnotationProcessor
- java.lang.Object
-
- javax.annotation.processing.AbstractProcessor
-
- com.google.auto.common.BasicAnnotationProcessor
-
- All Implemented Interfaces:
javax.annotation.processing.Processor
public abstract class BasicAnnotationProcessor extends javax.annotation.processing.AbstractProcessorAn abstractProcessorimplementation that defers processing ofElements to later rounds if they cannot be processed.Subclasses put their processing logic in
BasicAnnotationProcessor.Stepimplementations. The steps are passed to the processor by returning them in thesteps()method, and can access theProcessingEnvironmentusingAbstractProcessor.processingEnv.Any logic that needs to happen once per round can be specified by overriding
postRound(RoundEnvironment).Ill-formed elements are deferred
Any annotated element whose nearest enclosing type is not well-formed is deferred, and not passed to anyStep. This helps processors to avoid many common pitfalls, such asErrorTypeinstances,ClassCastExceptions and badly coerced types.A non-package element is considered well-formed if its type, type parameters, parameters, default values, supertypes, annotations, and enclosed elements are. Package elements are treated similarly, except that their enclosed elements are not validated. See
SuperficialValidation.validateElement(Element)for details.The primary disadvantage to this validation is that any element that forms a circular dependency with a type generated by another
BasicAnnotationProcessorwill never compile because the element will never be fully complete. All such compilations will fail with an error message on the offending type that describes the issue.Each
Stepcan defer elementsEach
Stepcan defer elements by including them in the set returned byBasicAnnotationProcessor.Step.process(ImmutableSetMultimap); elements deferred by a step will be passed back to that step in a later round of processing.This feature is useful when one processor may depend on code generated by another, independent processor, in a way that isn't caught by the well-formedness check described above. For example, if an element
Acannot be processed because processing it depends on the existence of some classB, thenAshould be deferred until a later round of processing, whenBwill have been generated by another processor.If
Adirectly referencesB, then the well-formedness check will correctly defer processing ofAuntilBhas been generated.However, if
AreferencesBonly indirectly (for example, from within a method body), then the well-formedness check will not defer processingA, but a processing step can rejectA.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceBasicAnnotationProcessor.ProcessingStepDeprecated.ImplementBasicAnnotationProcessor.Stepinstead.static interfaceBasicAnnotationProcessor.StepThe unit of processing logic that runs under the guarantee that all elements are complete and well-formed.
-
Constructor Summary
Constructors Constructor Description BasicAnnotationProcessor()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected static BasicAnnotationProcessor.StepasStep(BasicAnnotationProcessor.ProcessingStep processingStep)Wraps the passedBasicAnnotationProcessor.ProcessingStepin aBasicAnnotationProcessor.Step.com.google.common.collect.ImmutableSet<java.lang.String>getSupportedAnnotationTypes()Returns the set of supported annotation types as collected from registered processing steps.voidinit(javax.annotation.processing.ProcessingEnvironment processingEnv)protected java.lang.Iterable<? extends BasicAnnotationProcessor.ProcessingStep>initSteps()Deprecated.Implementsteps()instead.protected voidpostProcess()Deprecated.usepostRound(RoundEnvironment)insteadprotected voidpostRound(javax.annotation.processing.RoundEnvironment roundEnv)An optional hook for logic to be executed at the end of each round.booleanprocess(java.util.Set<? extends javax.lang.model.element.TypeElement> annotations, javax.annotation.processing.RoundEnvironment roundEnv)protected java.lang.Iterable<? extends BasicAnnotationProcessor.Step>steps()Creates processing steps for this processor.
-
-
-
Method Detail
-
init
public final void init(javax.annotation.processing.ProcessingEnvironment processingEnv)
- Specified by:
initin interfacejavax.annotation.processing.Processor- Overrides:
initin classjavax.annotation.processing.AbstractProcessor
-
initSteps
@Deprecated protected java.lang.Iterable<? extends BasicAnnotationProcessor.ProcessingStep> initSteps()
Deprecated.Implementsteps()instead.Creates processing steps for this processor.AbstractProcessor.processingEnvis guaranteed to be set when this method is invoked.
-
steps
protected java.lang.Iterable<? extends BasicAnnotationProcessor.Step> steps()
Creates processing steps for this processor.AbstractProcessor.processingEnvis guaranteed to be set when this method is invoked.Note: If you are migrating some steps from
BasicAnnotationProcessor.ProcessingSteptoBasicAnnotationProcessor.Step, then you can callasStep(ProcessingStep)on any unmigrated steps.
-
postProcess
@Deprecated protected void postProcess()
Deprecated.usepostRound(RoundEnvironment)insteadAn optional hook for logic to be executed at the end of each round.
-
postRound
protected void postRound(javax.annotation.processing.RoundEnvironment roundEnv)
An optional hook for logic to be executed at the end of each round.
-
getSupportedAnnotationTypes
public final com.google.common.collect.ImmutableSet<java.lang.String> getSupportedAnnotationTypes()
Returns the set of supported annotation types as collected from registered processing steps.- Specified by:
getSupportedAnnotationTypesin interfacejavax.annotation.processing.Processor- Overrides:
getSupportedAnnotationTypesin classjavax.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:
processin interfacejavax.annotation.processing.Processor- Specified by:
processin classjavax.annotation.processing.AbstractProcessor
-
asStep
protected static BasicAnnotationProcessor.Step asStep(BasicAnnotationProcessor.ProcessingStep processingStep)
Wraps the passedBasicAnnotationProcessor.ProcessingStepin aBasicAnnotationProcessor.Step. This is a convenience method to allow incremental migration to a String-based API. This method can be used to return a not yet convertedBasicAnnotationProcessor.ProcessingStepfromsteps().
-
-