Class AbstractCommandSpecProcessor
- java.lang.Object
-
- javax.annotation.processing.AbstractProcessor
-
- picocli.codegen.annotation.processing.AbstractCommandSpecProcessor
-
- All Implemented Interfaces:
javax.annotation.processing.Processor
- Direct Known Subclasses:
AbstractCompositeGeneratorProcessor
public abstract class AbstractCommandSpecProcessor extends javax.annotation.processing.AbstractProcessor
Abstract annotation processor for@picocli.*
annotations that produces a set ofCommandLine.Model.CommandSpec
objects built from the annotated source code.Subclasses should override the
handleCommands(Map, Set, RoundEnvironment)
method to do something useful with theseCommandSpec
objects, 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
Class
are not available asClass
values at compile time, but only asTypeMirror
values. Picocli 4.0 introduces a newCommandLine.Model.ITypeInfo
interface that providesArgSpec
type metadata that can be used both at compile time and at runtime.Similarly,
ArgSpec
objects constructed by the annotation processor will have aCommandLine.Model.IGetter
andCommandLine.Model.ISetter
implementation that is different from the one used at runtime and cannot be invoked directly: the annotation processor will assign anAnnotatedElementHolder
implementation that gives subclass annotation processors access to the annotated element.CommandSpec
objects constructed by the annotation processor will have anVersionProviderMetaData
version provider and aDefaultValueProviderMetaData
default value provider, which gives subclass annotation processors access to theTypeMirror
of the version provider and default value provider specified in the annotation.- Since:
- 4.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
AbstractCommandSpecProcessor.Context
(package private) static class
AbstractCommandSpecProcessor.Graph
Helper class for topologically sorting ArgGroups.(package private) static class
AbstractCommandSpecProcessor.NullFactory
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.String
COMMAND_DEFAULT_NAME
private static java.lang.String
COMMAND_TYPE
(package private) static java.util.logging.ConsoleHandler
handler
private static java.util.logging.Logger
logger
protected javax.annotation.processing.ProcessingEnvironment
processingEnv
The ProcessingEnvironment set by theinit(ProcessingEnvironment)
method.
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractCommandSpecProcessor()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description private void
buildArgGroup(javax.lang.model.element.Element element, AbstractCommandSpecProcessor.Context context, javax.annotation.processing.RoundEnvironment roundEnv)
private void
buildArgGroups(javax.annotation.processing.RoundEnvironment roundEnv, AbstractCommandSpecProcessor.Context context)
private CommandLine.Model.CommandSpec
buildCommand(javax.lang.model.element.Element element, AbstractCommandSpecProcessor.Context context, javax.annotation.processing.RoundEnvironment roundEnv)
private void
buildCommands(javax.annotation.processing.RoundEnvironment roundEnv, AbstractCommandSpecProcessor.Context context)
private void
buildMixin(javax.lang.model.element.Element element, javax.annotation.processing.RoundEnvironment roundEnv, AbstractCommandSpecProcessor.Context context)
private void
buildMixins(javax.annotation.processing.RoundEnvironment roundEnv, AbstractCommandSpecProcessor.Context context)
private void
buildOption(javax.lang.model.element.Element element, AbstractCommandSpecProcessor.Context context)
private void
buildOptions(javax.annotation.processing.RoundEnvironment roundEnv, AbstractCommandSpecProcessor.Context context)
private void
buildOptionsAndPositionalsFromMethodParameters(javax.lang.model.element.ExecutableElement method, CommandLine.Model.CommandSpec result, AbstractCommandSpecProcessor.Context context)
private void
buildParameter(javax.lang.model.element.Element element, AbstractCommandSpecProcessor.Context context)
private void
buildParameters(javax.annotation.processing.RoundEnvironment roundEnv, AbstractCommandSpecProcessor.Context context)
private void
buildParentCommand(javax.lang.model.element.Element element, AbstractCommandSpecProcessor.Context context)
private void
buildParentCommands(javax.annotation.processing.RoundEnvironment roundEnv, AbstractCommandSpecProcessor.Context context)
private void
buildSpec(javax.lang.model.element.Element element, AbstractCommandSpecProcessor.Context context)
private void
buildSpecs(javax.annotation.processing.RoundEnvironment roundEnv, AbstractCommandSpecProcessor.Context context)
private CommandLine.Model.IAnnotatedElement
buildTypedMember(javax.lang.model.element.Element element)
private java.util.Stack<javax.lang.model.element.TypeElement>
buildTypeHierarchy(javax.lang.model.element.TypeElement typeElement)
private void
buildUnmatched(javax.annotation.processing.RoundEnvironment roundEnv, AbstractCommandSpecProcessor.Context context)
private void
buildUnmatched(javax.lang.model.element.Element element, AbstractCommandSpecProcessor.Context context)
private void
debugElement(javax.lang.model.element.Element element, java.lang.String s)
private void
debugFoundAnnotations(java.util.Set<? extends javax.lang.model.element.TypeElement> annotations, javax.annotation.processing.RoundEnvironment roundEnv)
private void
debugMethod(javax.lang.model.element.ExecutableElement method)
private void
debugType(javax.lang.model.type.TypeMirror typeMirror, javax.lang.model.element.Element element, java.lang.String indent)
protected void
error(javax.lang.model.element.Element element, java.lang.String msg, java.lang.Object... args)
Prints a compile-time error message for the specified element.private TypedMember
extractTypedMember(javax.lang.model.element.Element element, java.lang.String annotation)
protected void
fatalError(java.lang.String msg)
Prints a compile-time error message prefixed with "FATAL ERROR".private java.util.List<CommandLine.Model.CommandSpec>
findSubcommands(java.util.List<? extends javax.lang.model.element.AnnotationMirror> annotationMirrors, AbstractCommandSpecProcessor.Context context, javax.annotation.processing.RoundEnvironment roundEnv)
java.util.Set<java.lang.String>
getSupportedAnnotationTypes()
Returns the annotation types supported by the super class, and adds"picocli.*"
if necessary.javax.lang.model.SourceVersion
getSupportedSourceVersion()
Returns the max supported source version.protected abstract boolean
handleCommands(java.util.Map<javax.lang.model.element.Element,CommandLine.Model.CommandSpec> commands, java.util.Set<? extends javax.lang.model.element.TypeElement> annotations, javax.annotation.processing.RoundEnvironment roundEnv)
Subclasses must implement this method and do something with theCommandSpec
command model objects that were found during compilation.void
init(javax.annotation.processing.ProcessingEnvironment processingEnv)
private boolean
isSubcommand(javax.lang.model.element.ExecutableElement method, javax.annotation.processing.RoundEnvironment roundEnv)
private boolean
isValidSubcommandHasNameAttribute(javax.lang.model.element.Element subcommandElement)
private void
logElementDetails(javax.lang.model.element.Element element, java.lang.String s)
protected void
logInfo(java.lang.String msg)
Prints a compile-time NOTE message.boolean
process(java.util.Set<? extends javax.lang.model.element.TypeElement> annotations, javax.annotation.processing.RoundEnvironment roundEnv)
private void
processEnclosedElements(AbstractCommandSpecProcessor.Context context, javax.annotation.processing.RoundEnvironment roundEnv, java.util.List<? extends javax.lang.model.element.Element> enclosedElements)
private void
registerSubcommands(java.util.List<javax.lang.model.element.AnnotationValue> typeMirrors, java.util.List<CommandLine.Model.CommandSpec> result, AbstractCommandSpecProcessor.Context context, javax.annotation.processing.RoundEnvironment roundEnv)
private static java.lang.String
stacktrace(java.lang.Exception e)
private static javax.lang.model.element.TypeElement
superClassFor(javax.lang.model.element.TypeElement element)
Obtains the super type element for a given type element.private boolean
tryProcess(java.util.Set<? extends javax.lang.model.element.TypeElement> annotations, javax.annotation.processing.RoundEnvironment roundEnv)
private void
updateCommandAttributes(CommandLine.Model.CommandSpec result, CommandLine.Command cmd)
private void
updateCommandFromMethodElement(javax.lang.model.element.ExecutableElement method, AbstractCommandSpecProcessor.Context context, CommandLine.Model.CommandSpec result, javax.annotation.processing.RoundEnvironment roundEnv)
private void
updateCommandSpecFromCommandAnnotation(CommandLine.Model.CommandSpec result, javax.lang.model.element.Element element, AbstractCommandSpecProcessor.Context context, javax.annotation.processing.RoundEnvironment roundEnv)
private void
updateCommandSpecFromTypeElement(javax.lang.model.element.TypeElement typeElement, AbstractCommandSpecProcessor.Context context, CommandLine.Model.CommandSpec result, javax.annotation.processing.RoundEnvironment roundEnv)
protected void
warn(javax.lang.model.element.Element element, java.lang.String msg, java.lang.Object... args)
Prints a compile-time warning message for the specified element.
-
-
-
Field Detail
-
COMMAND_DEFAULT_NAME
private static final java.lang.String COMMAND_DEFAULT_NAME
- See Also:
- Constant Field Values
-
logger
private static java.util.logging.Logger logger
-
processingEnv
protected javax.annotation.processing.ProcessingEnvironment processingEnv
The ProcessingEnvironment set by theinit(ProcessingEnvironment)
method.
-
COMMAND_TYPE
private static final java.lang.String COMMAND_TYPE
-
handler
static java.util.logging.ConsoleHandler handler
-
-
Method Detail
-
getSupportedAnnotationTypes
public java.util.Set<java.lang.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:
getSupportedAnnotationTypes
in interfacejavax.annotation.processing.Processor
- Overrides:
getSupportedAnnotationTypes
in classjavax.annotation.processing.AbstractProcessor
- Returns:
- the set of supported annotation types, with at least
"picocli.*"
-
getSupportedSourceVersion
public javax.lang.model.SourceVersion getSupportedSourceVersion()
Returns the max supported source version. ReturnsSourceVersion.latest()
by default, subclasses may override or may use theSupportedSourceVersion
annotation.- Specified by:
getSupportedSourceVersion
in interfacejavax.annotation.processing.Processor
- Overrides:
getSupportedSourceVersion
in classjavax.annotation.processing.AbstractProcessor
- Returns:
- the max supported source version
-
init
public void init(javax.annotation.processing.ProcessingEnvironment processingEnv)
- Specified by:
init
in interfacejavax.annotation.processing.Processor
- Overrides:
init
in classjavax.annotation.processing.AbstractProcessor
-
process
public boolean process(java.util.Set<? extends javax.lang.model.element.TypeElement> annotations, javax.annotation.processing.RoundEnvironment roundEnv)
- Specified by:
process
in interfacejavax.annotation.processing.Processor
- Specified by:
process
in classjavax.annotation.processing.AbstractProcessor
-
stacktrace
private static java.lang.String stacktrace(java.lang.Exception e)
-
tryProcess
private boolean tryProcess(java.util.Set<? extends javax.lang.model.element.TypeElement> annotations, javax.annotation.processing.RoundEnvironment roundEnv)
-
handleCommands
protected abstract boolean handleCommands(java.util.Map<javax.lang.model.element.Element,CommandLine.Model.CommandSpec> commands, java.util.Set<? extends javax.lang.model.element.TypeElement> annotations, javax.annotation.processing.RoundEnvironment roundEnv)
Subclasses must implement this method and do something with theCommandSpec
command 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@Command
annotation but were added to the map because the class has fields annotated withOption
or@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
true
is returned, the annotation types are claimed and subsequent processors will not be asked to process them; iffalse
is 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.
-
buildCommands
private void buildCommands(javax.annotation.processing.RoundEnvironment roundEnv, AbstractCommandSpecProcessor.Context context)
-
buildCommand
private CommandLine.Model.CommandSpec buildCommand(javax.lang.model.element.Element element, AbstractCommandSpecProcessor.Context context, javax.annotation.processing.RoundEnvironment roundEnv)
-
updateCommandSpecFromTypeElement
private void updateCommandSpecFromTypeElement(javax.lang.model.element.TypeElement typeElement, AbstractCommandSpecProcessor.Context context, CommandLine.Model.CommandSpec result, javax.annotation.processing.RoundEnvironment roundEnv)
-
updateCommandFromMethodElement
private void updateCommandFromMethodElement(javax.lang.model.element.ExecutableElement method, AbstractCommandSpecProcessor.Context context, CommandLine.Model.CommandSpec result, javax.annotation.processing.RoundEnvironment roundEnv)
-
isSubcommand
private boolean isSubcommand(javax.lang.model.element.ExecutableElement method, javax.annotation.processing.RoundEnvironment roundEnv)
-
buildTypeHierarchy
private java.util.Stack<javax.lang.model.element.TypeElement> buildTypeHierarchy(javax.lang.model.element.TypeElement typeElement)
-
updateCommandSpecFromCommandAnnotation
private void updateCommandSpecFromCommandAnnotation(CommandLine.Model.CommandSpec result, javax.lang.model.element.Element element, AbstractCommandSpecProcessor.Context context, javax.annotation.processing.RoundEnvironment roundEnv)
-
updateCommandAttributes
private void updateCommandAttributes(CommandLine.Model.CommandSpec result, CommandLine.Command cmd)
-
findSubcommands
private java.util.List<CommandLine.Model.CommandSpec> findSubcommands(java.util.List<? extends javax.lang.model.element.AnnotationMirror> annotationMirrors, AbstractCommandSpecProcessor.Context context, javax.annotation.processing.RoundEnvironment roundEnv)
-
registerSubcommands
private void registerSubcommands(java.util.List<javax.lang.model.element.AnnotationValue> typeMirrors, java.util.List<CommandLine.Model.CommandSpec> result, AbstractCommandSpecProcessor.Context context, javax.annotation.processing.RoundEnvironment roundEnv)
-
processEnclosedElements
private void processEnclosedElements(AbstractCommandSpecProcessor.Context context, javax.annotation.processing.RoundEnvironment roundEnv, java.util.List<? extends javax.lang.model.element.Element> enclosedElements)
-
isValidSubcommandHasNameAttribute
private boolean isValidSubcommandHasNameAttribute(javax.lang.model.element.Element subcommandElement)
-
buildMixins
private void buildMixins(javax.annotation.processing.RoundEnvironment roundEnv, AbstractCommandSpecProcessor.Context context)
-
buildMixin
private void buildMixin(javax.lang.model.element.Element element, javax.annotation.processing.RoundEnvironment roundEnv, AbstractCommandSpecProcessor.Context context)
-
buildArgGroups
private void buildArgGroups(javax.annotation.processing.RoundEnvironment roundEnv, AbstractCommandSpecProcessor.Context context)
-
buildArgGroup
private void buildArgGroup(javax.lang.model.element.Element element, AbstractCommandSpecProcessor.Context context, javax.annotation.processing.RoundEnvironment roundEnv)
-
buildOptions
private void buildOptions(javax.annotation.processing.RoundEnvironment roundEnv, AbstractCommandSpecProcessor.Context context)
-
buildOption
private void buildOption(javax.lang.model.element.Element element, AbstractCommandSpecProcessor.Context context)
-
buildParameters
private void buildParameters(javax.annotation.processing.RoundEnvironment roundEnv, AbstractCommandSpecProcessor.Context context)
-
buildParameter
private void buildParameter(javax.lang.model.element.Element element, AbstractCommandSpecProcessor.Context context)
-
extractTypedMember
private TypedMember extractTypedMember(javax.lang.model.element.Element element, java.lang.String annotation)
-
buildOptionsAndPositionalsFromMethodParameters
private void buildOptionsAndPositionalsFromMethodParameters(javax.lang.model.element.ExecutableElement method, CommandLine.Model.CommandSpec result, AbstractCommandSpecProcessor.Context context)
-
superClassFor
private static javax.lang.model.element.TypeElement superClassFor(javax.lang.model.element.TypeElement element)
Obtains the super type element for a given type element.- Parameters:
element
- The type element- Returns:
- The super type element or null if none exists
-
buildUnmatched
private void buildUnmatched(javax.annotation.processing.RoundEnvironment roundEnv, AbstractCommandSpecProcessor.Context context)
-
buildUnmatched
private void buildUnmatched(javax.lang.model.element.Element element, AbstractCommandSpecProcessor.Context context)
-
buildSpecs
private void buildSpecs(javax.annotation.processing.RoundEnvironment roundEnv, AbstractCommandSpecProcessor.Context context)
-
buildSpec
private void buildSpec(javax.lang.model.element.Element element, AbstractCommandSpecProcessor.Context context)
-
buildParentCommands
private void buildParentCommands(javax.annotation.processing.RoundEnvironment roundEnv, AbstractCommandSpecProcessor.Context context)
-
buildParentCommand
private void buildParentCommand(javax.lang.model.element.Element element, AbstractCommandSpecProcessor.Context context)
-
buildTypedMember
private CommandLine.Model.IAnnotatedElement buildTypedMember(javax.lang.model.element.Element element)
-
debugMethod
private void debugMethod(javax.lang.model.element.ExecutableElement method)
-
debugElement
private void debugElement(javax.lang.model.element.Element element, java.lang.String s)
-
logElementDetails
private void logElementDetails(javax.lang.model.element.Element element, java.lang.String s)
-
debugType
private void debugType(javax.lang.model.type.TypeMirror typeMirror, javax.lang.model.element.Element element, java.lang.String indent)
-
debugFoundAnnotations
private void debugFoundAnnotations(java.util.Set<? extends javax.lang.model.element.TypeElement> annotations, javax.annotation.processing.RoundEnvironment roundEnv)
-
logInfo
protected void logInfo(java.lang.String msg)
Prints a compile-time NOTE message.- Parameters:
msg
- the info message
-
error
protected void error(javax.lang.model.element.Element element, java.lang.String msg, java.lang.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.format
on the error message
-
warn
protected void warn(javax.lang.model.element.Element element, java.lang.String msg, java.lang.Object... args)
Prints a compile-time warning message for the specified element.- Parameters:
element
- the problematic element, may benull
msg
- the warning message with optional format specifiersargs
- the arguments to use to callString.format
on the warning message
-
fatalError
protected void fatalError(java.lang.String msg)
Prints a compile-time error message prefixed with "FATAL ERROR".- Parameters:
msg
- the error message with optional format specifiers
-
-