Code generation and code processing tools of Helidon.
We see the following three environments that are used for code processing:
- Annotation processors
- Classpath scanning
- Reflection based in a running VM
In Helidon, we do as much as possible using annotation processing and source code generation. For the cases where we need to analyze external libraries, or arbitrary code, we use classpath scanning (such as from a Maven plugin or command line tool). Reflection can only be used in selected modules that are intentionally reflection based (this should currently be limited to the Config object mapping module).
This top level module contains the following modules:
helidon-codegen- API and SPI and utilities that are shared between possible environmentshelidon-codegen-apt- implementations specific to annotation processinghelidon-codegen-scan- implementations specific to classpath scanningclass-model- class code generation abstraction, that provides builders to create a new source filecompiler- wrapper around Java compiler that is running within the current VMhelidon-copyright- Helidon specific implementation of copyright handler, used by Helidon project itself to generate sources
Codegen provides types that each code generation implementation can code against, without the need to hard code against annotation processing or classpath scanning.
Main entry point is the CodegenContext that provides:
- Current module info (if available) - this is a read-only representation of a module info, to validate
providesetc. CodegenFiler- filer abstraction, to generate source files and resourcesCodegenLogger- logger abstraction, with implementation for annotation processorMessager, MavenLogandSystem.LoggerCodegenScope- to provide information on the scope we are processing (expecting production or test)- possibility to obtain
TypeInfo(backed by appropriateTypeInfoFactoryBase) - access to
ElementMapper,TypeMapper, andAnnotationMapperused in those factories CodegenOptions- configuration options provided either from Maven plugin, Annotation processing options, or command line arguments
CodegenUtil- methods useful when generating code (such as capitalization of first letter, constant name from method name etc.)CopyrightHandler- API and SPI to generate correct copyright statementsElementInfoPredicates- predicates to filterTypedElementInfo, such as only getting public, static etc. elementsGeneratedAnnotationHandler- API and SPI to generate correct@GeneratedannotationModuleInfoSourceParser- to parse source code of module-info.javaTypesCodeGen- tool to generate source code to create instances of common types
Class model provides APIs to construct a class in memory, and then write it out (using for example CodegenFiler) as source file.
Start with ClassModel.builder().