public abstract class BasicAnnotationProcessor
extends javax.annotation.processing.AbstractProcessor
Processor
implementation that ensures that top-level Element
instances are well-formed before attempting to perform processing on any of their children. In
the event that they are not, the element will be enqueued for processing in a subsequent round.
This ensures that processors will avoid many common pitfalls like ErrorType
instances,
ClassCastException
s and badly coerced types.
The primary disadvantage to this processor is that any Element
that forms a circular
dependency with a type generated by a subclass of this processor will never compile because the
given Element
will never be fully complete. All such compilations will fail with an error
message on the offending type that describes the issue.
Subclasses should put their processing logic in BasicAnnotationProcessor.ProcessingStep
implementations. The
steps are passed to the processor by returning them in the initSteps()
method - access
the ProcessingEnvironment
using AbstractProcessor.processingEnv
. Finally, any logic that needs to
happen once per round can be specified by overriding postProcess()
.
Modifier and Type | Class and Description |
---|---|
static interface |
BasicAnnotationProcessor.ProcessingStep
The unit of processing logic that runs under the guarantee that all elements are complete and
well-formed.
|
Modifier and Type | Field and Description |
---|---|
private java.util.Set<java.lang.String> |
deferredPackageNames |
private java.util.Set<java.lang.String> |
deferredTypeNames |
private javax.lang.model.util.Elements |
elements |
private static java.lang.String |
INVALID_ELEMENT_MESSAGE_FORMAT |
private javax.annotation.processing.Messager |
messager |
private java.lang.String |
processorName |
private com.google.common.collect.ImmutableList<? extends BasicAnnotationProcessor.ProcessingStep> |
steps |
Constructor and Description |
---|
BasicAnnotationProcessor() |
Modifier and Type | Method and Description |
---|---|
private static void |
findAnnotatedElements(javax.lang.model.element.Element element,
com.google.common.collect.ImmutableSet<? extends java.lang.Class<? extends java.lang.annotation.Annotation>> annotationClasses,
com.google.common.collect.ImmutableSetMultimap.Builder<java.lang.Class<? extends java.lang.annotation.Annotation>,javax.lang.model.element.Element> builder) |
private static javax.lang.model.element.TypeElement |
getEnclosingType(javax.lang.model.element.Element element)
Returns the nearest enclosing
TypeElement to the current element, throwing
an IllegalArgumentException if the provided Element is a
PackageElement or is otherwise not enclosed by a type. |
private com.google.common.collect.ImmutableSet<? extends java.lang.Class<? extends java.lang.annotation.Annotation>> |
getSupportedAnnotationClasses() |
com.google.common.collect.ImmutableSet<java.lang.String> |
getSupportedAnnotationTypes()
Returns the set of supported annotation types as a collected from registered
processing steps.
|
void |
init(javax.annotation.processing.ProcessingEnvironment processingEnv) |
protected abstract java.lang.Iterable<? extends BasicAnnotationProcessor.ProcessingStep> |
initSteps()
The mechanism by which processing steps are associated with the
processor.
|
protected void |
postProcess()
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) |
private void |
reportMissingElements(java.util.Map<java.lang.String,? extends com.google.common.base.Optional<? extends javax.lang.model.element.Element>> missingElements) |
private final java.util.Set<java.lang.String> deferredPackageNames
private final java.util.Set<java.lang.String> deferredTypeNames
private final java.lang.String processorName
private javax.lang.model.util.Elements elements
private javax.annotation.processing.Messager messager
private com.google.common.collect.ImmutableList<? extends BasicAnnotationProcessor.ProcessingStep> steps
private static final java.lang.String INVALID_ELEMENT_MESSAGE_FORMAT
public final void init(javax.annotation.processing.ProcessingEnvironment processingEnv)
init
in interface javax.annotation.processing.Processor
init
in class javax.annotation.processing.AbstractProcessor
protected abstract java.lang.Iterable<? extends BasicAnnotationProcessor.ProcessingStep> initSteps()
protected void postProcess()
private com.google.common.collect.ImmutableSet<? extends java.lang.Class<? extends java.lang.annotation.Annotation>> getSupportedAnnotationClasses()
public final com.google.common.collect.ImmutableSet<java.lang.String> getSupportedAnnotationTypes()
getSupportedAnnotationTypes
in interface javax.annotation.processing.Processor
getSupportedAnnotationTypes
in class javax.annotation.processing.AbstractProcessor
public final boolean process(java.util.Set<? extends javax.lang.model.element.TypeElement> annotations, javax.annotation.processing.RoundEnvironment roundEnv)
process
in interface javax.annotation.processing.Processor
process
in class javax.annotation.processing.AbstractProcessor
private void reportMissingElements(java.util.Map<java.lang.String,? extends com.google.common.base.Optional<? extends javax.lang.model.element.Element>> missingElements)
private static void findAnnotatedElements(javax.lang.model.element.Element element, com.google.common.collect.ImmutableSet<? extends java.lang.Class<? extends java.lang.annotation.Annotation>> annotationClasses, com.google.common.collect.ImmutableSetMultimap.Builder<java.lang.Class<? extends java.lang.annotation.Annotation>,javax.lang.model.element.Element> builder)
private static javax.lang.model.element.TypeElement getEnclosingType(javax.lang.model.element.Element element)
TypeElement
to the current element, throwing
an IllegalArgumentException
if the provided Element
is a
PackageElement
or is otherwise not enclosed by a type.