Slide 1

Slide 1 text

Annotation Processing Building a Java Annotation Processor Corneil du Plessis

Slide 2

Slide 2 text

Agenda • What are annotation processors? • How can we use annotation processors? • How do we build an annotation processor?

Slide 3

Slide 3 text

Java Annotations – A Primer • Annotations are Class metadata • Annotations can be used at runtime. • Annotations can be used at compile time.

Slide 4

Slide 4 text

Annotation Processors – Why? • Generate source code or resources. • Modify existing source. • Analyse existing code to provide diagnostic feedback.

Slide 5

Slide 5 text

The Problem

Slide 6

Slide 6 text

Solution

Slide 7

Slide 7 text

Example of Use

Slide 8

Slide 8 text

Performing the check

Slide 9

Slide 9 text

How – RequiresStatic annotation

Slide 10

Slide 10 text

RequiresStaticSupport.checkClass

Slide 11

Slide 11 text

RequiresStaticSupport.checkClass continued

Slide 12

Slide 12 text

RequiresStaticSupport.checkClass continued

Slide 13

Slide 13 text

RequiresStaticSupport.checkClass continued

Slide 14

Slide 14 text

RequiresStaticProcessor

Slide 15

Slide 15 text

Annotation Processor META-INF • File named META-INF/services/javax.annotation.processing.Processor • Content is fully qualifed names of annotation processors.

Slide 16

Slide 16 text

RequestStaticProcessor Source Let’s look at the code.

Slide 17

Slide 17 text

Debugging your Annotation Processors • Add Compiler option: -Xdebug - agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=7777 • Debug remote (Attach) with options: -Xdebug - agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=7777 • Set break point on process method. • Build will pause so that you can attach debugger.

Slide 18

Slide 18 text

Finally • Contact: @corneil • Source Code - https://github.com/corneil/static-requirements • Resources • https://www.javacodegeeks.com/2015/09/java-annotation-proc essors.html • http://hannesdorfmann.com/annotation-processing/annotation processing101 Questions?

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content