Class AbstractTypeProcessor

java.lang.Object
javax.annotation.processing.AbstractProcessor
org.checkerframework.javacutil.AbstractTypeProcessor
All Implemented Interfaces:
Processor
Direct Known Subclasses:
BasicTypeProcessor, SignaturePrinter, SourceChecker, TreePrinter

public abstract class AbstractTypeProcessor extends AbstractProcessor
This class is an abstract annotation processor designed to be a convenient superclass for concrete "type processors", processors that require the type information in the processed source.

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.

  1. If an existing Processor object is not being used, to create an instance of a processor the tool calls the no-arg constructor of the processor class.
  2. Next, the tool calls the init method with an appropriate ProcessingEnvironment.
  3. Afterwards, the tool calls getSupportedAnnotationTypes, getSupportedOptions, and getSupportedSourceVersion. These methods are only called once per run, not on each round.
  4. For each class containing a supported annotation, the tool calls typeProcess method on the Processor. The class is guaranteed to be type-checked Java code and all the tree type and symbol information is resolved.
  5. Finally, the tool calls the typeProcessingOver method on the Processor.

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.

  • 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 own TaskListener. Must be called before init(ProcessingEnvironment).

      A host that embeds the Checker Framework (such as the Error Prone plugin bridge) calls this with true and then drives the type-processing lifecycle through typeProcessExternally(TypeElement, TreePath) and typeProcessingOverExternally().

      Parameters:
      externallyDriven - true if a host will drive the type-processing lifecycle
      See Also:
      • externallyDriven
    • 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

      public void init(ProcessingEnvironment env)

      In the default (self-driven) mode, registers a TaskListener that will get called after FLOW. In externally-driven mode (see externallyDriven), no TaskListener is registered; the host drives the type-processing lifecycle instead.

      The shouldStopPolicy is bumped to at least FLOW in 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:
      init in interface Processor
      Overrides:
      init in class AbstractProcessor
    • process

      public final boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv)
      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:
      process in interface Processor
      Specified by:
      process in class AbstractProcessor
    • 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

      public abstract void typeProcess(TypeElement element, TreePath tree)
      Processes a fully-analyzed class that contains a supported annotation (see AbstractProcessor.getSupportedAnnotationTypes()).

      The passed class is always valid type-checked Java code.

      Parameters:
      element - element of the analyzed class
      tree - the tree path to the element, with the leaf being a ClassTree
    • packageProcess

      public void packageProcess(PackageElement element, TreePath tree)
      Processes a fully-analyzed package declaration, that is, the package clause of a package-info.java in this compilation.

      The @Target of an annotation may include ElementKind.PACKAGE, so a declaration annotation and its contradictions can appear here exactly as they can on a class. A package-info.java declares no type, however, so typeProcess(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 package
      tree - the tree path to the package declaration, with the leaf being a PackageTree
    • handleProcessingError

      protected void handleProcessingError(String methodName, Throwable t)
      Handles a throwable thrown by typeProcessingOver(). 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 threw t
      t - the throwable that methodName threw
    • 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

      public final void typeProcessExternally(TypeElement element, TreePath tree)
      Drives a single typeProcess(TypeElement, TreePath) invocation on behalf of an external host (see externallyDriven), handling the once-only typeProcessingStart() bracketing.

      Intended for hosts that own the compilation TaskListener (e.g. an Error Prone plugin) and therefore cannot rely on this processor's own AbstractTypeProcessor.AttributionTaskListener. The host must call typeProcessingOverExternally() once, after the last class has been processed.

      Unlike the self-driven path, this method does not consult the elements set (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 class
      tree - the tree path to the element, with the leaf being a ClassTree
    • packageProcessExternally

      public final void packageProcessExternally(PackageElement element, TreePath tree)
      Processes a package declaration on behalf of an external host (see externallyDriven), handling the once-only typeProcessingStart() invocation.

      The counterpart of typeProcessExternally(javax.lang.model.element.TypeElement, com.sun.source.util.TreePath) for a package-info.java. A host needs it because the TaskListener that would otherwise dispatch the package declaration is not registered in externally-driven mode.

      Parameters:
      element - the package being processed
      tree - the path to the package declaration, with the leaf being a PackageTree
    • typeProcessingOverExternally

      public final void typeProcessingOverExternally()
      Signals, on behalf of an external host (see externallyDriven), that all classes have been processed. Invokes typeProcessingOver() exactly once. Also invokes typeProcessingStart() first if it has not yet run (e.g. if no classes were processed).
      See Also:
    • getCompilerLog

      @SideEffectFree public com.sun.tools.javac.util.Log getCompilerLog()
      Return the compiler log, which contains errors and warnings.
      Returns:
      the compiler log, which contains errors and warnings