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

CUSTOM ANNOTATION PROCESSORS FOR YOUR PRODUCTION CODE

CUSTOM ANNOTATION PROCESSORS FOR YOUR PRODUCTION CODE

The annotation processing tool (APT) was released into the JDK 5, but we did not put enough attention to it in order to exploit its potential. Today, annotation processors are part of many well known libraries that we use everyday in our professional code. On this talk we will learn how to code our custom annotation processor and to start looking at it as a very useful tool to reduce overhead in our production code. Because many recurrent problems could get solved using annotation processors!

3b38214ae9927683d69680cb6161852b?s=128

Jorge Castillo

October 22, 2016
Tweet

More Decks by Jorge Castillo

Other Decks in Programming

Transcript

  1. None
  2. None
  3. None
  4. Special mention to @jmpergar

  5. Let’ s move on!

  6. tooling automatize as much as you can

  7. why annotation processors?

  8. 1. From runtime to compile time 2. Avoid reflection 3.

    Eliminate boilerplate
  9. Added in JDK 5 APT and Rich public API on

    JDK 6 Standardized through JSR 269
  10. APT is a powerful tool

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

  12. not doable: 1. Inject code into / modify sources

  13. Reflective APIs: 1. Standard (Java Reflection API) 2. Mirror based

    ones (Java Mirror API) ObjectMirror carMirror = Reflection.reflect(myCar);
  14. before compiling, ServiceLoader runs all the processors using source files

    as input
  15. ¿how to register a processor?

  16. Option 1: static file declaring its path

  17. include qualified name of your processor class

  18. Option 2: AutoService library

  19. processing rounds

  20. creating a custom processor

  21. Extend AbstractProcessor and override some methods

  22. None
  23. get utility classes from processingEnvironment

  24. 4 phases per round

  25. 1. Scan for annotations 2. Static validation 3. Parse to

    meta-models 4. Code generation
  26. 1. Scan for annotations get a Set of annotated Elements

  27. 1. Executable Element (method) 2. Type Element (class) 3. Variable

    Element (fields, method params)
  28. 2. Static Validation get a Set of annotated Elements

  29. None
  30. None
  31. 3. Parse to meta-models create meta-models to increase simplicity

  32. None
  33. 4. Code generation JavaPoet: Based on specs

  34. None
  35. https://github.com/square/javapoet

  36. how to test to avoid compiling the whole project many

    times?
  37. Truth + compile-testing test validation code while developing it

  38. test code generation

  39. None
  40. base classes to improve readability

  41. None
  42. debug is possible at compile time

  43. 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 {
  44. • 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
  45. questions?