Package host.anzo.commons.processors
Class CommonProcessor
java.lang.Object
javax.annotation.processing.AbstractProcessor
host.anzo.commons.processors.CommonProcessor
- All Implemented Interfaces:
Processor
- Direct Known Subclasses:
ExtendedEnumProcessor
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 Summary
FieldsModifier and TypeFieldDescriptionprotected com.sun.tools.javac.tree.TreeMakerFactory for creating Javac AST nodes (JCTree).protected MessagerInterface for reporting errors, warnings, and other notices during annotation processing.protected com.sun.tools.javac.util.NamesFactory for creating JavacNameobjects (identifiers).protected TreesUtility for working with Javac ASTs (JCTree).Fields inherited from class javax.annotation.processing.AbstractProcessor
processingEnv -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected com.sun.tools.javac.tree.JCTree.JCExpressioncreateQualifiedName(@NotNull String fullName) Creates a Javac AST expression (JCTree.JCExpression) representing a fully qualified name (e.g.,java.util.List).protected booleanfieldExists(@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.Specifies the latest supported Java source version.voidinit(ProcessingEnvironment procEnv) protected voidLogs an informational message using the processing environment'sMessager.protected voidLogs an error message using the processing environment'sMessager.protected voidLogs a warning message using the processing environment'sMessager.protected booleanmethodExists(@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.Methods inherited from class javax.annotation.processing.AbstractProcessor
getCompletions, getSupportedAnnotationTypes, getSupportedOptions, isInitialized, process
-
Field Details
-
maker
protected com.sun.tools.javac.tree.TreeMaker makerFactory for creating Javac AST nodes (JCTree). Initialized ininit(ProcessingEnvironment). -
trees
Utility for working with Javac ASTs (JCTree). Initialized ininit(ProcessingEnvironment). Provides access to source tree structures. -
messager
Interface for reporting errors, warnings, and other notices during annotation processing. Obtained from theProcessingEnvironment. -
names
protected com.sun.tools.javac.util.Names namesFactory for creating JavacNameobjects (identifiers). Initialized ininit(ProcessingEnvironment).
-
-
Constructor Details
-
CommonProcessor
public CommonProcessor()
-
-
Method Details
-
getSupportedSourceVersion
Specifies the latest supported Java source version.- Specified by:
getSupportedSourceVersionin interfaceProcessor- Overrides:
getSupportedSourceVersionin classAbstractProcessor- Returns:
- The latest
SourceVersion.
-
init
Initializes the processor, setting up common utilities likeMessager,TreeMaker,Trees, andNames.This implementation attempts to unwrap the
ProcessingEnvironmentif it's potentially wrapped by JetBrains tools (like in IntelliJ IDEA) to ensure access to the underlyingJavacProcessingEnvironment. If the unwrapping fails or the environment is not aJavacProcessingEnvironment, an error is logged, and the Javac-specific tools (maker,trees,names) will remain null. -
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:
trueif a field with the given name exists,falseotherwise.
-
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:
trueif a method with the given name exists,falseotherwise.
-
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.JCExpressionrepresenting the qualified name, suitable for use in AST generation. This will typically be aJCTree.JCIdentfor single-part names or aJCTree.JCFieldAccess(Select) for multi-part names.
-
log
Logs an informational message using the processing environment'sMessager. The message is prefixed with the simple name of this processor class.- Parameters:
msg- The message to log.
-
logError
Logs an error message using the processing environment'sMessager. 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
Logs a warning message using the processing environment'sMessager. The message is prefixed with the simple name of this processor class.- Parameters:
msg- The warning message to log.
-