@SupportedAnnotationTypes(value="*") public class AndHowCompileProcessor extends AbstractProcessor
SupportedAnnotationTypes("*"), allowing it to
'see' all classes as they are compiled. This class then delegates to a
'scanner' class that does deep inspection on compiled code, looking for
AndHow Properties.
When an AndHow Property is found in a class, a new PropertyRegistrar
class is created, which contains the list of Properties in that class.
There is a one-to-one correspondence between user classes that contain
AndHow Properties and auto-created PropertyRegistrar classes.
At runtime, AndHow will use the ServiceLoader to discover all instances
of PropertyRegistrar on the classpath, thus finding all AndHow
Property containing classes.
| Modifier and Type | Class and Description |
|---|---|
protected static class |
AndHowCompileProcessor.CauseEffect
Match up a causal Element (Basically the compiler representation of a class to be compiled) w/
the Class name that will be registered in a service registry.
|
processingEnv| Constructor and Description |
|---|
AndHowCompileProcessor()
A no-arg constructor is required.
|
| Modifier and Type | Method and Description |
|---|---|
int |
getJdkVersion()
The major version number of the JDK used to compile.
|
int |
getSrcVersion()
The java language level the output file needs to be compatible with.
|
SourceVersion |
getSupportedSourceVersion() |
void |
init(ProcessingEnvironment processingEnv) |
boolean |
process(Set<? extends TypeElement> annotations,
RoundEnvironment roundEnv) |
protected void |
processFinalRound(Filer filer,
Messager log,
int srcVersion,
int jdkVersion,
List<CompileProblem> compileProblems,
List<AndHowCompileProcessor.CauseEffect> initCEs,
List<AndHowCompileProcessor.CauseEffect> testInitCEs,
List<AndHowCompileProcessor.CauseEffect> registrarCEs)
The final round of annotation processing.
|
protected void |
processNonFinalRound(ProcessingEnvironment localProcessingEnv,
RoundEnvironment roundEnv,
Calendar runDate,
Filer filer,
Messager log,
int srcVersion,
int jdkVersion,
List<CompileProblem> compileProblems,
List<AndHowCompileProcessor.CauseEffect> initCEs,
List<AndHowCompileProcessor.CauseEffect> testInitCEs,
List<AndHowCompileProcessor.CauseEffect> registrarCEs)
Process a round of annotation processing.
|
protected CompileUnit |
scanTypeElement(ProcessingEnvironment localProcessingEnv,
TypeElement typeElement)
Scan the TypeElement for init, testInit and AndHow properties.
|
void |
writeClassFile(Filer filer,
PropertyRegistrarClassGenerator generator,
Element causingElement)
Writes a new class implementing the
PropertyRegistrar interface. |
protected void |
writeServiceFile(Filer filer,
String fullyQualifiedServiceInterfaceName,
List<AndHowCompileProcessor.CauseEffect> implementingClasses)
Writes a new META-INF service file with a list of classes implementing the
specified interface.
|
getCompletions, getSupportedAnnotationTypes, getSupportedOptions, isInitializedpublic AndHowCompileProcessor()
public void init(ProcessingEnvironment processingEnv)
init in interface Processorinit in class AbstractProcessorpublic int getSrcVersion()
This is provided by the ProcessingEnvironment.getSourceVersion() as an enum, but is
converted here to the major version number. Version 1.8 return 8, ver. 9 returns 9 and so on.
public int getJdkVersion()
This taken from System.getProperty("java.version"),
See CompileUtil.getMajorJavaVersion().
JDK 1.8 return 8, JDK 9 returns 9 and so on.
public SourceVersion getSupportedSourceVersion()
getSupportedSourceVersion in interface ProcessorgetSupportedSourceVersion in class AbstractProcessorpublic boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv)
process in interface Processorprocess in class AbstractProcessorprotected void processNonFinalRound(ProcessingEnvironment localProcessingEnv, RoundEnvironment roundEnv, Calendar runDate, Filer filer, Messager log, int srcVersion, int jdkVersion, List<CompileProblem> compileProblems, List<AndHowCompileProcessor.CauseEffect> initCEs, List<AndHowCompileProcessor.CauseEffect> testInitCEs, List<AndHowCompileProcessor.CauseEffect> registrarCEs)
There will always be a 'final' round of processing after all annotation processors have completed and stop generating new files.
localProcessingEnv - The compile-time environment, for writing and analyzing classesroundEnv - State of a single round of annotation processingrunDate - Single consistent timestamp for the entire compilationfiler - ProcessingEnv provided file system for writing source fileslog - ProcessingEnv provided message/log systemsrcVersion - Major version number of the source code the compiler is usingjdkVersion - Major version number of the JDKcompileProblems - Accumulation of problems - reported in final processing roundinitCEs - Accumulation of AndHowInit implementing classes, as they are discoveredtestInitCEs - Accumulation of AndHowTestInit implementing classes, as they are discoveredregistrarCEs - Accumulation of AndHow Property containing top-level classes, as they are discoveredprotected void processFinalRound(Filer filer, Messager log, int srcVersion, int jdkVersion, List<CompileProblem> compileProblems, List<AndHowCompileProcessor.CauseEffect> initCEs, List<AndHowCompileProcessor.CauseEffect> testInitCEs, List<AndHowCompileProcessor.CauseEffect> registrarCEs)
No new root elements (i.e. application classes) are processed in this round. Instead, this round is the opportunity to write final generated output:
filer - ProcessingEnv provided file system for writing source fileslog - ProcessingEnv provided message/log systemsrcVersion - Major version number of the source code the compiler is usingjdkVersion - Major version number of the JDKcompileProblems - Accumulation of problems - reported in final processing roundinitCEs - Accumulation of AndHowInit implementing classes, as they are discoveredtestInitCEs - Accumulation of AndHowTestInit implementing classes, as they are discoveredregistrarCEs - Accumulation of AndHow Property containing top-level classes, as they are discoveredprotected CompileUnit scanTypeElement(ProcessingEnvironment localProcessingEnv, TypeElement typeElement)
localProcessingEnv - The environment used to analyze the typeElementtypeElement - Represents a class being compiledpublic void writeClassFile(Filer filer, PropertyRegistrarClassGenerator generator, Element causingElement) throws Exception
PropertyRegistrar interface.
The new class directly corresponds to a user classes containing AndHow Properties and will contain meta data about the properties.
filer - The javac file system representation for writing files.generator - AndHow class capable of generating source code for this
PropertyRegistrar class.causingElement - A javac Element, which generically refers to any
piece of source code such as a keyword, class name, etc.. When a file
is written to the filer, a causingElement is recorded as metadata
so there is an association between the file and the reason it was written.
Likely this is normally used to associate source code line numbers with
generated code.Exception - If unable to write (out of disc space?) or other issuesprotected void writeServiceFile(Filer filer, String fullyQualifiedServiceInterfaceName, List<AndHowCompileProcessor.CauseEffect> implementingClasses) throws IOException
The new class directly corresponds to a user classes containing AndHow Properties and will contain meta data about the properties.
filer - The ProcessingEnvironment provided FilerfullyQualifiedServiceInterfaceName - The name of the interface the classes implementimplementingClasses - List of CauseEffect's with each class implementing the interfaceIOException - If unable to write the fileCopyright © 2022. All rights reserved.