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

Annotation Processing Boilerplate Destruction (Square Waterloo 2014)

Jake Wharton
September 23, 2014

Annotation Processing Boilerplate Destruction (Square Waterloo 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 cover on showcasing existing annotation processors for Android development. We will then cover how to get started writing your own annotation processor.

Video: http://youtu.be/dOcs-NKK-RA

Jake Wharton

September 23, 2014
Tweet

More Decks by Jake Wharton

Other Decks in Technology

Transcript

  1. JSR 269 • 6 months after Java 5 released •

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

    “Pluggable Annotation Processing API” • Preceded by doclet and apt standalone tools
  3. JSR 269 • 6 months after Java 5 released •

    “Pluggable Annotation Processing API” • Preceded by doclet and apt standalone tools • Google, Oracle, Sun, BEA Systems sponsored
  4. JSR 269 • 6 months after Java 5 released •

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

    “Pluggable Annotation Processing API” • Preceded by doclet and apt standalone tools • Google, Oracle, Sun, BEA Systems sponsored • Simplify and standardize processing annotations • No standalone tool, part of javac
  6. AutoValue @AutoValue
 abstract 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/
  7. AutoValue github.com/google/auto/ final class AutoValue_Animal extends Animal {
 private final

    String name;
 private final int numberOfLegs;
 
 AutoValue_Animal(
 String name,
 int numberOfLegs) {
 if (name == null) {
 throw new NullPointerException("Null name");
 }
 this.name = name;
 this.numberOfLegs = numberOfLegs;
 }
 
 @Override
 String name() {
 return name;
 }
 
 @Override
 int numberOfLegs() {
 return numberOfLegs;
 }
 
 @Override
 public String toString() {
 return "Animal{"
 + "name=" + name
 + ", numberOfLegs=" + numberOfLegs
 + "}";
 }
 
 @Override
 public boolean equals(Object o) {
 if (o == this) {
 return true;
 }
 if (o instanceof Animal) {
 Animal that = (Animal) o;
 return (this.name.equals(that.name()))
 && (this.numberOfLegs == that.numberOfLegs());
 }
 return false;
 }
 
 @Override
 public int hashCode() {
 int h = 1;
 h *= 1000003;
 h ^= name.hashCode();
 h *= 1000003;
 h ^= numberOfLegs;
 return h;
 }
 }
  8. AutoValue github.com/google/auto/ final class AutoValue_Animal extends Animal {
 private final

    String name;
 private final int numberOfLegs;
 
 AutoValue_Animal(
 String name,
 int numberOfLegs) {
 if (name == null) {
 throw new NullPointerException("Null name");
 }
 this.name = name;
 this.numberOfLegs = numberOfLegs;
 }
 
 @Override
 String name() {
 return name;
 }
 
 @Override
 int numberOfLegs() {
 return numberOfLegs;
 }
 
 @Override
 public String toString() {
 return "Animal{"
 + "name=" + name
 + ", numberOfLegs=" + numberOfLegs
 + "}";
 }
 
 @Override
 public boolean equals(Object o) {
 if (o == this) {
 return true;
 }
 if (o instanceof Animal) {
 Animal that = (Animal) o;
 return (this.name.equals(that.name()))
 && (this.numberOfLegs == that.numberOfLegs());
 }
 return false;
 }
 
 @Override
 public int hashCode() {
 int h = 1;
 h *= 1000003;
 h ^= name.hashCode();
 h *= 1000003;
 h ^= numberOfLegs;
 return h;
 }
 } Final fields Constructor Getters .toString() .equals() .hashCode()
  9. AutoParcel github.com/frankiesardo/auto-parcel/ @AutoParcel
 abstract class Animal implements Parcelable {
 static

    Animal create(String name, int numberOfLegs) {
 return new AutoParcel_Animal(name, numberOfLegs);
 } 
 abstract String name();
 abstract int numberOfLegs();
 }
  10. AutoParcel github.com/frankiesardo/auto-parcel/ @AutoParcel
 abstract class Animal implements Parcelable {
 static

    Animal create(String name, int numberOfLegs) {
 return new AutoParcel_Animal(name, numberOfLegs);
 } 
 abstract String name();
 abstract int numberOfLegs();
 }
  11. AutoParcel final class AutoParcel_Animal extends Animal {
 private final String

    name;
 private final int numberOfLegs;
 
 AutoParcel_Animal(
 String name,
 int numberOfLegs) {
 if (name == null) {
 throw new NullPointerException("Null name");
 }
 this.name = name;
 this.numberOfLegs = numberOfLegs;
 }
 
 @Override
 String name() {
 return name;
 }
 
 @Override
 int numberOfLegs() {
 return numberOfLegs;
 }
 
 @Override
 public String toString() {
 return "Animal{"
 + "name=" + name
 + ", numberOfLegs=" + numberOfLegs
 + "}";
 }
 
 @Override
 public boolean equals(Object o) {
 if (o == this) {
 return true;
 }
 if (o instanceof Animal) {
 Animal that = (Animal) o;
 return (this.name.equals(that.name()))
 && (this.numberOfLegs == that.numberOfLegs());
 }
 return false;
 }
 
 @Override
 public int hashCode() {
 int h = 1;
 h *= 1000003;
 h ^= name.hashCode();
 h *= 1000003;
 h ^= numberOfLegs;
 return h;
 }
 
 public static final Parcelable.Creator<Animal> CREATOR = new Parcelable.Creator<Animal>() {
 @Override public Animal createFromParcel(Parcel in) {
 return new AutoParcel_Animal(in);
 }
 @Override public Animal[] newArray(int size) {
 return new Animal[size];
 }
 };
 
 private final static ClassLoader CL = AutoParcel_Animal.class.getClassLoader();
 
 private AutoParcel_Animal(Parcel in) {
 this(
 (String) in.readValue(CL),
 (Integer) in.readValue(CL));
 }
 
 @Override public void writeToParcel(Parcel dest, int flags) {
 dest.writeValue(name);
 dest.writeValue(numberOfLegs);
 }
 
 @Override public int describeContents() {
 return 0;
 }
 } github.com/frankiesardo/auto-parcel/
  12. AutoParcel final class AutoParcel_Animal extends Animal {
 private final String

    name;
 private final int numberOfLegs;
 
 AutoParcel_Animal(
 String name,
 int numberOfLegs) {
 if (name == null) {
 throw new NullPointerException("Null name");
 }
 this.name = name;
 this.numberOfLegs = numberOfLegs;
 }
 
 @Override
 String name() {
 return name;
 }
 
 @Override
 int numberOfLegs() {
 return numberOfLegs;
 }
 
 @Override
 public String toString() {
 return "Animal{"
 + "name=" + name
 + ", numberOfLegs=" + numberOfLegs
 + "}";
 }
 
 @Override
 public boolean equals(Object o) {
 if (o == this) {
 return true;
 }
 if (o instanceof Animal) {
 Animal that = (Animal) o;
 return (this.name.equals(that.name()))
 && (this.numberOfLegs == that.numberOfLegs());
 }
 return false;
 }
 
 @Override
 public int hashCode() {
 int h = 1;
 h *= 1000003;
 h ^= name.hashCode();
 h *= 1000003;
 h ^= numberOfLegs;
 return h;
 }
 
 public static final Parcelable.Creator<Animal> CREATOR = new Parcelable.Creator<Animal>() {
 @Override public Animal createFromParcel(Parcel in) {
 return new AutoParcel_Animal(in);
 }
 @Override public Animal[] newArray(int size) {
 return new Animal[size];
 }
 };
 
 private final static ClassLoader CL = AutoParcel_Animal.class.getClassLoader();
 
 private AutoParcel_Animal(Parcel in) {
 this(
 (String) in.readValue(CL),
 (Integer) in.readValue(CL));
 }
 
 @Override public void writeToParcel(Parcel dest, int flags) {
 dest.writeValue(name);
 dest.writeValue(numberOfLegs);
 }
 
 @Override public int describeContents() {
 return 0;
 }
 } Final fields Constructor Getters .toString() .equals() .hashCode() github.com/frankiesardo/auto-parcel/ CREATOR Parcel constructor Parcel writer
  13. Butter Knife class SimpleActivity extends Activity {
 @InjectView(R.id.title) TextView title;


    @InjectView(R.id.hello) Button hello;
 @InjectView(R.id.list_of_things) ListView listOfThings;
 @OnClick(R.id.hello) void sayHello() {
 Toast.makeText(this, "Hello, views!", LENGTH_SHORT).show();
 } ! 
 // ...
 } jakewharton.github.io/butterknife/
  14. Butter Knife jakewharton.github.io/butterknife/ static void inject(Finder finder, final SimpleActivity target,

    Object source) {
 View view;
 view = finder.findRequired(source, 2130968576, "field 'title'");
 target.title = (android.widget.TextView) view;
 view = finder.findRequired(source, 2130968578, "field 'hello' and method 'sayHello'");
 target.hello = (android.widget.Button) view;
 view.setOnClickListener(new View.OnClickListener() {
 @Override public void onClick(View p0) {
 target.sayHello();
 }
 });
 view = finder.findRequired(source, 2130968579, "field 'listOfThings'"); target.listOfThings = (android.widget.ListView) view;
 }
  15. Butter Knife jakewharton.github.io/butterknife/ static void inject(Finder finder, final SimpleActivity target,

    Object source) {
 View view;
 view = finder.findRequired(source, 2130968576, "field 'title'");
 target.title = (android.widget.TextView) view;
 view = finder.findRequired(source, 2130968578, "field 'hello' and method 'sayHello'");
 target.hello = (android.widget.Button) view;
 view.setOnClickListener(new View.OnClickListener() {
 @Override public void onClick(View p0) {
 target.sayHello();
 }
 });
 view = finder.findRequired(source, 2130968579, "field 'listOfThings'"); target.listOfThings = (android.widget.ListView) view;
 }
  16. Butter Knife jakewharton.github.io/butterknife/ static void inject(Finder finder, final SimpleActivity target,

    Object source) {
 View view;
 view = finder.findRequired(source, 2130968576, "field 'title'");
 target.title = (android.widget.TextView) view;
 view = finder.findRequired(source, 2130968578, "field 'hello' and method 'sayHello'");
 target.hello = (android.widget.Button) view;
 view.setOnClickListener(new View.OnClickListener() {
 @Override public void onClick(View p0) {
 target.sayHello();
 }
 });
 view = finder.findRequired(source, 2130968579, "field 'listOfThings'"); target.listOfThings = (android.widget.ListView) view;
 }
  17. Butter Knife jakewharton.github.io/butterknife/ static void inject(Finder finder, final SimpleActivity target,

    Object source) {
 View view;
 view = finder.findRequired(source, 2130968576, "field 'title'");
 target.title = (android.widget.TextView) view;
 view = finder.findRequired(source, 2130968578, "field 'hello' and method 'sayHello'");
 target.hello = (android.widget.Button) view;
 view.setOnClickListener(new View.OnClickListener() {
 @Override public void onClick(View p0) {
 target.sayHello();
 }
 });
 view = finder.findRequired(source, 2130968579, "field 'listOfThings'"); target.listOfThings = (android.widget.ListView) view;
 }
  18. Dagger square.github.io/dagger/ @Module(injects = CoffeeApp.class)
 class DripCoffeeModule {
 @Provides @Singleton

    Heater provideHeater() {
 return new ElectricHeater();
 }
 
 @Provides Pump providePump(Thermosiphon pump) {
 return pump;
 }
 }

  19. 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/
  20. Dagger square.github.io/dagger/ class DripCoffeeModule$$ModuleAdapter extends ModuleAdapter<DripCoffeeModule> { 
 @Override
 public

    void getBindings(BindingsGroup bindings, DripCoffeeModule module) {
 bindings.contributeProvidesBinding("coffee.Heater", new ProvideHeaterAdapter(module)); bindings.contributeProvidesBinding("coffee.Pump",
 new ProvidePumpAdapter(module));
 } }
  21. Dagger square.github.io/dagger/ class DripCoffeeModule$$ModuleAdapter extends ModuleAdapter<DripCoffeeModule> { 
 @Override
 public

    void getBindings(BindingsGroup bindings, DripCoffeeModule module) {
 bindings.contributeProvidesBinding("coffee.Heater", new ProvideHeaterAdapter(module)); bindings.contributeProvidesBinding("coffee.Pump",
 new ProvidePumpAdapter(module));
 } }
  22. Dagger square.github.io/dagger/ class ProvideHeaterProvidesAdapter extends ProvidesBinding<Heater> { 
 private final

    DripCoffeeModule module;
 
 public ProvideHeaterProvidesAdapter(DripCoffeeModule module) {
 this.module = module;
 }
 
 @Override
 public Heater get() {
 return module.provideHeater();
 }
 }
  23. Dagger square.github.io/dagger/ class CoffeeMaker$$InjectAdapter
 extends Binding<CoffeeMaker> {
 
 private Binding<Lazy<Heater>>

    heater;
 private Binding<Pump> pump;
 
 CoffeeMaker$$InjectAdapter() {
 super("coffee.CoffeeMaker", CoffeeMaker.class);
 }
 
 @Override public void attach(Linker linker) {
 heater = linker.requestBinding("dagger.Lazy<coffee.Heater>");
 pump = linker.requestBinding("coffee.Pump");
 } ! // ...
 }
  24. Dagger square.github.io/dagger/ class CoffeeMaker$$InjectAdapter
 extends Binding<CoffeeMaker> {
 
 private Binding<Lazy<Heater>>

    heater;
 private Binding<Pump> pump;
 
 CoffeeMaker$$InjectAdapter() {
 super("coffee.CoffeeMaker", CoffeeMaker.class);
 }
 
 @Override public void attach(Linker linker) {
 heater = linker.requestBinding("dagger.Lazy<coffee.Heater>");
 pump = linker.requestBinding("coffee.Pump");
 } ! // ...
 }
  25. Dagger square.github.io/dagger/ class CoffeeMaker$$InjectAdapter
 extends Binding<CoffeeMaker> {
 
 // ...


    
 @Override public CoffeeMaker get() {
 CoffeeMaker result = new CoffeeMaker();
 injectMembers(result);
 return result;
 }
 
 @Override public void injectMembers(CoffeeMaker object) {
 object.heater = heater.get();
 object.pump = pump.get();
 }
 }
  26. Dagger square.github.io/dagger/ class CoffeeMaker$$InjectAdapter
 extends Binding<CoffeeMaker> {
 
 // ...


    
 @Override public CoffeeMaker get() {
 CoffeeMaker result = new CoffeeMaker();
 injectMembers(result);
 return result;
 }
 
 @Override public void injectMembers(CoffeeMaker object) {
 object.heater = heater.get();
 object.pump = pump.get();
 }
 }
  27. Dagger square.github.io/dagger/ @Module(injects = CoffeeApp.class)
 class DripCoffeeModule {
 @Provides @Singleton

    Heater provideHeater() {
 return new ElectricHeater();
 }
 
 @Provides Pump providePump(Thermosiphon pump) {
 return pump;
 }
 } class CoffeeMaker {
 @Inject Lazy<Heater> heater;
 @Inject Pump pump;
 // ...
 }
  28. Dagger square.github.io/dagger/ @Module(injects = CoffeeApp.class)
 class DripCoffeeModule {
 @Provides @Singleton

    Heater provideHeater() {
 return new ElectricHeater();
 }
 
 @Provides Pump providePump(Thermosiphon pump) {
 return pump;
 }
 } class CoffeeMaker {
 @Inject Lazy<Heater> heater;
 @Inject Pump pump;
 // ...
 } class ProvideHeaterProvidesAdapter extends ProvidesBinding<Heater> { 
 private final DripCoffeeModule module;
 
 public ProvideHeaterProvidesAdapter(DripCoffeeModule module) {
 this.module = module;
 }
 
 @Override
 public Heater get() {
 return module.provideHeater();
 }
 }
  29. Dagger square.github.io/dagger/ @Module(injects = CoffeeApp.class)
 class DripCoffeeModule {
 @Provides @Singleton

    Heater provideHeater() {
 return new ElectricHeater();
 }
 
 @Provides Pump providePump(Thermosiphon pump) {
 return pump;
 }
 } class CoffeeMaker {
 @Inject Lazy<Heater> heater;
 @Inject Pump pump;
 // ...
 } class DripCoffeeModule$$ModuleAdapter extends ModuleAdapter<DripCoffeeModule> { 
 @Override
 public void getBindings(BindingsGroup bindings, DripCoffeeModule module) {
 bindings.contributeProvidesBinding("coffee.Heater", new ProvideHeaterAdapter(module)); bindings.contributeProvidesBinding("coffee.Pump",
 new ProvidePumpAdapter(module));
 } } class ProvideHeaterProvidesAdapter extends ProvidesBinding<Heater> { 
 private final DripCoffeeModule module;
 
 public ProvideHeaterProvidesAdapter(DripCoffeeModule module) {
 this.module = module;
 }
 
 @Override
 public Heater get() {
 return module.provideHeater();
 }
 }
  30. Dagger square.github.io/dagger/ @Module(injects = CoffeeApp.class)
 class DripCoffeeModule {
 @Provides @Singleton

    Heater provideHeater() {
 return new ElectricHeater();
 }
 
 @Provides Pump providePump(Thermosiphon pump) {
 return pump;
 }
 } class CoffeeMaker {
 @Inject Lazy<Heater> heater;
 @Inject Pump pump;
 // ...
 } class DripCoffeeModule$$ModuleAdapter extends ModuleAdapter<DripCoffeeModule> { 
 @Override
 public void getBindings(BindingsGroup bindings, DripCoffeeModule module) {
 bindings.contributeProvidesBinding("coffee.Heater", new ProvideHeaterAdapter(module)); bindings.contributeProvidesBinding("coffee.Pump",
 new ProvidePumpAdapter(module));
 } } class ProvideHeaterProvidesAdapter extends ProvidesBinding<Heater> { 
 private final DripCoffeeModule module;
 
 public ProvideHeaterProvidesAdapter(DripCoffeeModule module) {
 this.module = module;
 }
 
 @Override
 public Heater get() {
 return module.provideHeater();
 }
 } class ObjectGraph {
 // ...
 }
  31. Dagger square.github.io/dagger/ @Module(injects = CoffeeApp.class)
 class DripCoffeeModule {
 @Provides @Singleton

    Heater provideHeater() {
 return new ElectricHeater();
 }
 
 @Provides Pump providePump(Thermosiphon pump) {
 return pump;
 }
 } class CoffeeMaker {
 @Inject Lazy<Heater> heater;
 @Inject Pump pump;
 // ...
 } class DripCoffeeModule$$ModuleAdapter extends ModuleAdapter<DripCoffeeModule> { 
 @Override
 public void getBindings(BindingsGroup bindings, DripCoffeeModule module) {
 bindings.contributeProvidesBinding("coffee.Heater", new ProvideHeaterAdapter(module)); bindings.contributeProvidesBinding("coffee.Pump",
 new ProvidePumpAdapter(module));
 } } class ProvideHeaterProvidesAdapter extends ProvidesBinding<Heater> { 
 private final DripCoffeeModule module;
 
 public ProvideHeaterProvidesAdapter(DripCoffeeModule module) {
 this.module = module;
 }
 
 @Override
 public Heater get() {
 return module.provideHeater();
 }
 } class CoffeeMaker$$InjectAdapter
 extends Binding<CoffeeMaker> {
 
 @Override public void attach(Linker linker) {
 heater = linker.requestBinding("dagger.Lazy<coffee.Heater>");
 pump = linker.requestBinding("coffee.Pump");
 } 
 @Override public void injectMembers(CoffeeMaker object) {
 object.heater = heater.get();
 object.pump = pump.get();
 } ! // ...
 } class ObjectGraph {
 // ...
 }
  32. Dagger square.github.io/dagger/ @Module(injects = CoffeeApp.class)
 class DripCoffeeModule {
 @Provides @Singleton

    Heater provideHeater() {
 return new ElectricHeater();
 }
 
 @Provides Pump providePump(Thermosiphon pump) {
 return pump;
 }
 } class CoffeeMaker {
 @Inject Lazy<Heater> heater;
 @Inject Pump pump;
 // ...
 } class DripCoffeeModule$$ModuleAdapter extends ModuleAdapter<DripCoffeeModule> { 
 @Override
 public void getBindings(BindingsGroup bindings, DripCoffeeModule module) {
 bindings.contributeProvidesBinding("coffee.Heater", new ProvideHeaterAdapter(module)); bindings.contributeProvidesBinding("coffee.Pump",
 new ProvidePumpAdapter(module));
 } } class ProvideHeaterProvidesAdapter extends ProvidesBinding<Heater> { 
 private final DripCoffeeModule module;
 
 public ProvideHeaterProvidesAdapter(DripCoffeeModule module) {
 this.module = module;
 }
 
 @Override
 public Heater get() {
 return module.provideHeater();
 }
 } class CoffeeMaker$$InjectAdapter
 extends Binding<CoffeeMaker> {
 
 @Override public void attach(Linker linker) {
 heater = linker.requestBinding("dagger.Lazy<coffee.Heater>");
 pump = linker.requestBinding("coffee.Pump");
 } 
 @Override public void injectMembers(CoffeeMaker object) {
 object.heater = heater.get();
 object.pump = pump.get();
 } ! // ...
 } class ObjectGraph {
 // ...
 }
  33. Dagger square.github.io/dagger/ @Module(injects = CoffeeApp.class)
 class DripCoffeeModule {
 @Provides @Singleton

    Heater provideHeater() {
 return new ElectricHeater();
 }
 
 @Provides Pump providePump(Thermosiphon pump) {
 return pump;
 }
 } class CoffeeMaker {
 @Inject Lazy<Heater> heater;
 @Inject Pump pump;
 // ...
 } class DripCoffeeModule$$ModuleAdapter extends ModuleAdapter<DripCoffeeModule> { 
 @Override
 public void getBindings(BindingsGroup bindings, DripCoffeeModule module) {
 bindings.contributeProvidesBinding("coffee.Heater", new ProvideHeaterAdapter(module)); bindings.contributeProvidesBinding("coffee.Pump",
 new ProvidePumpAdapter(module));
 } } class ProvideHeaterProvidesAdapter extends ProvidesBinding<Heater> { 
 private final DripCoffeeModule module;
 
 public ProvideHeaterProvidesAdapter(DripCoffeeModule module) {
 this.module = module;
 }
 
 @Override
 public Heater get() {
 return module.provideHeater();
 }
 } class CoffeeMaker$$InjectAdapter
 extends Binding<CoffeeMaker> {
 
 @Override public void attach(Linker linker) {
 heater = linker.requestBinding("dagger.Lazy<coffee.Heater>");
 pump = linker.requestBinding("coffee.Pump");
 } 
 @Override public void injectMembers(CoffeeMaker object) {
 object.heater = heater.get();
 object.pump = pump.get();
 } ! // ...
 }
  34. 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";
 } github.com/SimonVT/schematic/
  35. 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";
 
 } github.com/SimonVT/schematic/
  36. 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);
 }
  37. public class ExampleProcessor extends AbstractProcessor {
 @Override public SourceVersion getSupportedSourceVersion()

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

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

    {
 return SourceVersion.latestSupported();
 }
 
 @Override public Set<String> getSupportedAnnotationTypes() {
 return Collections.singleton(Example.class.getName());
 }
 
 
 
 
 
 
 }
  40. 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) {
 
 }
 }
  41. public class ExampleProcessor extends AbstractProcessor {
 // ...
 @Override public

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

    public boolean process(
 Set<? extends TypeElement> annotations,
 RoundEnvironment env) {
 Set<? extends Element> elements
 = env.getElementsAnnotatedWith(Example.class);
 
 
 
 
 
 }
 }
  43. 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);
 }
 
 
 }
 }
  44. 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;
 }
 }
  45. 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;
 }
 }
  46. Execution • Happens automatically when javac runs. • ServiceLoader finds

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

    and creates Processor instances. • Inside a full JVM. • Compiled sources and its dependencies not available.
  48. 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.
  49. 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);
 }
  50. 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);
 }
  51. 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);
 }
  52. Processing Environment interface ProcessingEnvironment {
 Map<String, String> getOptions(); 
 SourceVersion

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

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

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

    getSourceVersion();
 
 Locale getLocale();
 
 Messager getMessager();
 
 Filer getFiler();
 
 Elements getElementUtils();
 
 Types getTypeUtils();
 }
  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);
 }
 }
  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
  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
  59. 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
  60. 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
  61. 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
  62. 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
  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. 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);
 }
 }
  66. 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);
 }
 }
  67. 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);
 }
 }
  68. 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);
 }
 }
  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
 }
 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
 }
 return false;
 }
 } 
 
 
 
 
 
 
 
 
 );
 
 
 

  71. 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()
 
 
 

  72. 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;
 }
 }
  73. 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;
 }
 }
  74. 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;
 }
 }
  75. Processing Rounds @Example
 public class RandomClass {
 // ...
 }

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

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

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

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

    ExampleProcessor OtherProcessor javac RandomClass.class out/ @Other
 public class GeneratedClass {
 // ...
 } @Example
 public class RandomClass {
 // ...
 } javac
  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 {
 // ...
 }
  82. Processing Rounds @Example
 public class RandomClass {
 // ...
 }

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

    ExampleProcessor OtherProcessor javac RandomClass.class out/ @Other
 public class GeneratedClass {
 // ...
 } @Example
 public class RandomClass {
 // ...
 } javac
  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
  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 ExampleProcessor
  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 OtherProcessor
  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 OtherProcessor 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 {
 // ...
 }
  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 {
 // ...
 }
  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 {
 // ...
 } 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 {
 // ...
 } ExampleProcessor 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 {
 // ...
 } OtherProcessor 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 {
 // ...
 } javac public class OtherGenClass {
 // ...
 } OtherGenClass.class public class OtherGenClass {
 // ...
 }
  95. 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 {
 // ...
 }
  96. 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 {
 // ...
 }
  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;
 }
 }
  98. 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'.
 
 
 
 

  99. 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.
 
 
 

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

    annotations,
 RoundEnvironment env) {
 
 
 
 
 return false;
 }

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

    annotations,
 RoundEnvironment env) {
 
 
 
 
 return false;
 }
 
 
 
 List<ExampleModel> models = parseExampleAnnotations(env);
  102. 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) {
 // ...
 }
  103. 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) {
 // ...
 }
  104. 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) {
 // ...
 }
  105. Parsing and Generation Element ExampleModel ! name: “Foo” package: “com.example”

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

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

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

    • google/auto ‘common’ • AutoService (google/auto)
  109. 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();
  110. 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();
  111. 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();
  112. AutoService • Remember “ServiceLoader finds and creates” • AutoService generates

    service files from annotations ! public class ExampleProcessor extends AbstractProcessor { // ... }
  113. AutoService • Remember “ServiceLoader finds and creates” • AutoService generates

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

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


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

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

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

    class GeneratedClass {",
 "}"
 ); JavaFileObject expectedFile =
 JavaFileObjects.forSourceString("test.GeneratedClass", expected);
  119. 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()
 );
 }
 }
  120. 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 {",
 "}"
 );
  121. 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 {",
 "}"
 );
  122. 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
  123. 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