Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Special mention to @jmpergar

Slide 5

Slide 5 text

Let’ s move on!

Slide 6

Slide 6 text

tooling automatize as much as you can

Slide 7

Slide 7 text

why annotation processors?

Slide 8

Slide 8 text

1. From runtime to compile time 2. Avoid reflection 3. Eliminate boilerplate

Slide 9

Slide 9 text

Added in JDK 5 APT and Rich public API on JDK 6 Standardized through JSR 269

Slide 10

Slide 10 text

APT is a powerful tool

Slide 11

Slide 11 text

doable: 1. Scan sources for annotations 2. Generate source files

Slide 12

Slide 12 text

not doable: 1. Inject code into / modify sources

Slide 13

Slide 13 text

Reflective APIs: 1. Standard (Java Reflection API) 2. Mirror based ones (Java Mirror API) ObjectMirror carMirror = Reflection.reflect(myCar);

Slide 14

Slide 14 text

before compiling, ServiceLoader runs all the processors using source files as input

Slide 15

Slide 15 text

¿how to register a processor?

Slide 16

Slide 16 text

Option 1: static file declaring its path

Slide 17

Slide 17 text

include qualified name of your processor class

Slide 18

Slide 18 text

Option 2: AutoService library

Slide 19

Slide 19 text

processing rounds

Slide 20

Slide 20 text

creating a custom processor

Slide 21

Slide 21 text

Extend AbstractProcessor and override some methods

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

get utility classes from processingEnvironment

Slide 24

Slide 24 text

4 phases per round

Slide 25

Slide 25 text

1. Scan for annotations 2. Static validation 3. Parse to meta-models 4. Code generation

Slide 26

Slide 26 text

1. Scan for annotations get a Set of annotated Elements

Slide 27

Slide 27 text

1. Executable Element (method) 2. Type Element (class) 3. Variable Element (fields, method params)

Slide 28

Slide 28 text

2. Static Validation get a Set of annotated Elements

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

3. Parse to meta-models create meta-models to increase simplicity

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

4. Code generation JavaPoet: Based on specs

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

https://github.com/square/javapoet

Slide 36

Slide 36 text

how to test to avoid compiling the whole project many times?

Slide 37

Slide 37 text

Truth + compile-testing test validation code while developing it

Slide 38

Slide 38 text

test code generation

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

base classes to improve readability

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

debug is possible at compile time

Slide 43

Slide 43 text

1. divide your library in separate modules (compiler, public api) 2. include other libraries just for compile time (provided) 3. war plugin for pure java modules public final recommendations {

Slide 44

Slide 44 text

● AutoService https://github.com/google/auto/tree/master/service ● Truth https://github.com/google/truth ● Compile-Testing https://github.com/google/compile-testing ● JavaPoet https://github.com/square/javapoet ● Great threads about creating your custom processor http://hannesdorfmann.com/annotation-processing/annotationprocessing101 ● https://deors.wordpress.com/2011/09/26/annotation-types/ https://deors.wordpress.com/2011/10/08/annotation-processors/ https://deors.wordpress.com/2011/10/31/annotation-generators/ ● Some official (SUN) info about both types of reflective APIs http://bracha.org/mirrors.pdf Resources and recommended reads

Slide 45

Slide 45 text

questions?