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
Base processor class providing common AST manipulation utilities.

Handles low-level Javac API access and provides:

  • Field/Method existence checks
  • Qualified type name construction
  • Custom javac environment unwrapping
  • Logging facilities
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected boolean
     
    protected com.sun.tools.javac.tree.TreeMaker
    Factory for creating Javac AST nodes (JCTree).
    protected Messager
    Interface for reporting errors, warnings, and other notices during annotation processing.
    protected com.sun.tools.javac.util.Names
    Factory for creating Javac Name objects (identifiers).
    protected Trees
    Utility for working with Javac ASTs (JCTree).

    Fields inherited from class javax.annotation.processing.AbstractProcessor

    processingEnv
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    addToStaticInitializer(@NotNull com.sun.tools.javac.tree.JCTree.JCClassDecl classDecl, com.sun.tools.javac.tree.JCTree.JCStatement stmt)
     
    protected @NotNull String
    capitalize(@NotNull String str)
    Capitalizes the first character of the input string.
    protected com.sun.tools.javac.tree.JCTree.JCExpression
    createQualifiedName(int pos, @NotNull String fullTypeName)
    Creates a Javac AST expression (JCTree.JCExpression) representing a fully qualified name (e.g., java.util.List).
    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.
    protected @Nullable com.sun.tools.javac.tree.JCTree.JCExpression
    findFieldType(@NotNull com.sun.tools.javac.tree.JCTree.JCClassDecl classDecl, String fieldName)
    Finds the field type of a given field name in a class declaration.
    protected com.sun.tools.javac.tree.JCTree.JCExpression
    findMethodReturnType(@NotNull Element element, String methodName, int pos)
    Finds the return type of a given method name in the given class declaration.
    protected @NotNull com.sun.tools.javac.tree.JCTree.JCExpression
    getBoxedType(@NotNull com.sun.tools.javac.tree.JCTree.JCExpression primitiveOrRefType, int pos)
    Returns the boxed type expression for a given primitive type expression.
    protected com.sun.tools.javac.processing.JavacProcessingEnvironment
    Attempts to retrieve the underlying JavacProcessingEnvironment from a potentially wrapped processing environment object.
    Specifies the latest supported Java source version.
    void
    Initializes the processor, setting up common utilities like Messager, TreeMaker, Trees, and Names.
    protected void
    log(String msg)
    Logs an informational message using the processing environment's Messager.
    protected void
    Logs an error message using the processing environment's Messager.
    protected void
    Logs a warning message using the processing environment's Messager.
    protected boolean
    methodExists(@NotNull com.sun.tools.javac.tree.JCTree.JCClassDecl classDecl, String methodName, int paramCount)
    Checks method existence with parameter count constraint
    protected void
    printCompilationUnitAst(com.sun.tools.javac.tree.JCTree.JCCompilationUnit compilationUnit)
    Prints the Abstract Syntax Tree (AST) of the given compilation unit to the log.

    Methods inherited from class javax.annotation.processing.AbstractProcessor

    getCompletions, getSupportedAnnotationTypes, getSupportedOptions, isInitialized, process

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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).
    • isVerbose

      protected boolean isVerbose
  • 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:
    • fieldExists

      protected boolean fieldExists(@NotNull @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 @NotNull com.sun.tools.javac.tree.JCTree.JCClassDecl classDecl, String methodName, int paramCount)
      Checks method existence with parameter count constraint
      Parameters:
      classDecl - The class declaration to check
      methodName - Target method name
      paramCount - Expected number of parameters
      Returns:
      true if method exists with specified name and parameter count
    • createQualifiedName

      protected com.sun.tools.javac.tree.JCTree.JCExpression createQualifiedName(int pos, @NotNull @NotNull String fullTypeName)
      Creates a Javac AST expression (JCTree.JCExpression) representing a fully qualified name (e.g., java.util.List).
      Parameters:
      fullTypeName - 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.
    • findFieldType

      @Nullable protected @Nullable com.sun.tools.javac.tree.JCTree.JCExpression findFieldType(@NotNull @NotNull com.sun.tools.javac.tree.JCTree.JCClassDecl classDecl, String fieldName)
      Finds the field type of a given field name in a class declaration.
      Parameters:
      classDecl - The class declaration in which to search for the field.
      fieldName - The name of the field whose type is to be found.
      Returns:
      The type of the field if found, otherwise null.
    • findMethodReturnType

      protected com.sun.tools.javac.tree.JCTree.JCExpression findMethodReturnType(@NotNull @NotNull Element element, String methodName, int pos)
      Finds the return type of a given method name in the given class declaration.
      Parameters:
      element - The class declaration in which to search for the method.
      methodName - The name of the method whose return type is to be found.
      pos - The maker position
      Returns:
      The type of the method return if found, otherwise null.
    • addToStaticInitializer

      protected void addToStaticInitializer(@NotNull @NotNull com.sun.tools.javac.tree.JCTree.JCClassDecl classDecl, com.sun.tools.javac.tree.JCTree.JCStatement stmt)
    • capitalize

      @NotNull protected @NotNull String capitalize(@NotNull @NotNull String str)
      Capitalizes the first character of the input string.
      Parameters:
      str - The string to be capitalized.
      Returns:
      The capitalized string.
    • getBoxedType

      @NotNull protected @NotNull com.sun.tools.javac.tree.JCTree.JCExpression getBoxedType(@NotNull @NotNull com.sun.tools.javac.tree.JCTree.JCExpression primitiveOrRefType, int pos)
      Returns the boxed type expression for a given primitive type expression. If the input type is not a primitive type, it is returned unchanged.
      Parameters:
      primitiveOrRefType - The JCTree.JCExpression representing the type.
      Returns:
      The JCTree.JCExpression for the boxed type or the original type if not primitive.
    • getJavacProcessingEnvironment

      protected com.sun.tools.javac.processing.JavacProcessingEnvironment getJavacProcessingEnvironment(Object procEnv)
      Attempts to retrieve the underlying JavacProcessingEnvironment from a potentially wrapped processing environment object. This method handles various wrapping strategies used by different tools and environments, such as Gradle and IntelliJ IDEA.

      If the input object is already an instance of JavacProcessingEnvironment, it is returned directly. Otherwise, the method attempts to unwrap it by searching for delegate fields recursively until the desired environment is found.

      Parameters:
      procEnv - The processing environment object, potentially wrapped.
      Returns:
      The unwrapped JavacProcessingEnvironment if found, otherwise null.
    • printCompilationUnitAst

      protected void printCompilationUnitAst(com.sun.tools.javac.tree.JCTree.JCCompilationUnit compilationUnit)
      Prints the Abstract Syntax Tree (AST) of the given compilation unit to the log.

      This method uses the Pretty printer to generate a textual representation of the AST for debugging or analysis purposes. If an error occurs during printing, an error message is logged along with the stack trace.

      Parameters:
      compilationUnit - the JCTree.JCCompilationUnit representing the compilation unit whose AST is to be printed; may be null
      See Also:
      • Pretty
      • JCTree.JCCompilationUnit
    • 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.