public abstract class BasicAnnotationProcessor
extends javax.annotation.processing.AbstractProcessor
Processor implementation that defers processing of Elements to later
rounds if they cannot be processed.
Subclasses put their processing logic in BasicAnnotationProcessor.Step implementations. The steps are passed to
the processor by returning them in the steps() method, and can access the ProcessingEnvironment using AbstractProcessor.processingEnv.
Any logic that needs to happen once per round can be specified by overriding postRound(RoundEnvironment).
Step. This helps processors to avoid many common pitfalls, such as ErrorType instances, 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 BasicAnnotationProcessor will 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.
Step can defer elementsEach Step can defer elements by including them in the set returned by BasicAnnotationProcessor.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 A cannot be processed because processing it depends on the existence of
some class B, then A should be deferred until a later round of processing, when
B will have been generated by another processor.
If A directly references B, then the well-formedness check will correctly
defer processing of A until B has been generated.
However, if A references B only indirectly (for example, from within a method
body), then the well-formedness check will not defer processing A, but a processing step
can reject A.
| Modifier and Type | Class and Description |
|---|---|
static interface |
BasicAnnotationProcessor.ProcessingStep
Deprecated.
Implement
BasicAnnotationProcessor.Step instead. See steps(). |
static interface |
BasicAnnotationProcessor.Step
The unit of processing logic that runs under the guarantee that all elements are complete and
well-formed.
|
| Constructor and Description |
|---|
BasicAnnotationProcessor() |
| Modifier and Type | Method and Description |
|---|---|
protected static BasicAnnotationProcessor.Step |
asStep(BasicAnnotationProcessor.ProcessingStep processingStep)
Wraps the passed
BasicAnnotationProcessor.ProcessingStep in a BasicAnnotationProcessor.Step. |
com.google.common.collect.ImmutableSet<java.lang.String> |
getSupportedAnnotationTypes()
Returns the set of supported annotation types as collected from registered processing steps.
|
void |
init(javax.annotation.processing.ProcessingEnvironment processingEnv) |
protected java.lang.Iterable<? extends BasicAnnotationProcessor.ProcessingStep> |
initSteps()
Deprecated.
Implement
steps() instead. |
protected void |
postProcess()
Deprecated.
use
postRound(RoundEnvironment) instead |
protected void |
postRound(javax.annotation.processing.RoundEnvironment roundEnv)
An optional hook for logic to be executed at the end of each round.
|
boolean |
process(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.
|
public final void init(javax.annotation.processing.ProcessingEnvironment processingEnv)
init in interface javax.annotation.processing.Processorinit in class javax.annotation.processing.AbstractProcessor@Deprecated protected java.lang.Iterable<? extends BasicAnnotationProcessor.ProcessingStep> initSteps()
steps() instead.AbstractProcessor.processingEnv
is guaranteed to be set when this method is invoked.protected java.lang.Iterable<? extends BasicAnnotationProcessor.Step> steps()
AbstractProcessor.processingEnv is
guaranteed to be set when this method is invoked.
Note: If you are migrating some steps from BasicAnnotationProcessor.ProcessingStep to BasicAnnotationProcessor.Step, then you
can call asStep(ProcessingStep) on any unmigrated steps.
@Deprecated protected void postProcess()
postRound(RoundEnvironment) insteadprotected void postRound(javax.annotation.processing.RoundEnvironment roundEnv)
public final com.google.common.collect.ImmutableSet<java.lang.String> getSupportedAnnotationTypes()
getSupportedAnnotationTypes in interface javax.annotation.processing.ProcessorgetSupportedAnnotationTypes in class javax.annotation.processing.AbstractProcessorpublic final boolean process(java.util.Set<? extends javax.lang.model.element.TypeElement> annotations,
javax.annotation.processing.RoundEnvironment roundEnv)
process in interface javax.annotation.processing.Processorprocess in class javax.annotation.processing.AbstractProcessorprotected static BasicAnnotationProcessor.Step asStep(BasicAnnotationProcessor.ProcessingStep processingStep)
BasicAnnotationProcessor.ProcessingStep in a BasicAnnotationProcessor.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
converted BasicAnnotationProcessor.ProcessingStep from steps().Copyright © 2021 Google LLC. All Rights Reserved.