ANNOTATION PROCESSING
HOW IT WORKS
ANNOTATION PROCESSING
A BIT OF HISTORY
▸ Were first introduced in Java Language with the third
edition of the Java Language Specification (JLS)
▸ And introduced in Java 5
ANNOTATION PROCESSING
WHAT IS?
▸ Annotation processing lets you generate new Java files at
build time.
▸ There is no need to use reflection. Although the generated
Java files can use reflection, if you wish.
▸ Just cause performance degradation on build time.
ANNOTATION PROCESSING
HOW CAN I DO
▸ Android Studio doesn’t have build in support for
annotation processing.
▸ You need to use the android-apt Gradle plugin for your
compile code as build time dependency without including
it in the final apk.
ANNOTATION PROCESSING
▸ First: In your project's build.gradle you should use
▸ Second: Create a module Compiler
▸ Third: App’s build.gradle
ANNOTATION PROCESSING
▸ Finally: The compiler’s build.gradle
▸ plugin java instead plugin android. After all, this code
going to run on the machine. Note on an Android.
ANNOTATION PROCESSING
ABSTRACT PROCESSOR
▸ AbstractProcessor. Let’s have a look at the Processor API.
ANNOTATION PROCESSING
THE PROCESSOR IT SELF
▸ void init (ProcessingEnvironment)
▸ All annotation processor must have a empty constructor!
▸ This is special init() which is invoked by the annotation
processing tool
ANNOTATION PROCESSING
THE PROCESSOR IT SELF
▸ boolean process(Set<? extends TypeElement>
annotations, RoundEnvironment env)
▸ This is kind of main() method of each processor
▸ Here you write code for spanning, processing annotation
and generating java files.
ANNOTATION PROCESSING
LIVE CODE
▸ Let’s generate automatic getter for
our fields.
ANNOTATION PROCESSING
OK, SHOW ME EXEMPLES
MUCH MORE…
ANNOTATION PROCESSING
ABOUT ME
▸ Felipe Theodoro Gonçalves

Android Developer at Elo7



@ftgoncalves

ftgoncalves@gmail.com
▸ Slack

Android Dev BR / Android Study Group
http://brianattwell.com/android-annotation-
processing-pojo-string-generator/
http://hannesdorfmann.com/annotation-
processing/annotationprocessing101
Referências

Annotation processing

  • 1.
  • 2.
    ANNOTATION PROCESSING A BITOF HISTORY ▸ Were first introduced in Java Language with the third edition of the Java Language Specification (JLS) ▸ And introduced in Java 5
  • 3.
    ANNOTATION PROCESSING WHAT IS? ▸Annotation processing lets you generate new Java files at build time. ▸ There is no need to use reflection. Although the generated Java files can use reflection, if you wish. ▸ Just cause performance degradation on build time.
  • 4.
    ANNOTATION PROCESSING HOW CANI DO ▸ Android Studio doesn’t have build in support for annotation processing. ▸ You need to use the android-apt Gradle plugin for your compile code as build time dependency without including it in the final apk.
  • 5.
    ANNOTATION PROCESSING ▸ First:In your project's build.gradle you should use ▸ Second: Create a module Compiler ▸ Third: App’s build.gradle
  • 6.
    ANNOTATION PROCESSING ▸ Finally:The compiler’s build.gradle ▸ plugin java instead plugin android. After all, this code going to run on the machine. Note on an Android.
  • 7.
    ANNOTATION PROCESSING ABSTRACT PROCESSOR ▸AbstractProcessor. Let’s have a look at the Processor API.
  • 8.
    ANNOTATION PROCESSING THE PROCESSORIT SELF ▸ void init (ProcessingEnvironment) ▸ All annotation processor must have a empty constructor! ▸ This is special init() which is invoked by the annotation processing tool
  • 9.
    ANNOTATION PROCESSING THE PROCESSORIT SELF ▸ boolean process(Set<? extends TypeElement> annotations, RoundEnvironment env) ▸ This is kind of main() method of each processor ▸ Here you write code for spanning, processing annotation and generating java files.
  • 10.
    ANNOTATION PROCESSING LIVE CODE ▸Let’s generate automatic getter for our fields.
  • 11.
    ANNOTATION PROCESSING OK, SHOWME EXEMPLES MUCH MORE…
  • 12.
    ANNOTATION PROCESSING ABOUT ME ▸Felipe Theodoro Gonçalves
 Android Developer at Elo7
 
 @ftgoncalves
 ftgoncalves@gmail.com ▸ Slack
 Android Dev BR / Android Study Group http://brianattwell.com/android-annotation- processing-pojo-string-generator/ http://hannesdorfmann.com/annotation- processing/annotationprocessing101 Referências