Share
Embed
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 PRO
September 23, 2014
Transcript
182 slides
-
JSR 269 • 6 months after Java 5 released •
“Pluggable Annotation Processing API” -
JSR 269 • 6 months after Java 5 released •
“Pluggable Annotation Processing API” • Preceded by doclet and apt standalone tools -
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 -
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 -
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 -
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/ -
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; } } -
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() -
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(); } -
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(); } -
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/ -
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 -
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/ -
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; } -
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; } -
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; } -
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; } -
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; } } -
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/ -
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)); } } -
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)); } } -
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(); } } -
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"); } ! // ... } -
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"); } ! // ... } -
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(); } } -
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(); } } -
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; // ... } -
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(); } } -
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(); } } -
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 { // ... } -
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 { // ... } -
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 { // ... } -
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(); } ! // ... } -
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/ -
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/ -
Schematic @ContentProvider(authority = NotesProvider.AUTHORITY, database = NotesDatabase.class) public final class
NotesProvider { // ... } github.com/SimonVT/schematic/ -
Execution • Happens automatically when javac runs. • ServiceLoader finds
and creates Processor instances. -
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); } -
public class ExampleProcessor extends AbstractProcessor { @Override public SourceVersion getSupportedSourceVersion()
{ return SourceVersion.latestSupported(); } } -
public class ExampleProcessor extends AbstractProcessor { @Override public SourceVersion getSupportedSourceVersion()
{ return SourceVersion.latestSupported(); } @Override public Set<String> getSupportedAnnotationTypes() { } } -
public class ExampleProcessor extends AbstractProcessor { @Override public SourceVersion getSupportedSourceVersion()
{ return SourceVersion.latestSupported(); } @Override public Set<String> getSupportedAnnotationTypes() { return Collections.singleton(Example.class.getName()); } } -
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) { } } -
public class ExampleProcessor extends AbstractProcessor { // ... @Override public
boolean process( Set<? extends TypeElement> annotations, RoundEnvironment env) { ! ! ! ! ! } } -
public class ExampleProcessor extends AbstractProcessor { // ... @Override
public boolean process( Set<? extends TypeElement> annotations, RoundEnvironment env) { Set<? extends Element> elements = env.getElementsAnnotatedWith(Example.class); } } -
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); } } } -
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; } } -
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; } } -
Execution • Happens automatically when javac runs. • ServiceLoader finds
and creates Processor instances. -
Execution • Happens automatically when javac runs. • ServiceLoader finds
and creates Processor instances. • Inside a full JVM. -
Execution • Happens automatically when javac runs. • ServiceLoader finds
and creates Processor instances. • Inside a full JVM. • Compiled sources and its dependencies not available. -
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. -
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); } -
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); } -
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); } -
Processing Environment interface ProcessingEnvironment { Map<String, String> getOptions(); SourceVersion
getSourceVersion(); Locale getLocale(); Messager getMessager(); Filer getFiler(); Elements getElementUtils(); Types getTypeUtils(); } -
Processing Environment interface ProcessingEnvironment { Map<String, String> getOptions(); SourceVersion
getSourceVersion(); Locale getLocale(); Messager getMessager(); Filer getFiler(); Elements getElementUtils(); Types getTypeUtils(); } -
Processing Environment interface ProcessingEnvironment { Map<String, String> getOptions(); SourceVersion
getSourceVersion(); Locale getLocale(); Messager getMessager(); Filer getFiler(); Elements getElementUtils(); Types getTypeUtils(); } -
Processing Environment interface ProcessingEnvironment { Map<String, String> getOptions(); SourceVersion
getSourceVersion(); Locale getLocale(); Messager getMessager(); Filer getFiler(); Elements getElementUtils(); Types getTypeUtils(); } -
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); } } -
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 -
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 -
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 -
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 -
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 -
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 -
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); } } -
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); } } -
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); } } -
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); } } -
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); } } -
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); } } -
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; } } ); -
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; } } ); -
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() -
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; } } -
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; } } -
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; } } -
Processing Rounds @Example public class RandomClass { // ... }
ExampleProcessor OtherProcessor javac -
Processing Rounds @Example public class RandomClass { // ... }
ExampleProcessor OtherProcessor javac javac -
Processing Rounds @Example public class RandomClass { // ... }
ExampleProcessor OtherProcessor javac RandomClass.class out/ @Example public class RandomClass { // ... } javac -
Processing Rounds @Example public class RandomClass { // ... }
ExampleProcessor OtherProcessor javac RandomClass.class out/ @Example public class RandomClass { // ... } javac ExampleProcessor -
Processing Rounds @Example public class RandomClass { // ... }
ExampleProcessor OtherProcessor javac RandomClass.class out/ @Other public class GeneratedClass { // ... } @Example public class RandomClass { // ... } javac ExampleProcessor -
Processing Rounds @Example public class RandomClass { // ... }
ExampleProcessor OtherProcessor javac RandomClass.class out/ @Other public class GeneratedClass { // ... } @Example public class RandomClass { // ... } javac OtherProcessor -
Processing Rounds @Example public class RandomClass { // ... }
ExampleProcessor OtherProcessor javac RandomClass.class out/ @Other public class GeneratedClass { // ... } @Example public class RandomClass { // ... } javac -
Processing Rounds @Example public class RandomClass { // ... }
ExampleProcessor OtherProcessor javac RandomClass.class out/ @Other public class GeneratedClass { // ... } @Example public class RandomClass { // ... } javac -
Processing Rounds @Example public class RandomClass { // ... }
ExampleProcessor OtherProcessor javac RandomClass.class out/ @Other public class GeneratedClass { // ... } @Example public class RandomClass { // ... } -
Processing Rounds @Example public class RandomClass { // ... }
ExampleProcessor OtherProcessor javac RandomClass.class out/ @Other public class GeneratedClass { // ... } @Example public class RandomClass { // ... } javac -
Processing Rounds @Example public class RandomClass { // ... }
ExampleProcessor OtherProcessor javac RandomClass.class out/ @Other public class GeneratedClass { // ... } @Example public class RandomClass { // ... } javac -
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 -
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 -
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 -
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 { // ... } -
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 { // ... } -
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 { // ... } -
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 { // ... } -
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 { // ... } -
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 { // ... } -
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 { // ... } -
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 { // ... } -
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 { // ... } -
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 { // ... } -
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; } } -
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'. -
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. -
Parsing and Generation @Override public boolean process( Set<? extends TypeElement>
annotations, RoundEnvironment env) { return false; } -
Parsing and Generation @Override public boolean process( Set<? extends TypeElement>
annotations, RoundEnvironment env) { return false; } List<ExampleModel> models = parseExampleAnnotations(env); -
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) { // ... } -
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) { // ... } -
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) { // ... } -
Parsing and Generation Element ExampleModel ! name: “Foo” package: “com.example”
public: true ExampleModel ! name: “Foo” package: “com.example” public: true element: -
Parsing and Generation Element ExampleModel ! name: “Foo” package: “com.example”
public: true ExampleModel ! name: “Foo” package: “com.example” public: true element: GeneratedClass -
Parsing and Generation Element ExampleModel ! name: “Foo” package: “com.example”
public: true ExampleModel ! name: “Foo” package: “com.example” public: true element: GeneratedClass -
Bring Your Own Dependencies • Guava, Guava, Guava! • JavaWriter
• google/auto ‘common’ -
Bring Your Own Dependencies • Guava, Guava, Guava! • JavaWriter
• google/auto ‘common’ • AutoService (google/auto) -
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(); -
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(); -
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(); -
AutoService • Remember “ServiceLoader finds and creates” • AutoService generates
service files from annotations -
AutoService • Remember “ServiceLoader finds and creates” • AutoService generates
service files from annotations ! public class ExampleProcessor extends AbstractProcessor { // ... } -
AutoService • Remember “ServiceLoader finds and creates” • AutoService generates
service files from annotations ! public class ExampleProcessor extends AbstractProcessor { // ... } @AutoService(Processor.class) -
Testing and Debugging • Google to the rescue! • Guava,
Guava, Guava! -
Testing and Debugging • Google to the rescue! • Guava,
Guava, Guava! • compile-testing -
Testing and Debugging • Google to the rescue! • Guava,
Guava, Guava! • compile-testing • Truth -
final class TestProcessors { static Iterable<? extends Processor> exampleProcessors() {
return Collections.singletonList( new ExampleProcessor() ); } } -
String input = Joiner.on('\n').join( "package test;", "import com.example.Example;", "@Example", "public
class ExampleClass {", "}" ); -
String input = Joiner.on('\n').join( "package test;", "import com.example.Example;", "@Example", "public
class ExampleClass {", "}" ); JavaFileObject inputFile = JavaFileObjects.forSourceString("test.Test", input); -
JavaFileObject inputFile = JavaFileObjects.forSourceString("test.Test", input); String input = """\ package
test; import com.example.Example; @Example public class ExampleClass { } """ .stripIndent() -
String expected = Joiner.on('\n').join( "package test;", "import com.example.Other;", "@Other", "public
class GeneratedClass {", "}" ); -
String expected = Joiner.on('\n').join( "package test;", "import com.example.Other;", "@Other", "public
class GeneratedClass {", "}" ); JavaFileObject expectedFile = JavaFileObjects.forSourceString("test.GeneratedClass", expected); -
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 {", "}" ); -
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() ); } } -
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 {", "}" ); -
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 {", "}" ); -
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 -
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