Class CommonProcessor

java.lang.Object
javax.annotation.processing.AbstractProcessor
host.anzo.commons.processors.CommonProcessor
All Implemented Interfaces:
Processor
Direct Known Subclasses:
ExtendedEnumProcessor

public abstract class CommonProcessor extends AbstractProcessor
Abstract base class for annotation processors providing common utilities and initialization logic, especially for accessing Javac-specific APIs like TreeMaker, Trees, and Names. It also includes handling for potential JetBrains IDE-specific wrappers around the ProcessingEnvironment.
  • Field Details

    • maker

      protected com.sun.tools.javac.tree.TreeMaker maker
      Factory for creating Javac AST nodes (JCTree). Initialized in init(ProcessingEnvironment).
    • trees

      protected Trees trees
      Utility for working with Javac ASTs (JCTree). Initialized in init(ProcessingEnvironment). Provides access to source tree structures.
    • messager

      protected Messager messager
      Interface for reporting errors, warnings, and other notices during annotation processing. Obtained from the ProcessingEnvironment.
    • names

      protected com.sun.tools.javac.util.Names names
      Factory for creating Javac Name objects (identifiers). Initialized in init(ProcessingEnvironment).
  • Constructor Details

    • CommonProcessor

      public CommonProcessor()
  • Method Details

    • getSupportedSourceVersion

      public SourceVersion getSupportedSourceVersion()
      Specifies the latest supported Java source version.
      Specified by:
      getSupportedSourceVersion in interface Processor
      Overrides:
      getSupportedSourceVersion in class AbstractProcessor
      Returns:
      The latest SourceVersion.
    • init

      public void init(ProcessingEnvironment procEnv)
      Initializes the processor, setting up common utilities like Messager, TreeMaker, Trees, and Names.

      This implementation attempts to unwrap the ProcessingEnvironment if it's potentially wrapped by JetBrains tools (like in IntelliJ IDEA) to ensure access to the underlying JavacProcessingEnvironment. If the unwrapping fails or the environment is not a JavacProcessingEnvironment, an error is logged, and the Javac-specific tools (maker, trees, names) will remain null.

      Specified by:
      init in interface Processor
      Overrides:
      init in class AbstractProcessor
      Parameters:
      procEnv - Environment providing access to facilities like Messager, Filer, and Elements.
      See Also:
      • tryUnwrapJetBrainsProcEnv(Object)
    • fieldExists

      protected boolean fieldExists(@NotNull com.sun.tools.javac.tree.JCTree.JCClassDecl classDecl, String fieldName)
      Checks if a field with the specified name already exists within the given class declaration.
      Parameters:
      classDecl - The Javac AST node representing the class declaration.
      fieldName - The name of the field to check for.
      Returns:
      true if a field with the given name exists, false otherwise.
    • methodExists

      protected boolean methodExists(@NotNull com.sun.tools.javac.tree.JCTree.JCClassDecl classDecl, String methodName)
      Checks if a method with the specified name already exists within the given class declaration. Note: This performs a simple name check and does not consider method parameters (overloads).
      Parameters:
      classDecl - The Javac AST node representing the class declaration.
      methodName - The name of the method to check for.
      Returns:
      true if a method with the given name exists, false otherwise.
    • createQualifiedName

      protected com.sun.tools.javac.tree.JCTree.JCExpression createQualifiedName(@NotNull @NotNull String fullName)
      Creates a Javac AST expression (JCTree.JCExpression) representing a fully qualified name (e.g., java.util.List).
      Parameters:
      fullName - The fully qualified name as a String (e.g., "java.lang.String").
      Returns:
      A JCTree.JCExpression representing the qualified name, suitable for use in AST generation. This will typically be a JCTree.JCIdent for single-part names or a JCTree.JCFieldAccess (Select) for multi-part names.
    • log

      protected void log(String msg)
      Logs an informational message using the processing environment's Messager. The message is prefixed with the simple name of this processor class.
      Parameters:
      msg - The message to log.
    • logError

      protected void logError(String msg)
      Logs an error message using the processing environment's Messager. The message is prefixed with the simple name of this processor class. Note that reporting an error typically halts the compilation process.
      Parameters:
      msg - The error message to log.
    • logWarn

      protected void logWarn(String msg)
      Logs a warning message using the processing environment's Messager. The message is prefixed with the simple name of this processor class.
      Parameters:
      msg - The warning message to log.