Class SmithyAnnotationProcessor<A extends Annotation>
- Type Parameters:
A
- Annotation to execute the processor for. This annotation should target thePACKAGE
ElementType
and should be applied in thepackage-info.java
file of a package.
- All Implemented Interfaces:
Processor
This implementation can be extended to create an Annotation processor for a Smithy build plugin that generates Java code. The processor will execute a single build plugin and write any generated Java code created by the plugin to the correct output path.
Smithy models on the classpath of extending annotation processors are automatically
discovered and any java artifacts generated by this plugin are written to standard locations.
Generated resource files are written to the class output while all other generated Java
classes are written to the source output. Plugin artifacts that are not java files or
resource files under META-INF/
are ignored.
Extending classes must specify the name of the build plugin they will execute using
getPluginName()
. This plugin must be discoverable on the annotation processor classpath.
The plugin is then executed using the data from the annotation as the plugin configuration.
For example, if your plugin has the following configuration node in a `smithy-build.json` file:
"myPlugin" : { "packageName" = "myPackage.namespace", "listOfTags" = ["a", "b", "c"] }Then you would define an annotation like the following:
@Target(ElementType.PACKAGE)
public @interface MyPlugin {
String[] listOfTags();
}
The createPluginNode(A, java.lang.String)
method is used to map this annotation to the object node
expected by the plugin. Once a mapping has been defined for the annotation, it can be
added to the package-info.java
as follows:
@MyPlugin(listOfTags = {"a", "b", "c"})
package com.example.traitcodegen;
import com.example.annotations.MyPlugin;
The base processor class will discover the namespace of the package the annotation
is applied to and pass that as a parameter to the createPluginNode(A, java.lang.String)
method.-
Field Summary
Fields inherited from class javax.annotation.processing.AbstractProcessor
processingEnv
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected abstract ObjectNode
createPluginNode
(A annotation, String packageName) Maps annotation data to a plugin configuration node.Annotation class for the processor.protected abstract String
Name of the Smithy build plugin to execute with this annotation processor.void
init
(ProcessingEnvironment processingEnv) boolean
process
(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) Methods inherited from class javax.annotation.processing.AbstractProcessor
getCompletions, getSupportedAnnotationTypes, getSupportedOptions, getSupportedSourceVersion, isInitialized
-
Constructor Details
-
SmithyAnnotationProcessor
public SmithyAnnotationProcessor()
-
-
Method Details
-
init
- Specified by:
init
in interfaceProcessor
- Overrides:
init
in classAbstractProcessor
-
process
- Specified by:
process
in interfaceProcessor
- Specified by:
process
in classAbstractProcessor
-
getPluginName
Name of the Smithy build plugin to execute with this annotation processor.- Returns:
- name of plugin to run.
-
getAnnotationClass
Annotation class for the processor.Each implementation of
SmithyProcessor
should have a specific package-scoped annotation used for configuration.createPluginNode(Annotation, String)
maps this annotation to the configuration node for the plugin specified bygetPluginName()
.- Returns:
- class of the annotation used by this processor
-
createPluginNode
Maps annotation data to a plugin configuration node.- Parameters:
annotation
- instance of generator annotation to use to create the build config.- Returns:
- ObjectNode to use as plugin configuration node.
-