15. @AutoService(Processor.class)
public class ExampleAnnotationProcessor extends AbstractProcessor {
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
// for all annotations to be processed:
// 1. Find elements for a given annotation to be processed
// 2. Perform validation (produce compiler errors, warnings, etc)
// 3. Process
// Finally, generate/write code
return true; //Claim all annotations in this round
}
}
Processing Steps
@jameskbride
20. TypeSpec.classBuilder(/* class/interface name */)
.addSuperinterface(/* interface name */)
.addModifiers(/* array of modifiers */)
.addField(/* FieldSpec */)
.addMethod(/* First MethodSpec */)
.addMethod(/* ...nth MethodSpec */)
.build();
Generate a Class
@jameskbride
21. MethodSpec
.methodBuilder(/* Method name */)
.addModifiers(/* Array of modifiers */)
.returns(/* return TypeName */)
.addCode(/* CodeBlock */)
.build()
Generate a Method
@jameskbride
25. • The Alchemist by William Fettes Douglas - https://commons.wikimedia.org/wiki/File:William_Fettes_Douglas_-
_The_Alchemist.jpg
• Alchemist’s Laboratory, 1595 - https://commons.wikimedia.org/wiki/File:Amphitheatrum_sapientiae_aeternae_-
_Alchemist%27s_Laboratory.jpg
• Medical miscellany, anatomical illustration showing the veins. Circa 1292 - https://commons.wikimedia.org/wiki/File:
13th_century_anatomical_illustration.jpg
• Anatomy lesson of Dr. Willem van der Meer by Unknown, 1617 - https://commons.wikimedia.org/wiki/
File:Michiel_Jansz_van_Mierevelt_-_Anatomy_lesson_of_Dr._Willem_van_der_Meer.jpg
• An Experiment on a Bird in an Air Pump by Joseph Wright of Derby, 1768 - https://commons.wikimedia.org/wiki/
File:An_Experiment_on_a_Bird_in_an_Air_Pump_by_Joseph_Wright_of_Derby,_1768.jpg
• Louis Pasteur, 1885 - https://commons.wikimedia.org/wiki/File:Albert_Edelfelt_-_Louis_Pasteur_-_1885.jpg
• Anatomy High-tech - http://www.publicdomainpictures.net/view-image.php?image=42723
References
@jameskbride