Class SmithyAnnotationProcessor<A extends Annotation>

java.lang.Object
javax.annotation.processing.AbstractProcessor
software.amazon.smithy.build.processor.SmithyAnnotationProcessor<A>
Type Parameters:
A - Annotation to execute the processor for. This annotation should target the PACKAGE ElementType and should be applied in the package-info.java file of a package.
All Implemented Interfaces:
Processor

public abstract class SmithyAnnotationProcessor<A extends Annotation> extends AbstractProcessor
Base implementation of a Smithy annotation 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.
  • Constructor Details

    • SmithyAnnotationProcessor

      public SmithyAnnotationProcessor()
  • Method Details

    • init

      public void init(ProcessingEnvironment processingEnv)
      Specified by:
      init in interface Processor
      Overrides:
      init in class AbstractProcessor
    • process

      public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv)
      Specified by:
      process in interface Processor
      Specified by:
      process in class AbstractProcessor
    • getPluginName

      protected abstract String getPluginName()
      Name of the Smithy build plugin to execute with this annotation processor.
      Returns:
      name of plugin to run.
    • getAnnotationClass

      protected abstract Class<A> 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 by getPluginName().

      Returns:
      class of the annotation used by this processor
    • createPluginNode

      protected abstract ObjectNode createPluginNode(A annotation, String packageName)
      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.