Slide 1

Slide 1 text

ANNOTATION PROCESSING Generate code, eliminate boilerplate @ Rygel Louv

Slide 2

Slide 2 text

Rygel Louv Android dev Makeba.Money GitHub: @Rygelouv Twitter: @Rygellouv Medium: @rygel

Slide 3

Slide 3 text

What do you really know About Wakanda Annotation Processing ?

Slide 4

Slide 4 text

WHAT IS ANNOTATION PROCESSING ? Part of Java compiler Java 5 Improved in Java 6 Scans annotations Process annotations At compile time

Slide 5

Slide 5 text

CODE GENERATION ? Boilerplate problem Generate code .java files or .kt files

Slide 6

Slide 6 text

WHAT ARE THE BENEFITS OF THAT ? Code generator written once Trustable Eliminate boilerplate code Improve productivity Don’t repeat yourself

Slide 7

Slide 7 text

YOU USE IT EVERYDAY! Dagger2 Butterknife Android Annotation library Room persistence library And many more….

Slide 8

Slide 8 text

BUILD AP: WHICH COMPONENTS ? Annotation Processor APT: Annotation Processing Tool android-apt App code with annotated element For Android annotationProcessor kapt

Slide 9

Slide 9 text

STEPS OF AP: HOW DOES IT WORK ? Build starts in javac Starts all annotation processors which are not executed Loop over annotated elements inside the processor Find annotated classes, methods, fields etc. Generated new classes with metadata of founded classes Create new files and write the generated string as class Compiler checks if all annotation processors are executed. If not start next ROUND

Slide 10

Slide 10 text

STEPS OF AP: WHAT ARE ROUNDS FOR APT ?

Slide 11

Slide 11 text

STEPS OF AP: WHAT ARE ROUNDS FOR APT ?

Slide 12

Slide 12 text

STEPS OF AP: WHAT ARE ROUNDS FOR APT ?

Slide 13

Slide 13 text

STEPS OF AP: WHAT ARE ROUNDS FOR APT ?

Slide 14

Slide 14 text

STEPS OF AP: WHAT ARE ROUNDS FOR APT ?

Slide 15

Slide 15 text

BUILD OUR OWN AP: MODULES Create a new project: Android, Java, or Kotlin Create annotation module : Java/Kotlin Create processor module: Java/Kotlin

Slide 16

Slide 16 text

BUILD OUR OWN AP: DEPENDENCIES Annotation module Processor module Application module

Slide 17

Slide 17 text

BUILD OUR OWN AP: CUSTOM ANNOTATIONS Java Kotlin

Slide 18

Slide 18 text

BUILD OUR OWN AP: THE PROCESSOR

Slide 19

Slide 19 text

BUILD OUR OWN AP: THE PROCESSOR Init(): gives tools (Messager and Filer) getSupportedAnnotationType(): returns only our custom annotations getSupportedSourceVersion(): Always return latest java version process(): This were the magic happen. Starts rounding and gives all annotated elements

Slide 20

Slide 20 text

BUILD OUR OWN AP: THE PROCESSOR In the processor you loop over Elements in order to make calculations

Slide 21

Slide 21 text

BUILD OUR OWN AP: TELL JAVAC ABOUT YOUR PROCESSOR Register the processor for it to be found by ServiceLoader Use AutoService

Slide 22

Slide 22 text

BUILD OUR OWN AP: THE RULES Cannot modify existing files Runs in the compile time in its own jvm instance Dependencies are its own and do not impact the main project

Slide 23

Slide 23 text

CODE GENERATION JavaPoet Kotlin Poet Kotlin annotation processing is a bit tricky Temporary solution: Generate .java file in kotlin

Slide 24

Slide 24 text

CODE GENERATION: JAVAPOET AND KOTLINPOET

Slide 25

Slide 25 text

Demo

Slide 26

Slide 26 text

THANK YOU! Wakanda forever!