Class AbstractTypeProcessor
- All Implemented Interfaces:
Processor
- Direct Known Subclasses:
BasicTypeProcessor,SignaturePrinter,SourceChecker,TreePrinter
Type processing occurs in one round after the tool (e.g. Java compiler) analyzes the source (all sources taken as input to the tool and sources generated by other annotation processors).
The tool infrastructure will interact with classes extending this abstract class as follows.
1-3 are identical to the Processor life cycle. 4-5 are unique to
AbstractTypeProcessor subclasses.
- If an existing
Processorobject is not being used, to create an instance of a processor the tool calls the no-arg constructor of the processor class. - Next, the tool calls the
initmethod with an appropriateProcessingEnvironment. - Afterwards, the tool calls
getSupportedAnnotationTypes,getSupportedOptions, andgetSupportedSourceVersion. These methods are only called once per run, not on each round. - For each class containing a supported annotation, the tool calls
typeProcessmethod on theProcessor. The class is guaranteed to be type-checked Java code and all the tree type and symbol information is resolved. - Finally, the tool calls the
typeProcessingOvermethod on theProcessor.
The tool is permitted to ask type processors to process a class once it is analyzed before the
rest of classes are analyzed. The tool is also permitted to stop type processing immediately if
any errors are raised, without invoking typeProcessingOver().
The above describes the default, self-driven mode, in which this class registers its own
TaskListener. A host that already owns the compilation TaskListener can instead
call setExternallyDriven(boolean) and drive steps 4-5 itself; see typeProcessExternally(javax.lang.model.element.TypeElement, com.sun.source.util.TreePath)
and typeProcessingOverExternally().
A subclass may override any of the methods in this class, as long as the general Processor contract is obeyed, with one notable exception.
process(Set, RoundEnvironment) may not be overridden, as it is called during the
declaration annotation phase before classes are analyzed.
-
Field Summary
Fields inherited from class javax.annotation.processing.AbstractProcessor
processingEnv -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedConstructor for subclasses to call. -
Method Summary
Modifier and TypeMethodDescriptioncom.sun.tools.javac.util.LogReturn the compiler log, which contains errors and warnings.protected voidhandleProcessingError(String methodName, Throwable t) Handles a throwable thrown bytypeProcessingOver().voidprotected final booleanReturns true if a host drives this processor's type-processing lifecycle.voidpackageProcess(PackageElement element, TreePath tree) Processes a fully-analyzed package declaration, that is, thepackageclause of apackage-info.javain this compilation.final voidpackageProcessExternally(PackageElement element, TreePath tree) Processes a package declaration on behalf of an external host (seeexternallyDriven), handling the once-onlytypeProcessingStart()invocation.final booleanprocess(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) The use of this method is obsolete in type processors.voidsetExternallyDriven(boolean externallyDriven) Sets whether this processor is driven by an external host rather than by its ownTaskListener.abstract voidtypeProcess(TypeElement element, TreePath tree) Processes a fully-analyzed class that contains a supported annotation (seeAbstractProcessor.getSupportedAnnotationTypes()).final voidtypeProcessExternally(TypeElement element, TreePath tree) Drives a singletypeProcess(TypeElement, TreePath)invocation on behalf of an external host (seeexternallyDriven), handling the once-onlytypeProcessingStart()bracketing.voidA method to be called once all the classes are processed.final voidSignals, on behalf of an external host (seeexternallyDriven), that all classes have been processed.voidA method to be called once before the first call to typeProcess.Methods inherited from class javax.annotation.processing.AbstractProcessor
getCompletions, getSupportedAnnotationTypes, getSupportedOptions, getSupportedSourceVersion, isInitialized
-
Constructor Details
-
AbstractTypeProcessor
protected AbstractTypeProcessor()Constructor for subclasses to call.
-
-
Method Details
-
setExternallyDriven
public void setExternallyDriven(boolean externallyDriven) Sets whether this processor is driven by an external host rather than by its ownTaskListener. Must be called beforeinit(ProcessingEnvironment).A host that embeds the Checker Framework (such as the Error Prone plugin bridge) calls this with
trueand then drives the type-processing lifecycle throughtypeProcessExternally(TypeElement, TreePath)andtypeProcessingOverExternally().- Parameters:
externallyDriven- true if a host will drive the type-processing lifecycle- See Also:
-
isExternallyDriven
protected final boolean isExternallyDriven()Returns true if a host drives this processor's type-processing lifecycle.- Returns:
- true if a host drives the type-processing lifecycle
- See Also:
-
init
In the default (self-driven) mode, registers a
TaskListenerthat will get called after FLOW. In externally-driven mode (seeexternallyDriven), noTaskListeneris registered; the host drives the type-processing lifecycle instead.The
shouldStopPolicyis bumped to at leastFLOWin both modes. The bump is idempotent, so it is harmless if a host has already requested the same or a later stop policy.- Specified by:
initin interfaceProcessor- Overrides:
initin classAbstractProcessor
-
process
The use of this method is obsolete in type processors. The method is called during declaration annotation processing phase only. It registers the names of elements to process.- Specified by:
processin interfaceProcessor- Specified by:
processin classAbstractProcessor
-
typeProcessingStart
public void typeProcessingStart()A method to be called once before the first call to typeProcess.Subclasses may override this method to do any initialization work.
-
typeProcess
Processes a fully-analyzed class that contains a supported annotation (seeAbstractProcessor.getSupportedAnnotationTypes()).The passed class is always valid type-checked Java code.
- Parameters:
element- element of the analyzed classtree- the tree path to the element, with the leaf being aClassTree
-
packageProcess
Processes a fully-analyzed package declaration, that is, thepackageclause of apackage-info.javain this compilation.The
@Targetof an annotation may includeElementKind.PACKAGE, so a declaration annotation and its contradictions can appear here exactly as they can on a class. Apackage-info.javadeclares no type, however, sotypeProcess(javax.lang.model.element.TypeElement, com.sun.source.util.TreePath)is never invoked for one; this is the corresponding hook.Does nothing by default. A subclass that has nothing to check on a package declaration need not override it.
- Parameters:
element- the analyzed packagetree- the tree path to the package declaration, with the leaf being aPackageTree
-
handleProcessingError
Handles a throwable thrown bytypeProcessingOver(). The default implementation rethrows it, so that it is reported as an uncaught annotation processor exception. A subclass that reports a throwable as a compiler diagnostic overrides this.- Parameters:
methodName- the name of the method that threwtt- the throwable thatmethodNamethrew
-
typeProcessingOver
public void typeProcessingOver()A method to be called once all the classes are processed.Subclasses may override this method to do any aggregate analysis (e.g. generate report, persistence) or resource deallocation.
Method
getCompilerLog()can be used to access the number of compiler errors. -
typeProcessExternally
Drives a singletypeProcess(TypeElement, TreePath)invocation on behalf of an external host (seeexternallyDriven), handling the once-onlytypeProcessingStart()bracketing.Intended for hosts that own the compilation
TaskListener(e.g. an Error Prone plugin) and therefore cannot rely on this processor's ownAbstractTypeProcessor.AttributionTaskListener. The host must calltypeProcessingOverExternally()once, after the last class has been processed.Unlike the self-driven path, this method does not consult the
elementsset (which is only populated during the declaration annotation-processing round); the host is responsible for deciding which classes to process and when processing is over.- Parameters:
element- element of the analyzed classtree- the tree path to the element, with the leaf being aClassTree
-
packageProcessExternally
Processes a package declaration on behalf of an external host (seeexternallyDriven), handling the once-onlytypeProcessingStart()invocation.The counterpart of
typeProcessExternally(javax.lang.model.element.TypeElement, com.sun.source.util.TreePath)for apackage-info.java. A host needs it because theTaskListenerthat would otherwise dispatch the package declaration is not registered in externally-driven mode.- Parameters:
element- the package being processedtree- the path to the package declaration, with the leaf being aPackageTree
-
typeProcessingOverExternally
public final void typeProcessingOverExternally()Signals, on behalf of an external host (seeexternallyDriven), that all classes have been processed. InvokestypeProcessingOver()exactly once. Also invokestypeProcessingStart()first if it has not yet run (e.g. if no classes were processed).- See Also:
-
getCompilerLog
Return the compiler log, which contains errors and warnings.- Returns:
- the compiler log, which contains errors and warnings
-