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.AbstractProcessorAbstract 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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classAbstractCommandSpecProcessor.Context(package private) static classAbstractCommandSpecProcessor.GraphHelper class for topologically sorting ArgGroups.(package private) static classAbstractCommandSpecProcessor.NullFactory
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.StringCOMMAND_DEFAULT_NAMEprivate static java.lang.StringCOMMAND_TYPE(package private) static java.util.logging.ConsoleHandlerhandlerprivate static java.util.logging.Loggerloggerprotected javax.annotation.processing.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 private voidbuildArgGroup(javax.lang.model.element.Element element, AbstractCommandSpecProcessor.Context context, javax.annotation.processing.RoundEnvironment roundEnv)private voidbuildArgGroups(javax.annotation.processing.RoundEnvironment roundEnv, AbstractCommandSpecProcessor.Context context)private CommandLine.Model.CommandSpecbuildCommand(javax.lang.model.element.Element element, AbstractCommandSpecProcessor.Context context, javax.annotation.processing.RoundEnvironment roundEnv)private voidbuildCommands(javax.annotation.processing.RoundEnvironment roundEnv, AbstractCommandSpecProcessor.Context context)private voidbuildMixin(javax.lang.model.element.Element element, javax.annotation.processing.RoundEnvironment roundEnv, AbstractCommandSpecProcessor.Context context)private voidbuildMixins(javax.annotation.processing.RoundEnvironment roundEnv, AbstractCommandSpecProcessor.Context context)private voidbuildOption(javax.lang.model.element.Element element, AbstractCommandSpecProcessor.Context context)private voidbuildOptions(javax.annotation.processing.RoundEnvironment roundEnv, AbstractCommandSpecProcessor.Context context)private voidbuildOptionsAndPositionalsFromMethodParameters(javax.lang.model.element.ExecutableElement method, CommandLine.Model.CommandSpec result, AbstractCommandSpecProcessor.Context context)private voidbuildParameter(javax.lang.model.element.Element element, AbstractCommandSpecProcessor.Context context)private voidbuildParameters(javax.annotation.processing.RoundEnvironment roundEnv, AbstractCommandSpecProcessor.Context context)private voidbuildParentCommand(javax.lang.model.element.Element element, AbstractCommandSpecProcessor.Context context)private voidbuildParentCommands(javax.annotation.processing.RoundEnvironment roundEnv, AbstractCommandSpecProcessor.Context context)private voidbuildSpec(javax.lang.model.element.Element element, AbstractCommandSpecProcessor.Context context)private voidbuildSpecs(javax.annotation.processing.RoundEnvironment roundEnv, AbstractCommandSpecProcessor.Context context)private CommandLine.Model.IAnnotatedElementbuildTypedMember(javax.lang.model.element.Element element)private java.util.Stack<javax.lang.model.element.TypeElement>buildTypeHierarchy(javax.lang.model.element.TypeElement typeElement)private voidbuildUnmatched(javax.annotation.processing.RoundEnvironment roundEnv, AbstractCommandSpecProcessor.Context context)private voidbuildUnmatched(javax.lang.model.element.Element element, AbstractCommandSpecProcessor.Context context)private voiddebugElement(javax.lang.model.element.Element element, java.lang.String s)private voiddebugFoundAnnotations(java.util.Set<? extends javax.lang.model.element.TypeElement> annotations, javax.annotation.processing.RoundEnvironment roundEnv)private voiddebugMethod(javax.lang.model.element.ExecutableElement method)private voiddebugType(javax.lang.model.type.TypeMirror typeMirror, javax.lang.model.element.Element element, java.lang.String indent)protected voiderror(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 TypedMemberextractTypedMember(javax.lang.model.element.Element element, java.lang.String annotation)protected voidfatalError(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.SourceVersiongetSupportedSourceVersion()Returns the max supported source version.protected abstract booleanhandleCommands(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 theCommandSpeccommand model objects that were found during compilation.voidinit(javax.annotation.processing.ProcessingEnvironment processingEnv)private booleanisSubcommand(javax.lang.model.element.ExecutableElement method, javax.annotation.processing.RoundEnvironment roundEnv)private booleanisValidSubcommandHasNameAttribute(javax.lang.model.element.Element subcommandElement)private voidlogElementDetails(javax.lang.model.element.Element element, java.lang.String s)protected voidlogInfo(java.lang.String msg)Prints a compile-time NOTE message.booleanprocess(java.util.Set<? extends javax.lang.model.element.TypeElement> annotations, javax.annotation.processing.RoundEnvironment roundEnv)private voidprocessEnclosedElements(AbstractCommandSpecProcessor.Context context, javax.annotation.processing.RoundEnvironment roundEnv, java.util.List<? extends javax.lang.model.element.Element> enclosedElements)private voidregisterSubcommands(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.Stringstacktrace(java.lang.Exception e)private static javax.lang.model.element.TypeElementsuperClassFor(javax.lang.model.element.TypeElement element)Obtains the super type element for a given type element.private booleantryProcess(java.util.Set<? extends javax.lang.model.element.TypeElement> annotations, javax.annotation.processing.RoundEnvironment roundEnv)private voidupdateCommandAttributes(CommandLine.Model.CommandSpec result, CommandLine.Command cmd)private voidupdateCommandFromMethodElement(javax.lang.model.element.ExecutableElement method, AbstractCommandSpecProcessor.Context context, CommandLine.Model.CommandSpec result, javax.annotation.processing.RoundEnvironment roundEnv)private voidupdateCommandSpecFromCommandAnnotation(CommandLine.Model.CommandSpec result, javax.lang.model.element.Element element, AbstractCommandSpecProcessor.Context context, javax.annotation.processing.RoundEnvironment roundEnv)private voidupdateCommandSpecFromTypeElement(javax.lang.model.element.TypeElement typeElement, AbstractCommandSpecProcessor.Context context, CommandLine.Model.CommandSpec result, javax.annotation.processing.RoundEnvironment roundEnv)protected voidwarn(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:
getSupportedAnnotationTypesin interfacejavax.annotation.processing.Processor- Overrides:
getSupportedAnnotationTypesin 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 theSupportedSourceVersionannotation.- Specified by:
getSupportedSourceVersionin interfacejavax.annotation.processing.Processor- Overrides:
getSupportedSourceVersionin classjavax.annotation.processing.AbstractProcessor- Returns:
- the max supported source version
-
init
public void init(javax.annotation.processing.ProcessingEnvironment processingEnv)
- Specified by:
initin interfacejavax.annotation.processing.Processor- Overrides:
initin 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:
processin interfacejavax.annotation.processing.Processor- Specified by:
processin 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 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.
-
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.formaton 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 benullmsg- the warning message with optional format specifiersargs- the arguments to use to callString.formaton 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
-
-