Class AbstractCommandSpecProcessor
- java.lang.Object
-
- javax.annotation.processing.AbstractProcessor
-
- picocli.codegen.annotation.processing.AbstractCommandSpecProcessor
-
- All Implemented Interfaces:
Processor
- Direct Known Subclasses:
NativeImageConfigGeneratorProcessor
public abstract class AbstractCommandSpecProcessor extends AbstractProcessor
Abstract annotation processor for@picocli.*annotations that produces a set ofCommandLine.Model.CommandSpecobjects built from the annotated source code.Subclasses should override the
handleCommands(Map, Set, RoundEnvironment)method to do something useful with theseCommandSpecobjects, like generating source code, documentation or configuration files.Note that due to the limitations of annotation processing and the compiler API, annotation attributes of type
Classare not available asClassvalues at compile time, but only asTypeMirrorvalues. Picocli 4.0 introduces a newCommandLine.Model.ITypeInfointerface that providesArgSpectype metadata that can be used both at compile time and at runtime.Similarly,
ArgSpecobjects constructed by the annotation processor will have aCommandLine.Model.IGetterandCommandLine.Model.ISetterimplementation that is different from the one used at runtime and cannot be invoked directly: the annotation processor will assign anAnnotatedElementHolderimplementation that gives subclass annotation processors access to the annotated element.CommandSpecobjects constructed by the annotation processor will have anVersionProviderMetaDataversion provider and aDefaultValueProviderMetaDatadefault value provider, which gives subclass annotation processors access to theTypeMirrorof the version provider and default value provider specified in the annotation.- Since:
- 4.0
-
-
Field Summary
Fields Modifier and Type Field Description protected ProcessingEnvironmentprocessingEnvThe ProcessingEnvironment set by theinit(ProcessingEnvironment)method.
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractCommandSpecProcessor()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected voiderror(Element element, String msg, Object... args)Prints a compile-time error message for the specified element.protected voidfatalError(String msg)Prints a compile-time error message prefixed with "FATAL ERROR".Set<String>getSupportedAnnotationTypes()Returns the annotation types supported by the super class, and adds"picocli.*"if necessary.SourceVersiongetSupportedSourceVersion()Returns the max supported source version.protected abstract booleanhandleCommands(Map<Element,CommandLine.Model.CommandSpec> commands, Set<? extends TypeElement> annotations, RoundEnvironment roundEnv)Subclasses must implement this method and do something with theCommandSpeccommand model objects that were found during compilation.voidinit(ProcessingEnvironment processingEnv)protected voidlogInfo(String msg)Prints a compile-time NOTE message.booleanprocess(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv)static voidsetLoadResourceBundles(boolean loadBundles)During annotation processing, resource bundles may not be available on the classpath and thereby cause failures.protected voidwarn(Element element, String msg, Object... args)Prints a compile-time warning message for the specified element.-
Methods inherited from class javax.annotation.processing.AbstractProcessor
getCompletions, getSupportedOptions, isInitialized
-
-
-
-
Field Detail
-
processingEnv
protected ProcessingEnvironment processingEnv
The ProcessingEnvironment set by theinit(ProcessingEnvironment)method.
-
-
Method Detail
-
getSupportedAnnotationTypes
public Set<String> getSupportedAnnotationTypes()
Returns the annotation types supported by the super class, and adds"picocli.*"if necessary. Subclasses can omit the@SupportedAnnotationTypes("picocli.*")annotation, but add other annotations if desired.- Specified by:
getSupportedAnnotationTypesin interfaceProcessor- Overrides:
getSupportedAnnotationTypesin classAbstractProcessor- Returns:
- the set of supported annotation types, with at least
"picocli.*"
-
getSupportedSourceVersion
public SourceVersion getSupportedSourceVersion()
Returns the max supported source version. ReturnsSourceVersion.latest()by default, subclasses may override or may use theSupportedSourceVersionannotation.- Specified by:
getSupportedSourceVersionin interfaceProcessor- Overrides:
getSupportedSourceVersionin classAbstractProcessor- Returns:
- the max supported source version
-
init
public void init(ProcessingEnvironment processingEnv)
- Specified by:
initin interfaceProcessor- Overrides:
initin classAbstractProcessor
-
process
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv)
- Specified by:
processin interfaceProcessor- Specified by:
processin classAbstractProcessor
-
setLoadResourceBundles
public static final void setLoadResourceBundles(boolean loadBundles)
During annotation processing, resource bundles may not be available on the classpath and thereby cause failures. For that reason, by default, resource bundles are not loaded during annotation processing. This method allows for enabling loading of resource bundles during annotation processing.- Parameters:
loadBundles- true if bundles should be loaded, false (default) if bundles should not be loaded- Since:
- 4.8.0
-
handleCommands
protected abstract boolean handleCommands(Map<Element,CommandLine.Model.CommandSpec> commands, Set<? extends TypeElement> annotations, RoundEnvironment roundEnv)
Subclasses must implement this method and do something with theCommandSpeccommand model objects that were found during compilation.- Parameters:
commands- a map of annotated elements to their associatedCommandSpec. Note that the key set may contain classes that do not have a@Commandannotation but were added to the map because the class has fields annotated withOptionor@Parameters.annotations- the annotation types requested to be processedroundEnv- environment for information about the current and prior round- Returns:
- whether or not the set of annotation types are claimed by this processor.
If
trueis returned, the annotation types are claimed and subsequent processors will not be asked to process them; iffalseis returned, the annotation types are unclaimed and subsequent processors may be asked to process them. A processor may always return the same boolean value or may vary the result based on chosen criteria.
-
logInfo
protected void logInfo(String msg)
Prints a compile-time NOTE message.- Parameters:
msg- the info message
-
error
protected void error(Element element, String msg, Object... args)
Prints a compile-time error message for the specified element.- Parameters:
element- the problematic elementmsg- the error message with optional format specifiersargs- the arguments to use to callString.formaton the error message
-
warn
protected void warn(Element element, String msg, Object... args)
Prints a compile-time warning message for the specified element.- Parameters:
element- the problematic element, may benullmsg- the warning message with optional format specifiersargs- the arguments to use to callString.formaton the warning message
-
fatalError
protected void fatalError(String msg)
Prints a compile-time error message prefixed with "FATAL ERROR".- Parameters:
msg- the error message with optional format specifiers
-
-