Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Annotation Processing Boilerplate Destruction (Droidcon NYC 2014)

Jake Wharton
September 20, 2014

Annotation Processing Boilerplate Destruction (Droidcon NYC 2014)

The Java programming language has a knack for requiring a lot of boilerplate code. Annotation processing is a feature of the Java compiler which provides hooks that allow automatic code generation based on annotations. This greatly simplifies your code by pushing the burden of the boilerplate on automated tooling.

This talk will briefly showcase existing annotation processors for Android development. We will then focus on how to get started writing your own annotation processor.

Jake Wharton

September 20, 2014
Tweet

More Decks by Jake Wharton

Other Decks in Technology

Transcript

  1. Dagger class CoffeeMaker {
 @Inject Lazy<Heater> heater;
 @Inject Pump pump;


    
 public void brew() {
 heater.get().on();
 pump.pump();
 System.out.println(" [_]P coffee! [_]P ");
 heater.get().off();
 }
 }
 square.github.io/dagger/
  2. Butter Knife class SimpleActivity extends Activity {
 @InjectView(R.id.title) TextView title;


    @InjectView(R.id.subtitle) TextView subtitle;
 @InjectView(R.id.hello) Button hello;
 @InjectView(R.id.list_of_things) ListView listOfThings;
 @InjectView(R.id.footer) TextView footer;
 
 // ...
 } jakewharton.github.io/butterknife/
  3. Butter Knife class SimpleActivity extends Activity {
 @InjectView(R.id.title) TextView title;


    @InjectView(R.id.subtitle) TextView subtitle;
 @InjectView(R.id.hello) Button hello;
 @InjectView(R.id.list_of_things) ListView listOfThings;
 @InjectView(R.id.footer) TextView footer;
 
 // ...
 } jakewharton.github.io/butterknife/ ButterKnife.inject(this);
  4. AutoValue @AutoValue
 abstract static class Animal {
 static Animal create(String

    name, int numberOfLegs) {
 return new AutoValue_Animal(name, numberOfLegs);
 } 
 abstract String name();
 abstract int numberOfLegs();
 }
 github.com/google/auto/
  5. AutoParcel @AutoParcel
 abstract class SomeModel implements Parcelable {
 abstract String

    name();
 abstract List<SomeSubModel> subModels();
 abstract Map<String, OtherSubModel> modelsMap();
 
 static SomeModel create(String name, List<SomeSubModel> subModels, Map<String, OtherSubModel> modelsMap) {
 return new AutoParcel_SomeModel(name, subModels, modelsMap);
 }
 } github.com/frankiesardo/auto-parcel/
  6. Schematic interface NoteColumns {
 
 @DataType(INTEGER) @PrimaryKey @AutoIncrement
 String _ID

    = "_id";
 
 @DataType(INTEGER) @References(table = NotesDatabase.LISTS,
 column = ListColumns._ID)
 String LIST_ID = "listId";
 
 @DataType(TEXT) String NOTE = "note";
 }
  7. Schematic @Database(version = NotesDatabase.VERSION)
 public final class NotesDatabase {
 


    public static final int VERSION = 1;
 
 @Table(ListColumns.class)
 public static final String LISTS = "lists";
 
 @Table(NoteColumns.class)
 public static final String NOTES = "notes";
 
 }
  8. Doclet import com.sun.javadoc.*; ! public class ExampleDoclet {
 public static

    boolean start(RootDoc root) {
 ! ! ! 
 }
 } http://docs.oracle.com/javase/8/docs/technotes/guides/javadoc/doclet/overview.html
  9. Doclet import com.sun.javadoc.*; ! public class ExampleDoclet {
 public static

    boolean start(RootDoc root) {
 ! ! ! 
 }
 } import com.sun.javadoc.*; ! public class ExampleDoclet {
 public static boolean start(RootDoc root) {
 ClassDoc[] classes = root.classes();
 for (int i = 0; i < classes.length; i++) {
 System.out.println(classes[i]);
 }
 return true; 
 }
 } http://docs.oracle.com/javase/8/docs/technotes/guides/javadoc/doclet/overview.html
  10. Doclet import com.sun.javadoc.*; ! public class ExampleDoclet {
 public static

    boolean start(RootDoc root) {
 ! ! ! 
 }
 } import com.sun.javadoc.*; ! public class ExampleDoclet {
 public static boolean start(RootDoc root) {
 ClassDoc[] classes = root.classes();
 for (int i = 0; i < classes.length; i++) {
 System.out.println(classes[i]);
 }
 return true; 
 }
 } javadoc -doclet ExampleDoclet -docletpath . RandomClass.java \
 FooBarClass.java HelloClass.java WorldClass.java import com.sun.javadoc.*; ! public class ExampleDoclet {
 public static boolean start(RootDoc root) {
 ClassDoc[] classes = root.classes();
 for (int i = 0; i < classes.length; i++) {
 System.out.println(classes[i]);
 }
 return true; 
 }
 } http://docs.oracle.com/javase/8/docs/technotes/guides/javadoc/doclet/overview.html
  11. Java 1.5 • JSR 14: Generics • JSR 175: Annotations

    • Also a whole bunch of other really awesome things!
  12. Java 1.5 • JSR 14: Generics • JSR 175: Annotations

    • Also a whole bunch of other really awesome things! • apt command-line tool
  13. apt

  14. @Target(ElementType.TYPE)
 public @interface Example {
 } @Example
 public class RandomClass

    {
 // ...
 } @Example
 public class FooBarClass {
 // ...
 } @Example
 public class HelloClass {
 // ...
 } @Example
 public class WorldClass {
 // ...
 } @Target(ElementType.TYPE)
 public @interface Example {
 } apt
  15. interface AnnotationProcessorFactory { 
 AnnotationProcessor getProcessorFor( Set<AnnotationTypeDeclaration> atds, AnnotationProcessorEnvironment env);

    
 Collection<String> supportedAnnotationTypes(); 
 Collection<String> supportedOptions(); 
 } interface AnnotationProcessor {
 void process();
 } apt
  16. apt public class ExampleAnnotationProcessorFactory implements AnnotationProcessorFactory { 
 
 


    
 
 
 
 
 
 
 @Override public Collection<String> supportedOptions() {
 return Collections.emptyList();
 }
 }
  17. apt public class ExampleAnnotationProcessorFactory implements AnnotationProcessorFactory { 
 
 


    
 
 
 @Override public Collection<String> supportedAnnotationTypes() {
 return Collections.singletonList(Example.class.getName());
 }
 
 @Override public Collection<String> supportedOptions() {
 return Collections.emptyList();
 }
 }
  18. apt public class ExampleAnnotationProcessorFactory implements AnnotationProcessorFactory { 
 @Override public

    AnnotationProcessor getProcessorFor( Set<AnnotationTypeDeclaration> atds,
 AnnotationProcessorEnvironment env) {
 return new ExampleAnnotationProcessor(atds, env);
 }
 
 @Override public Collection<String> supportedAnnotationTypes() {
 return Collections.singletonList(Example.class.getName());
 }
 
 @Override public Collection<String> supportedOptions() {
 return Collections.emptyList();
 }
 }
  19. apt public class ExampleAnnotationProcessorFactory implements AnnotationProcessorFactory { 
 @Override public

    AnnotationProcessor getProcessorFor( Set<AnnotationTypeDeclaration> atds,
 AnnotationProcessorEnvironment env) {
 return new ExampleAnnotationProcessor(atds, env);
 }
 
 @Override public Collection<String> supportedAnnotationTypes() {
 return Collections.singletonList(Example.class.getName());
 }
 
 @Override public Collection<String> supportedOptions() {
 return Collections.emptyList();
 }
 }
  20. apt public class 
 
 
 
 
 
 


    
 
 } public class ExampleAnnotationProcessor implements AnnotationProcessor { 
 private final Set<AnnotationTypeDeclaration> atds;
 private final AnnotationProcessorEnvironment env;
 
 public ExampleAnnotationProcessor( Set<AnnotationTypeDeclaration> atds,
 AnnotationProcessorEnvironment env) {
 this.atds = atds;
 this.env = env;
 } 
 }
  21. apt public class 
 
 
 
 
 
 


    
 
 } public class 
 
 Set<AnnotationTypeDeclaration> atds 
 } public class ExampleAnnotationProcessor implements AnnotationProcessor { 
 
 
 
 
 
 
 
 
 } public class ExampleAnnotationProcessor implements AnnotationProcessor { 
 // ...
 
 
 
 
 
 
 
 } public class ExampleAnnotationProcessor implements AnnotationProcessor { 
 // ...
 
 @Override public void process() {
 ! ! 
 } ! 
 }
  22. apt public class 
 
 
 
 
 
 


    
 
 } public class 
 
 Set<AnnotationTypeDeclaration> atds 
 } public class ExampleAnnotationProcessor implements AnnotationProcessor { 
 
 
 
 
 
 
 
 
 } public class ExampleAnnotationProcessor implements AnnotationProcessor { 
 // ...
 
 
 
 
 
 
 
 } public class ExampleAnnotationProcessor implements AnnotationProcessor { 
 // ...
 
 @Override public void process() { for (AnnotationTypeDeclaration atd : atds) {
 MemberDeclaration member = atd.getDeclaringType();
 System.out.println(member);
 }
 } ! ! }
  23. JSR 269 • 6 months after Java 5 released •

    “Pluggable Annotation Processing API”
  24. JSR 269 • 6 months after Java 5 released •

    “Pluggable Annotation Processing API” • Google, Oracle, Sun, BEA Systems sponsored
  25. JSR 269 • 6 months after Java 5 released •

    “Pluggable Annotation Processing API” • Google, Oracle, Sun, BEA Systems sponsored • Simplify and standardize processing annotations
  26. JSR 269 • 6 months after Java 5 released •

    “Pluggable Annotation Processing API” • Google, Oracle, Sun, BEA Systems sponsored • Simplify and standardize processing annotations • No standalone tool, part of javac
  27. interface Processor {
 Set<String> getSupportedOptions();
 
 Set<String> getSupportedAnnotationTypes();
 
 SourceVersion

    getSupportedSourceVersion();
 
 void init(ProcessingEnvironment processingEnv);
 
 boolean process(Set<? extends TypeElement> annotations,
 RoundEnvironment roundEnv);
 
 Iterable<? extends Completion> getCompletions(Element element,
 AnnotationMirror annotation,
 ExecutableElement member,
 String userText);
 }
  28. public class ExampleProcessor extends AbstractProcessor {
 @Override public SourceVersion getSupportedSourceVersion()

    {
 return SourceVersion.latestSupported();
 }
 
 
 
 
 
 
 
 
 
 
 }
  29. public class ExampleProcessor extends AbstractProcessor {
 @Override public SourceVersion getSupportedSourceVersion()

    {
 return SourceVersion.latestSupported();
 }
 
 @Override public Set<String> getSupportedAnnotationTypes() {
 
 }
 
 
 
 
 
 
 }
  30. public class ExampleProcessor extends AbstractProcessor {
 @Override public SourceVersion getSupportedSourceVersion()

    {
 return SourceVersion.latestSupported();
 }
 
 @Override public Set<String> getSupportedAnnotationTypes() {
 return Collections.singleton(Example.class.getName());
 }
 
 
 
 
 
 
 }
  31. public class ExampleProcessor extends AbstractProcessor {
 @Override public SourceVersion getSupportedSourceVersion()

    {
 return SourceVersion.latestSupported();
 }
 
 @Override public Set<String> getSupportedAnnotationTypes() {
 return Collections.singleton(Example.class.getName());
 }
 
 @Override public boolean process(
 Set<? extends TypeElement> annotations,
 RoundEnvironment env) {
 
 }
 }
  32. public class ExampleProcessor extends AbstractProcessor {
 // ...
 @Override public

    boolean process(
 Set<? extends TypeElement> annotations,
 RoundEnvironment env) {
 ! ! ! ! ! 
 }
 }
  33. public class ExampleProcessor extends AbstractProcessor {
 // ...
 
 @Override

    public boolean process(
 Set<? extends TypeElement> annotations,
 RoundEnvironment env) {
 Set<? extends Element> elements
 = env.getElementsAnnotatedWith(Example.class);
 
 
 
 
 
 }
 }
  34. public class ExampleProcessor extends AbstractProcessor {
 // ...
 
 @Override

    public boolean process(
 Set<? extends TypeElement> annotations,
 RoundEnvironment env) {
 Set<? extends Element> elements
 = env.getElementsAnnotatedWith(Example.class);
 for (Element element : elements) {
 System.out.println(element);
 }
 
 
 }
 }
  35. public class ExampleProcessor extends AbstractProcessor {
 // ...
 
 @Override

    public boolean process(
 Set<? extends TypeElement> annotations,
 RoundEnvironment env) {
 Set<? extends Element> elements
 = env.getElementsAnnotatedWith(Example.class);
 for (Element element : elements) {
 System.out.println(element);
 }
 
 return false;
 }
 }
  36. public class ExampleProcessor extends AbstractProcessor {
 @Override public SourceVersion getSupportedSourceVersion()

    {
 return SourceVersion.latestSupported();
 }
 
 @Override public Set<String> getSupportedAnnotationTypes() {
 return Collections.singleton(Example.class.getName());
 }
 
 @Override public boolean process(
 Set<? extends TypeElement> annotations,
 RoundEnvironment env) {
 Set<? extends Element> elements
 = env.getElementsAnnotatedWith(Example.class);
 for (Element element : elements) {
 System.out.println(element);
 }
 return false;
 }
 }
  37. Service Loader • META-­‐INF/services/ • File name of implemented fully-qualified

    class name. • javax.annotation.processing.Processor • Each line implementation fully-qualified class name.
  38. Service Loader • META-­‐INF/services/ • File name of implemented fully-qualified

    class name. • javax.annotation.processing.Processor • Each line implementation fully-qualified class name. • com.example.ExampleProcessor
  39. Execution • Happens automatically when javac runs. • ServiceLoader finds

    and creates Processor instances. • Inside a full JVM.
  40. Execution • Happens automatically when javac runs. • ServiceLoader finds

    and creates Processor instances. • Inside a full JVM. • Compiled sources and its dependencies not available.
  41. Execution • Happens automatically when javac runs. • ServiceLoader finds

    and creates Processor instances. • Inside a full JVM. • Compiled sources and its dependencies not available. • Bring your own dependencies.
  42. Environment interface Processor {
 Set<String> getSupportedOptions();
 
 Set<String> getSupportedAnnotationTypes();
 


    SourceVersion getSupportedSourceVersion();
 
 void init(ProcessingEnvironment processingEnv);
 
 boolean process(Set<? extends TypeElement> annotations,
 RoundEnvironment roundEnv);
 
 Iterable<? extends Completion> getCompletions(Element element,
 AnnotationMirror annotation,
 ExecutableElement member,
 String userText);
 }
  43. Environment interface Processor {
 Set<String> getSupportedOptions();
 
 Set<String> getSupportedAnnotationTypes();
 


    SourceVersion getSupportedSourceVersion();
 
 void init(ProcessingEnvironment processingEnv);
 
 boolean process(Set<? extends TypeElement> annotations,
 RoundEnvironment roundEnv);
 
 Iterable<? extends Completion> getCompletions(Element element,
 AnnotationMirror annotation,
 ExecutableElement member,
 String userText);
 }
  44. Environment interface Processor {
 Set<String> getSupportedOptions();
 
 Set<String> getSupportedAnnotationTypes();
 


    SourceVersion getSupportedSourceVersion();
 
 void init(ProcessingEnvironment processingEnv);
 
 boolean process(Set<? extends TypeElement> annotations,
 RoundEnvironment roundEnv);
 
 Iterable<? extends Completion> getCompletions(Element element,
 AnnotationMirror annotation,
 ExecutableElement member,
 String userText);
 }
  45. Environment interface Processor {
 Set<String> getSupportedOptions();
 
 Set<String> getSupportedAnnotationTypes();
 


    SourceVersion getSupportedSourceVersion();
 
 void init(ProcessingEnvironment processingEnv);
 
 boolean process(Set<? extends TypeElement> annotations,
 RoundEnvironment roundEnv);
 
 Iterable<? extends Completion> getCompletions(Element element,
 AnnotationMirror annotation,
 ExecutableElement member,
 String userText);
 }
  46. Environment interface Processor {
 Set<String> getSupportedOptions();
 
 Set<String> getSupportedAnnotationTypes();
 


    SourceVersion getSupportedSourceVersion();
 
 void init(ProcessingEnvironment processingEnv);
 
 boolean process(Set<? extends TypeElement> annotations,
 RoundEnvironment roundEnv);
 
 Iterable<? extends Completion> getCompletions(Element element,
 AnnotationMirror annotation,
 ExecutableElement member,
 String userText);
 }
  47. Processing Environment interface ProcessingEnvironment {
 Map<String, String> getOptions(); 
 SourceVersion

    getSourceVersion();
 
 Locale getLocale();
 
 Messager getMessager();
 
 Filer getFiler();
 
 Elements getElementUtils();
 
 Types getTypeUtils();
 }
  48. Processing Environment interface ProcessingEnvironment {
 Map<String, String> getOptions(); 
 SourceVersion

    getSourceVersion();
 
 Locale getLocale();
 
 Messager getMessager();
 
 Filer getFiler();
 
 Elements getElementUtils();
 
 Types getTypeUtils();
 }
  49. Processing Environment interface ProcessingEnvironment {
 Map<String, String> getOptions(); 
 SourceVersion

    getSourceVersion();
 
 Locale getLocale();
 
 Messager getMessager();
 
 Filer getFiler();
 
 Elements getElementUtils();
 
 Types getTypeUtils();
 }
  50. Processing Environment interface ProcessingEnvironment {
 Map<String, String> getOptions(); 
 SourceVersion

    getSourceVersion();
 
 Locale getLocale();
 
 Messager getMessager();
 
 Filer getFiler();
 
 Elements getElementUtils();
 
 Types getTypeUtils();
 }
  51. Processing Environment interface ProcessingEnvironment {
 Map<String, String> getOptions(); 
 SourceVersion

    getSourceVersion();
 
 Locale getLocale();
 
 Messager getMessager();
 
 Filer getFiler();
 
 Elements getElementUtils();
 
 Types getTypeUtils();
 }
  52. Elements @Example
 public class ExampleClass implements Runnable { 
 private

    final String name;
 
 public ExampleClass(String name) {
 this.name = name;
 }
 
 @Override public void run() {
 System.out.println("Hello, " + name);
 }
 }
  53. Elements @Example
 public class ExampleClass implements Runnable { 
 private

    final String name;
 
 public ExampleClass(String name) {
 this.name = name;
 }
 
 @Override public void run() {
 System.out.println("Hello, " + name);
 }
 } TypeElement
  54. Elements @Example
 public class ExampleClass implements Runnable { 
 private

    final String name;
 
 public ExampleClass(String name) {
 this.name = name;
 }
 
 @Override public void run() {
 System.out.println("Hello, " + name);
 }
 } TypeElement VariableElement
  55. Elements @Example
 public class ExampleClass implements Runnable { 
 private

    final String name;
 
 public ExampleClass(String name) {
 this.name = name;
 }
 
 @Override public void run() {
 System.out.println("Hello, " + name);
 }
 } TypeElement VariableElement ExecutableElement
  56. Elements @Example
 public class ExampleClass implements Runnable { 
 private

    final String name;
 
 public ExampleClass(String name) {
 this.name = name;
 }
 
 @Override public void run() {
 System.out.println("Hello, " + name);
 }
 } TypeElement VariableElement ExecutableElement ExecutableElement
  57. Elements @Example
 public class ExampleClass implements Runnable { 
 private

    final String name;
 
 public ExampleClass(String name) {
 this.name = name;
 }
 
 @Override public void run() {
 System.out.println("Hello, " + name);
 }
 } TypeElement VariableElement ExecutableElement ExecutableElement
  58. Elements @Example
 public class ExampleClass implements Runnable { 
 private

    final String name;
 
 public ExampleClass(String name) {
 this.name = name;
 }
 
 @Override public void run() {
 System.out.println("Hello, " + name);
 }
 } TypeElement VariableElement ExecutableElement ExecutableElement
  59. Types @Example
 public class ExampleClass implements Runnable { 
 private

    final String name;
 
 public ExampleClass(String name) {
 this.name = name;
 }
 
 @Override public void run() {
 System.out.println("Hello, " + name);
 }
 }
  60. Types @Example
 public class ExampleClass implements Runnable { 
 private

    final String name;
 
 public ExampleClass(String name) {
 this.name = name;
 }
 
 @Override public void run() {
 System.out.println("Hello, " + name);
 }
 }
  61. Types @Example
 public class ExampleClass implements Runnable { 
 private

    final String name;
 
 public ExampleClass(String name) {
 this.name = name;
 }
 
 @Override public void run() {
 System.out.println("Hello, " + name);
 }
 }
  62. Types @Example
 public class ExampleClass implements Runnable { 
 private

    final String name;
 
 public ExampleClass(String name) {
 this.name = name;
 }
 
 @Override public void run() {
 System.out.println("Hello, " + name);
 }
 }
  63. Types @Example
 public class ExampleClass implements Runnable { 
 private

    final String name;
 
 public ExampleClass(String name) {
 this.name = name;
 }
 
 @Override public void run() {
 System.out.println("Hello, " + name);
 }
 }
  64. Types @Example
 public class ExampleClass implements Runnable { 
 private

    final String name;
 
 public ExampleClass(String name) {
 this.name = name;
 }
 
 @Override public void run() {
 System.out.println("Hello, " + name);
 }
 }
  65. Environment public class ExampleProcessor extends AbstractProcessor {
 // ...
 


    @Override public boolean process(
 Set<? extends TypeElement> annotations,
 RoundEnvironment env) {
 Set<? extends Element> elements
 = env.getElementsAnnotatedWith(Example.class);
 for (Element element : elements) {
 System.out.println(element
 }
 return false;
 }
 } 
 
 
 
 
 
 
 
 
 );
 
 
 

  66. Environment public class ExampleProcessor extends AbstractProcessor {
 // ...
 


    @Override public boolean process(
 Set<? extends TypeElement> annotations,
 RoundEnvironment env) {
 Set<? extends Element> elements
 = env.getElementsAnnotatedWith(Example.class);
 for (Element element : elements) {
 System.out.println(element
 }
 return false;
 }
 } 
 
 
 
 
 
 
 
 
 );
 
 
 

  67. Environment public class ExampleProcessor extends AbstractProcessor {
 // ...
 


    @Override public boolean process(
 Set<? extends TypeElement> annotations,
 RoundEnvironment env) {
 Set<? extends Element> elements
 = env.getElementsAnnotatedWith(Example.class);
 for (Element element : elements) {
 System.out.println( );
 }
 return false;
 }
 } 
 
 
 
 
 
 
 
 
 element.asType()
 
 
 

  68. Environment public class ExampleProcessor extends AbstractProcessor {
 // ...
 


    @Override public boolean process(
 Set<? extends TypeElement> annotations,
 RoundEnvironment env) {
 Set<? extends Element> elements
 = env.getElementsAnnotatedWith(Example.class);
 for (Element element : elements) {
 System.out.println(element.asType());
 }
 return false;
 }
 }
  69. Environment public class ExampleProcessor extends AbstractProcessor {
 // ...
 


    @Override public boolean process(
 Set<? extends TypeElement> annotations,
 RoundEnvironment env) {
 Set<? extends Element> elements
 = env.getElementsAnnotatedWith(Example.class);
 for (Element element : elements) {
 System.out.println(element.asType());
 }
 return false;
 }
 }
  70. Environment public class ExampleProcessor extends AbstractProcessor {
 // ...
 


    @Override public boolean process(
 Set<? extends TypeElement> annotations,
 RoundEnvironment env) {
 Set<? extends Element> elements
 = env.getElementsAnnotatedWith(Example.class);
 for (Element element : elements) {
 System.out.println(element.asType());
 }
 return false;
 }
 }
  71. Round Environment public interface RoundEnvironment {
 boolean processingOver();
 
 boolean

    errorRaised();
 
 Set<? extends Element> getRootElements();
 
 Set<? extends Element> getElementsAnnotatedWith(
 TypeElement a);
 
 Set<? extends Element> getElementsAnnotatedWith(
 Class<? extends Annotation> a);
 }
  72. Round Environment public interface RoundEnvironment {
 boolean processingOver();
 
 boolean

    errorRaised();
 
 Set<? extends Element> getRootElements();
 
 Set<? extends Element> getElementsAnnotatedWith(
 TypeElement a);
 
 Set<? extends Element> getElementsAnnotatedWith(
 Class<? extends Annotation> a);
 }
  73. Processing Rounds @Example
 public class RandomClass {
 // ...
 }

    ExampleProcessor OtherProcessor javac RandomClass.class out/ @Example
 public class RandomClass {
 // ...
 } javac
  74. Processing Rounds @Example
 public class RandomClass {
 // ...
 }

    ExampleProcessor OtherProcessor javac RandomClass.class out/ @Example
 public class RandomClass {
 // ...
 } javac ExampleProcessor
  75. Processing Rounds @Example
 public class RandomClass {
 // ...
 }

    ExampleProcessor OtherProcessor javac RandomClass.class out/ @Other
 public class GeneratedClass {
 // ...
 } @Example
 public class RandomClass {
 // ...
 } javac ExampleProcessor
  76. Processing Rounds @Example
 public class RandomClass {
 // ...
 }

    ExampleProcessor OtherProcessor javac RandomClass.class out/ @Other
 public class GeneratedClass {
 // ...
 } @Example
 public class RandomClass {
 // ...
 } javac OtherProcessor
  77. Processing Rounds @Example
 public class RandomClass {
 // ...
 }

    ExampleProcessor OtherProcessor javac RandomClass.class out/ @Other
 public class GeneratedClass {
 // ...
 } @Example
 public class RandomClass {
 // ...
 } javac
  78. Processing Rounds @Example
 public class RandomClass {
 // ...
 }

    ExampleProcessor OtherProcessor javac RandomClass.class out/ @Other
 public class GeneratedClass {
 // ...
 } @Example
 public class RandomClass {
 // ...
 } javac
  79. Processing Rounds @Example
 public class RandomClass {
 // ...
 }

    ExampleProcessor OtherProcessor javac RandomClass.class out/ @Other
 public class GeneratedClass {
 // ...
 } @Example
 public class RandomClass {
 // ...
 }
  80. Processing Rounds @Example
 public class RandomClass {
 // ...
 }

    ExampleProcessor OtherProcessor javac RandomClass.class out/ @Other
 public class GeneratedClass {
 // ...
 } @Example
 public class RandomClass {
 // ...
 } javac
  81. Processing Rounds @Example
 public class RandomClass {
 // ...
 }

    ExampleProcessor OtherProcessor javac RandomClass.class out/ @Other
 public class GeneratedClass {
 // ...
 } @Example
 public class RandomClass {
 // ...
 } javac
  82. Processing Rounds @Example
 public class RandomClass {
 // ...
 }

    ExampleProcessor OtherProcessor javac RandomClass.class out/ @Other
 public class GeneratedClass {
 // ...
 } GeneratedClass.class @Example
 public class RandomClass {
 // ...
 } @Other
 public class GeneratedClass {
 // ...
 } javac
  83. Processing Rounds @Example
 public class RandomClass {
 // ...
 }

    ExampleProcessor OtherProcessor javac RandomClass.class out/ @Other
 public class GeneratedClass {
 // ...
 } GeneratedClass.class @Example
 public class RandomClass {
 // ...
 } @Other
 public class GeneratedClass {
 // ...
 } javac ExampleProcessor
  84. Processing Rounds @Example
 public class RandomClass {
 // ...
 }

    ExampleProcessor OtherProcessor javac RandomClass.class out/ @Other
 public class GeneratedClass {
 // ...
 } GeneratedClass.class @Example
 public class RandomClass {
 // ...
 } @Other
 public class GeneratedClass {
 // ...
 } javac OtherProcessor
  85. Processing Rounds @Example
 public class RandomClass {
 // ...
 }

    ExampleProcessor OtherProcessor javac RandomClass.class out/ @Other
 public class GeneratedClass {
 // ...
 } GeneratedClass.class @Example
 public class RandomClass {
 // ...
 } @Other
 public class GeneratedClass {
 // ...
 } javac OtherProcessor public class OtherGenClass {
 // ...
 }
  86. Processing Rounds @Example
 public class RandomClass {
 // ...
 }

    ExampleProcessor OtherProcessor javac RandomClass.class out/ @Other
 public class GeneratedClass {
 // ...
 } GeneratedClass.class @Example
 public class RandomClass {
 // ...
 } @Other
 public class GeneratedClass {
 // ...
 } javac public class OtherGenClass {
 // ...
 }
  87. Processing Rounds @Example
 public class RandomClass {
 // ...
 }

    ExampleProcessor OtherProcessor javac RandomClass.class out/ @Other
 public class GeneratedClass {
 // ...
 } GeneratedClass.class @Example
 public class RandomClass {
 // ...
 } @Other
 public class GeneratedClass {
 // ...
 } javac public class OtherGenClass {
 // ...
 }
  88. Processing Rounds @Example
 public class RandomClass {
 // ...
 }

    ExampleProcessor OtherProcessor javac RandomClass.class out/ @Other
 public class GeneratedClass {
 // ...
 } GeneratedClass.class @Example
 public class RandomClass {
 // ...
 } @Other
 public class GeneratedClass {
 // ...
 } javac public class OtherGenClass {
 // ...
 }
  89. Processing Rounds @Example
 public class RandomClass {
 // ...
 }

    ExampleProcessor OtherProcessor javac RandomClass.class out/ @Other
 public class GeneratedClass {
 // ...
 } GeneratedClass.class @Example
 public class RandomClass {
 // ...
 } @Other
 public class GeneratedClass {
 // ...
 } javac public class OtherGenClass {
 // ...
 } OtherGenClass.class public class OtherGenClass {
 // ...
 }
  90. Processing Rounds @Example
 public class RandomClass {
 // ...
 }

    ExampleProcessor OtherProcessor javac RandomClass.class out/ @Other
 public class GeneratedClass {
 // ...
 } GeneratedClass.class @Example
 public class RandomClass {
 // ...
 } @Other
 public class GeneratedClass {
 // ...
 } javac public class OtherGenClass {
 // ...
 } ExampleProcessor OtherGenClass.class public class OtherGenClass {
 // ...
 }
  91. Processing Rounds @Example
 public class RandomClass {
 // ...
 }

    ExampleProcessor OtherProcessor javac RandomClass.class out/ @Other
 public class GeneratedClass {
 // ...
 } GeneratedClass.class @Example
 public class RandomClass {
 // ...
 } @Other
 public class GeneratedClass {
 // ...
 } javac public class OtherGenClass {
 // ...
 } OtherProcessor OtherGenClass.class public class OtherGenClass {
 // ...
 }
  92. Processing Rounds @Example
 public class RandomClass {
 // ...
 }

    ExampleProcessor OtherProcessor javac RandomClass.class out/ @Other
 public class GeneratedClass {
 // ...
 } GeneratedClass.class @Example
 public class RandomClass {
 // ...
 } @Other
 public class GeneratedClass {
 // ...
 } javac public class OtherGenClass {
 // ...
 } OtherGenClass.class public class OtherGenClass {
 // ...
 }
  93. Processing Rounds @Example
 public class RandomClass {
 // ...
 }

    ExampleProcessor OtherProcessor javac RandomClass.class out/ @Other
 public class GeneratedClass {
 // ...
 } GeneratedClass.class @Example
 public class RandomClass {
 // ...
 } @Other
 public class GeneratedClass {
 // ...
 } javac public class OtherGenClass {
 // ...
 } OtherGenClass.class public class OtherGenClass {
 // ...
 }
  94. Processing Rounds @Example
 public class RandomClass {
 // ...
 }

    ExampleProcessor OtherProcessor javac RandomClass.class out/ @Other
 public class GeneratedClass {
 // ...
 } GeneratedClass.class @Example
 public class RandomClass {
 // ...
 } @Other
 public class GeneratedClass {
 // ...
 } public class OtherGenClass {
 // ...
 } OtherGenClass.class public class OtherGenClass {
 // ...
 }
  95. Parsing and Generation public class ExampleProcessor extends AbstractProcessor {
 //

    ...
 
 @Override public boolean process(
 Set<? extends TypeElement> annotations,
 RoundEnvironment env) {
 Set<? extends Element> elements
 = env.getElementsAnnotatedWith(Example.class);
 
 for (Element element : elements) {
 
 
 }
 return false;
 }
 }
  96. Parsing and Generation public class ExampleProcessor extends AbstractProcessor {
 //

    ...
 
 @Override public boolean process(
 Set<? extends TypeElement> annotations,
 RoundEnvironment env) {
 Set<? extends Element> elements
 = env.getElementsAnnotatedWith(Example.class);
 
 for (Element element : elements) {
 
 
 }
 return false;
 }
 } 
 
 
 
 
 
 
 
 
 // Look at attributes of 'element'.
 
 
 
 

  97. Parsing and Generation public class ExampleProcessor extends AbstractProcessor {
 //

    ...
 
 @Override public boolean process(
 Set<? extends TypeElement> annotations,
 RoundEnvironment env) {
 Set<? extends Element> elements
 = env.getElementsAnnotatedWith(Example.class);
 
 for (Element element : elements) {
 
 
 }
 return false;
 }
 } 
 
 
 
 
 
 
 
 
 // Look at attributes of 'element'.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 // Generate some code based on attributes.
 
 
 

  98. Parsing and Generation @Override public boolean process(
 Set<? extends TypeElement>

    annotations,
 RoundEnvironment env) {
 
 
 
 
 return false;
 }

  99. Parsing and Generation @Override public boolean process(
 Set<? extends TypeElement>

    annotations,
 RoundEnvironment env) {
 
 
 
 
 return false;
 }
 
 
 
 List<ExampleModel> models = parseExampleAnnotations(env);
  100. Parsing and Generation @Override public boolean process(
 Set<? extends TypeElement>

    annotations,
 RoundEnvironment env) {
 
 
 
 
 return false;
 }
 
 
 
 List<ExampleModel> models = parseExampleAnnotations(env); 
 
 
 
 
 
 
 
 
 
 List<ExampleModel> parseExampleAnnotations(RoundEnvironment env) {
 // ...
 }
  101. Parsing and Generation @Override public boolean process(
 Set<? extends TypeElement>

    annotations,
 RoundEnvironment env) {
 
 
 
 
 return false;
 }
 
 
 
 List<ExampleModel> models = parseExampleAnnotations(env); 
 
 
 
 for (ExampleModel model : models) {
 emitExampleCode(model);
 } 
 
 
 
 
 
 
 
 
 
 List<ExampleModel> parseExampleAnnotations(RoundEnvironment env) {
 // ...
 }
  102. Parsing and Generation @Override public boolean process(
 Set<? extends TypeElement>

    annotations,
 RoundEnvironment env) {
 
 
 
 
 return false;
 }
 
 
 
 List<ExampleModel> models = parseExampleAnnotations(env); 
 
 
 
 for (ExampleModel model : models) {
 emitExampleCode(model);
 } 
 
 
 
 
 
 
 
 
 
 List<ExampleModel> parseExampleAnnotations(RoundEnvironment env) {
 // ...
 } 
 
 
 
 
 
 
 
 
 ! ! 
 
 void emitExampleCode(ExampleModel model) {
 // ...
 }
  103. Parsing and Generation Element ExampleModel ! name: “Foo” package: “com.example”

    public: true ExampleModel ! name: “Foo” package: “com.example” public: true element:
  104. Parsing and Generation Element ExampleModel ! name: “Foo” package: “com.example”

    public: true ExampleModel ! name: “Foo” package: “com.example” public: true element: GeneratedClass
  105. Parsing and Generation Element ExampleModel ! name: “Foo” package: “com.example”

    public: true ExampleModel ! name: “Foo” package: “com.example” public: true element: GeneratedClass
  106. Bring Your Own Dependencies • Guava, Guava, Guava! • JavaWriter

    • google/auto ‘common’ • AutoService (google/auto)
  107. JavaWriter JavaFileObject sourceFile = filer.createSourceFile(name, element); ! ! ! JavaWriter

    writer = new JavaWriter(sourceFile.openWriter());
 writer.emitPackage("com.example")
 .emitAnnotation(Other.class)
 .beginType("GeneratedClass", "class")
 .emitSingleLineComment("...")
 .endType();
  108. JavaWriter JavaFileObject sourceFile = filer.createSourceFile(name, element); ! ! @com.example.Other
 public

    class GeneratedClass {
 // ...
 } ! ! ! JavaWriter writer = new JavaWriter(sourceFile.openWriter());
 writer.emitPackage("com.example")
 .emitAnnotation(Other.class)
 .beginType("GeneratedClass", "class")
 .emitSingleLineComment("...")
 .endType();
  109. JavaWriter JavaFileObject sourceFile = filer.createSourceFile(name, element); import com.example.Other; ! @Other


    public class GeneratedClass {
 // ...
 } ! ! ! JavaWriter writer = new JavaWriter(sourceFile.openWriter());
 writer.emitPackage("com.example")
 .emitImports(Other.class)
 .emitAnnotation(Other.class)
 .beginType("GeneratedClass", "class")
 .emitSingleLineComment("...")
 .endType(); JavaFileObject sourceFile = filer.createSourceFile(name, element);
  110. AutoService • Remember?
 META-­‐INF/services/javax.annotation.processing.Processor • AutoService generates service files from

    annotations ! public class ExampleProcessor extends AbstractProcessor { // ... } @AutoService(Processor.class)
  111. Testing and Debugging • Google to the rescue! • Guava

    • Guava • compile-testing • Truth
  112. final class TestProcessors {
 static Iterable<? extends Processor> exampleProcessors() {


    return Collections.singletonList(
 new ExampleProcessor()
 );
 }
 }
  113. String input = Joiner.on('\n').join(
 "package test;",
 "import com.example.Example;",
 "@Example",
 "public

    class ExampleClass {",
 "}"
 ); JavaFileObject inputFile =
 JavaFileObjects.forSourceString("test.Test", input);
  114. JavaFileObject inputFile =
 JavaFileObjects.forSourceString("test.Test", input); String input = """\
 package

    test; 
 import com.example.Example; 
 @Example
 public class ExampleClass {
 } """ .stripIndent()
  115. String expected = Joiner.on('\n').join(
 "package test;",
 "import com.example.Other;",
 "@Other",
 "public

    class GeneratedClass {",
 "}"
 ); JavaFileObject expectedFile =
 JavaFileObjects.forSourceString("test.GeneratedClass", expected);
  116. ASSERT.about(javaSource())
 .that(inputFile)
 .processedWith(exampleProcessors())
 .compilesWithoutError()
 .and()
 .generatesSources(expectedFile); String input = Joiner.on('\n').join(


    "package test;",
 "import com.example.Example;",
 "@Example",
 "public class ExampleClass {",
 "}"
 ); final class TestProcessors {
 static Iterable<? extends Processor> exampleProcessors() {
 return Collections.singletonList(
 new ExampleProcessor()
 );
 }
 }
  117. ASSERT.about(javaSource())
 .that(inputFile)
 .processedWith(exampleProcessors())
 .compilesWithoutError()
 .and()
 .generatesSources(expectedFile); String input = Joiner.on('\n').join(


    "package test;",
 "import com.example.Example;",
 "@Example",
 "public class ExampleClass {",
 "}"
 ); final class TestProcessors {
 static Iterable<? extends Processor> exampleProcessors() {
 return Collections.singletonList(
 new ExampleProcessor()
 );
 }
 } String expected = Joiner.on('\n').join(
 "package test;",
 "import com.example.Other;",
 "@Other",
 "public class GeneratedClass {",
 "}"
 );
  118. ASSERT.about(javaSource())
 .that(inputFile)
 .processedWith(exampleProcessors())
 .compilesWithoutError()
 .and()
 .generatesSources(expectedFile); String input = Joiner.on('\n').join(


    "package test;",
 "import com.example.Example;",
 "@Example",
 "public class ExampleClass {",
 "}"
 ); final class TestProcessors {
 static Iterable<? extends Processor> exampleProcessors() {
 return Collections.singletonList(
 new ExampleProcessor()
 );
 }
 } String expected = Joiner.on('\n').join(
 "package test;",
 "import com.example.Other;",
 "@Other",
 "public class GeneratedClass {",
 "}"
 );
  119. public class ExampleProcessor extends AbstractProcessor {
 // ...
 
 @Override

    public boolean process(
 Set<? extends TypeElement> annotations,
 RoundEnvironment env) {
 Set<? extends Element> elements
 = env.getElementsAnnotatedWith(Example.class);
 for (Element element : elements) {
 System.out.println(element
 }
 return false;
 }
 } Debugging
  120. public class ExampleProcessor extends AbstractProcessor {
 // ...
 
 @Override

    public boolean process(
 Set<? extends TypeElement> annotations,
 RoundEnvironment env) {
 Set<? extends Element> elements
 = env.getElementsAnnotatedWith(Example.class);
 for (Element element : elements) {
 System.out.println(element
 }
 return false;
 }
 } Debugging