Slide 1

Slide 1 text

Marvel of Annotation by Alexey Buzdin Preprocessing

Slide 2

Slide 2 text

@AlexeyBuzdin GDGRiga.lv JUG.lv RigaDevDay.lv Citadele.lv

Slide 3

Slide 3 text

๏ What? ๏ Why? ๏ How? ๏ Dagger ๏ Lombok ๏ MapStruct

Slide 4

Slide 4 text

๏ What? ๏ Why? ๏ How? ๏ Dagger ๏ MapStruct ๏ Lombok

Slide 5

Slide 5 text

“Developer loves to write code”

Slide 6

Slide 6 text

“Developer loves to write code” - noone ever

Slide 7

Slide 7 text

“Developer loves complex tasks”

Slide 8

Slide 8 text

“Developer loves complex tasks” - probably you

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

CRUD

Slide 11

Slide 11 text

CRUD - api - type: XML, JSON - resources - /clients - access: R - entity clientId - full name - email - /payments - entity paymentId clientId | link - amount - db: jdbc:mysql://localhost:3306/crudapp

Slide 12

Slide 12 text

CRUD - api - type: XML, JSON - resources - /clients - access: R - entity clientId - full name - email - /payments - entity paymentId clientId | link - amount - db: jdbc:mysql://localhost:3306/crudapp Authentication?

Slide 13

Slide 13 text

CRUD - api - type: XML, JSON - resources - /clients - access: R - entity clientId - full name - email - /payments - entity paymentId clientId | link - amount - db: jdbc:mysql://localhost:3306/crudapp - ldap: ldap://ldap.example.com/dc=example,dc=com

Slide 14

Slide 14 text

never let go of your D R E A M S

Slide 15

Slide 15 text

In Practice

Slide 16

Slide 16 text

In Practice Routing XML, JSON Building Response

Slide 17

Slide 17 text

public class SimpleServlet extends GenericServlet {
 
 public void service(ServletRequest req, ServletResponse res)
 throws ServletException, IOException {
 // do something in here
 }
 } Generic Servlet

Slide 18

Slide 18 text

In Practice Routing XML, JSON Building Response

Slide 19

Slide 19 text

In Practice Map Validate Routing XML, JSON Building Response

Slide 20

Slide 20 text

In Practice Construct Query Map Validate Routing XML, JSON Building Response

Slide 21

Slide 21 text

Reflection and Runtime Code Generation Everywhere! Spring, Hibernate, GSON, Jersy, Dozer, Guice, Weld, BeanValidation etc…

Slide 22

Slide 22 text

Reflection - Slow?

Slide 23

Slide 23 text

Reflection - Slow? http://docs.oracle.com/javase/tutorial/reflect/index.html Performance Overhead Because reflection involves types that are dynamically resolved, certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts, and should be avoided in sections of code which are called frequently in performance-sensitive applications.

Slide 24

Slide 24 text

Reflection - Slow? http://docs.oracle.com/javase/tutorial/reflect/index.html Performance Overhead Because reflection involves types that are dynamically resolved, certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts, and should be avoided in sections of code which are called frequently in performance-sensitive applications.

Slide 25

Slide 25 text

Reflection - Slow? http://docs.oracle.com/javase/tutorial/reflect/index.html Performance Overhead Because reflection involves types that are dynamically resolved, certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts, and should be avoided in sections of code which are called frequently in performance-sensitive applications.

Slide 26

Slide 26 text

Join the Dark Side: We have our own VM!

Slide 27

Slide 27 text

“In NYTimes GSON costed - 700ms startup delay.” http://blog.nimbledroid.com/2016/02/23/slow-Android-reflection.html Recommendation: understand what you're getting into when using reflection (or libraries that use reflection). In particular, do not use reflective type adapters to serialize or deserialize Java objects. - Jake Wharton

Slide 28

Slide 28 text

Code Generation? https://zeroturnaround.com/rebellabs/how-to-make-java-more-dynamic-with-runtime-code-generation/

Slide 29

Slide 29 text

Code Generation? https://zeroturnaround.com/rebellabs/how-to-make-java-more-dynamic-with-runtime-code-generation/ Generates Java Bytecode at Runtime

Slide 30

Slide 30 text

Code Generation? https://zeroturnaround.com/rebellabs/how-to-make-java-more-dynamic-with-runtime-code-generation/ Generates Java Bytecode at Runtime Not always ok

Slide 31

Slide 31 text

Code Generation? https://zeroturnaround.com/rebellabs/how-to-make-java-more-dynamic-with-runtime-code-generation/ GWT, Android, j2objc, RoboVM, code transpilers Doesn’t work

Slide 32

Slide 32 text

Rafael Winterhalter @rafaelcodes How to make Java more dynamic with Runtime Code Generation https://zeroturnaround.com/rebellabs/how-to-make-java-more-dynamic-with-runtime-code-generation/

Slide 33

Slide 33 text

Annotation Processing

Slide 34

Slide 34 text

Annotation Processing A tool build in javac for scanning and processing annotations at compile time

Slide 35

Slide 35 text

Annotation Processing A tool build in javac for scanning and processing annotations at compile time $ javac -cp target/apt-demo-1.0-SNAPSHOT.jar Test.java

Slide 36

Slide 36 text

How? MyProcessor.jar - com - example - MyProcessor.class - META-INF - services - javax.annotation.processing.Processor com.example.MyProcessor com.foo.OtherProcessor net.blabla.SpecialProcessor javax.annotation.processing.Processor javac will run the process in separate JVM

Slide 37

Slide 37 text

How? public class MyProcessor extends AbstractProcessor { @Override public synchronized void init(ProcessingEnvironment env){ } @Override public boolean process( Set annotations, RoundEnvironment env) { } @Override public Set getSupportedAnnotationTypes() { } @Override public SourceVersion getSupportedSourceVersion() { } } http://hannesdorfmann.com/annotation-processing/annotationprocessing101/

Slide 38

Slide 38 text

How? public class MyProcessor extends AbstractProcessor { public synchronized void init(ProcessingEnvironment env){ } @Override public boolean process( Set annotations, RoundEnvironment env) { } @Override public Set getSupportedAnnotationTypes() { } @Override public SourceVersion getSupportedSourceVersion() { } } http://hannesdorfmann.com/annotation-processing/annotationprocessing101/

Slide 39

Slide 39 text

How? http://hannesdorfmann.com/annotation-processing/annotationprocessing101/ public class MyProcessor extends AbstractProcessor { public synchronized void init(ProcessingEnvironment env){ } public boolean process( Set annotations, RoundEnvironment env) { } @Override public Set getSupportedAnnotationTypes() { } @Override public SourceVersion getSupportedSourceVersion() { } }

Slide 40

Slide 40 text

How? http://hannesdorfmann.com/annotation-processing/annotationprocessing101/ public class MyProcessor extends AbstractProcessor { public synchronized void init(ProcessingEnvironment env){ } public boolean process( Set annotations, RoundEnvironment env) { } public Set getSupportedAnnotationTypes() { } @Override public SourceVersion getSupportedSourceVersion() { } }

Slide 41

Slide 41 text

How? http://hannesdorfmann.com/annotation-processing/annotationprocessing101/ public class MyProcessor extends AbstractProcessor { public synchronized void init(ProcessingEnvironment env){ } public boolean process( Set annotations, RoundEnvironment env) { } public Set getSupportedAnnotationTypes() { } public SourceVersion getSupportedSourceVersion() { } }

Slide 42

Slide 42 text

How? MyProcessor.jar - com - example - MyProcessor.class - META-INF - services - javax.annotation.processing.Processor com.example.MyProcessor com.foo.OtherProcessor net.blabla.SpecialProcessor javax.annotation.processing.Processor javac will run the process in separate JVM

Slide 43

Slide 43 text

How? MyProcessor.jar - com - example - MyProcessor.class - META-INF - services - javax.annotation.processing.Processor com.example.MyProcessor com.foo.OtherProcessor net.blabla.SpecialProcessor javax.annotation.processing.Processor javac will run the process in separate JVM

Slide 44

Slide 44 text

https://github.com/google/auto/tree/master/service package foo.bar; import javax.annotation.processing.Processor; @AutoService(Processor.class) final class MyProcessor extends Processor { // … } AutoService

Slide 45

Slide 45 text

Example public class PizzaStore { public Meal order(String mealName) { if ("Margherita".equals(mealName)) return new MargheritaPizza(); if ("Calzone".equals(mealName)) return new CalzonePizza(); if ("Tiramisu".equals(mealName)) return new Tiramisu(); throw new IllegalArgumentException("Unknown meal '" + mealName + "'"); } }

Slide 46

Slide 46 text

Automatically Detect All Meals from ClassPath and Register them What do we Want?

Slide 47

Slide 47 text

What do we Want? public class PizzaStore { private MealFactory factory = new MealFactory(); public Meal order(String mealName) { return factory.create(mealName); } }

Slide 48

Slide 48 text

@Target(ElementType.TYPE) @Retention(RetentionPolicy.CLASS) public @interface Factory { Class type(); String id(); } @Factory( id = "Margherita", type = Meal.class ) public class MargheritaPizza implements Meal { @Override public float getPrice() { return 6f; } }

Slide 49

Slide 49 text

How? public class MyProcessor extends AbstractProcessor { public synchronized void init(ProcessingEnvironment env){ } public boolean process( Set annotations, RoundEnvironment env) { } public Set getSupportedAnnotationTypes() { } public SourceVersion getSupportedSourceVersion() { } }

Slide 50

Slide 50 text

@Override public Set getSupportedAnnotationTypes() { Set annotataions = new LinkedHashSet(); annotataions.add(Factory.class.getCanonicalName()); return annotataions; } @Override public SourceVersion getSupportedSourceVersion() { return SourceVersion.latestSupported(); }

Slide 51

Slide 51 text

@Override public synchronized void init(ProcessingEnvironment env) { super.init(env); typeUtils = env.getTypeUtils(); elementUtils = env.getElementUtils(); filer = env.getFiler(); messager = env.getMessager(); }

Slide 52

Slide 52 text

@Override public boolean process(Set annotations, RoundEnvironment env) { // Itearate over all @Factory annotated elements for (Element elem : env.getElementsAnnotatedWith(Factory.class)){ ... } }

Slide 53

Slide 53 text

package com.example; // PackageElement public class Foo { // TypeElement private int a; // VariableElement private Foo other; // VariableElement public Foo () {} // ExecuteableElement public void setA ( // ExecuteableElement int newA // TypeElement ) {} } javax.lang.model.TypeElement Gilad Bracha and David Ungar. Mirrors

Slide 54

Slide 54 text

@Override public boolean process(Set annotations, RoundEnvironment env) { // Itearate over all @Factory annotated elements for (Element elem : env.getElementsAnnotatedWith(Factory.class)){ ... } }

Slide 55

Slide 55 text

@Override public boolean process(Set annotations, RoundEnvironment env) { // Itearate over all @Factory annotated elements for (Element elem : env.getElementsAnnotatedWith(Factory.class)){ if (annotatedElement.getKind() != ElementKind.CLASS) { messager.printMessage(Diagnostic.Kind.ERROR, “Ooops!”, elem); return true; } ... } }

Slide 56

Slide 56 text

What do we Want? public class PizzaStore { public Meal order(String mealName) { if ("Margherita".equals(mealName)) return new MargheritaPizza(); if ("Calzone".equals(mealName)) return new CalzonePizza(); if ("Tiramisu".equals(mealName)) return new Tiramisu(); throw new IllegalArgumentException("Unknown meal '" + mealName + "'"); } }

Slide 57

Slide 57 text

What do we Want? public class PizzaStore { public Meal order(String mealName) { if ("Margherita".equals(mealName)) return new MargheritaPizza(); if ("Calzone".equals(mealName)) return new CalzonePizza(); if ("Tiramisu".equals(mealName)) return new Tiramisu(); throw new IllegalArgumentException("Unknown meal '" + mealName + "'"); } }

Slide 58

Slide 58 text

What do we Want? public class PizzaStore { public Meal order(String mealName) { if ("Margherita".equals(mealName)) return new MargheritaPizza(); if ("Calzone".equals(mealName)) return new CalzonePizza(); if ("Tiramisu".equals(mealName)) return new Tiramisu(); throw new IllegalArgumentException("Unknown meal '" + mealName + "'"); } } ID FactoryType ClassType

Slide 59

Slide 59 text

@Override public boolean process(Set annotations, RoundEnvironment env) { // Itearate over all @Factory annotated elements for (Element elem : env.getElementsAnnotatedWith(Factory.class)){ if (annotatedElement.getKind() != ElementKind.CLASS) { messager.printMessage(Diagnostic.Kind.ERROR, “Ooops!”, elem); return true; } … // Process Element } }

Slide 60

Slide 60 text

public class FactoryAnnotatedClass { private TypeElement annotatedClassElement; private String qualifiedClassName; // Class Canonical name private String simpleTypeName; // Class name private String id; // “Margherita” public FactoryAnnotatedClass(TypeElement classElement) throws Exception { Factory annotation = classElement.getAnnotation(Factory.class); id = annotation.id(); … } } Then Element is a TypeElement

Slide 61

Slide 61 text

try { Class clazz = annotation.type(); qualifiedClassName = clazz.getCanonicalName(); simpleClassName = clazz.getSimpleName(); } catch (MirroredTypeException mte) { DeclaredType classTypeMirror = (DeclaredType) mte.getTypeMirror(); TypeElement classTypeElement = (TypeElement) classTypeMirror.asElement(); qualifiedClassName = classTypeElement.getQualifiedName().toString(); simpleClassName = classTypeElement.getSimpleName().toString(); } Getting the Data

Slide 62

Slide 62 text

Writing an Output String src = public class PizzaFactory { public Meal create(String id) { if ("Margherita".equals(id)) return new my.MargheritaPizza(); if ("Calzone".equals(id)) return new my.CalzonePizza(); if ("Tiramisu".equals(id)) return new my.Tiramisu(); throw new IllegalArgumentException(“Unknown " + id); } }

Slide 63

Slide 63 text

Writing an Output String src = “public class PizzaFactory { \n public ”+T+“create(String id) { \n“; for (FactoryAnnotatedClass clazz : clazzes) { String id = clazz.getId(); String clazzName = clazz.getQualifiedClassName(); src+=“if (“ + id + ”.equals(id)) return new “+clazzName+”(); \n”; } src+=“throw new IllegalArgumentException(“Unknown " + id); \n” + “} \n” +“} \n”;

Slide 64

Slide 64 text

Writing an Output String src = “public class PizzaFactory { \n public ”+T+“create(String id) { \n“; for (FactoryAnnotatedClass clazz : clazzes) { String id = clazz.getId(); String clazzName = clazz.getQualifiedClassName(); src+=“if (“ + id + ”.equals(id)) return new “+clazzName+”(); \n”; } src+=“throw new IllegalArgumentException(“Unknown " + id); \n” + “} \n” +“} \n”; JavaFileObject jfo = processingEnv.getFiler().createSourceFile(“MyClass"); new BufferedWriter(jfo.openWriter()).append(src);

Slide 65

Slide 65 text

public void generateCode(Elements elementUtils, Filer filer)throws Exception{ MethodSpec.Builder method = MethodSpec.methodBuilder("create") .addModifiers(Modifier.PUBLIC) .addParameter(String.class, "id") .returns(TypeName.get(superClassName.asType())); // For Each Found @Factory method.beginControlFlow("if ($S.equals(id))", item.getId()) .addStatement("return new $L()”, item.getTypeElement().getQualifiedName().toString()) .endControlFlow(); TypeSpec typeSpec = TypeSpec.classBuilder(factoryClassName) .addMethod(method.build()).build(); JavaFile.builder(packageName, typeSpec).build().writeTo(filer); } https://github.com/square/javapoet JavaPoet

Slide 66

Slide 66 text

MethodSpec.Builder method = MethodSpec.methodBuilder("create") .addModifiers(Modifier.PUBLIC) .addParameter(String.class, "id") .returns(TypeName.get(superClassName.asType())); String src = public class PizzaFactory { public Meal create(String id) { if ("Margherita".equals(id)) return new my.MargheritaPizza(); if ("Calzone".equals(id)) return new my.CalzonePizza(); if ("Tiramisu".equals(id)) return new my.Tiramisu(); … } }

Slide 67

Slide 67 text

for (FactoryAnnotatedClass item : clazzes) { method.beginControlFlow("if ($S.equals(id))", item.getId()) .addStatement("return new $L()”, item.getTypeElement().getQualifiedName().toString()) .endControlFlow(); } String src = public class PizzaFactory { public Meal create(String id) { if ("Margherita".equals(id)) return new my.MargheritaPizza(); if ("Calzone".equals(id)) return new my.CalzonePizza(); if ("Tiramisu".equals(id)) return new my.Tiramisu(); … } }

Slide 68

Slide 68 text

TypeSpec typeSpec = TypeSpec.classBuilder(factoryClassName) .addMethod(method.build()).build(); JavaFile.builder(packageName, typeSpec).build().writeTo(filer); } https://github.com/square/javapoet String src = public class PizzaFactory { public Meal create(String id) { if ("Margherita".equals(id)) return new my.MargheritaPizza(); if ("Calzone".equals(id)) return new my.CalzonePizza(); if ("Tiramisu".equals(id)) return new my.Tiramisu(); … } }

Slide 69

Slide 69 text

Processing Rounds

Slide 70

Slide 70 text

Google’s Compile Testing https://github.com/google/compile-testing Compiler.javac() .withProcessors(new MyProcessor()) .compile(JavaFileObjects.forResource("com/example/MyClass")) .generatedSourceFile("MyGeneratedClass") .isPresent()

Slide 71

Slide 71 text

Annotation Processing A tool build in javac for scanning and processing annotations at compile time

Slide 72

Slide 72 text

Dependency Injection

Slide 73

Slide 73 text

Dagger 2 The fastest Java DI Framework! https://google.github.io/dagger/

Slide 74

Slide 74 text

import javax.inject.Inject; class CoffeeMaker { @Inject Heater heater; @Inject Pump pump; ... } Dagger 2

Slide 75

Slide 75 text

@Component(modules = DripCoffeeModule.class) interface CoffeeShop { CoffeeMaker maker(); } @Module class DripCoffeeModule { @Provides static Heater provideHeater() { return new ElectricHeater(); } @Provides static Pump providePump(Thermosiphon pump) { return pump; } }

Slide 76

Slide 76 text

Dagger 2 CoffeeShop coffeeShop = DaggerCoffeeShop.create(); import javax.inject.Inject; class CoffeeMaker { @Inject Heater heater; @Inject Pump pump; ... }

Slide 77

Slide 77 text

Dagger 2 ๏ Singletons and Scoped Bindings ๏ Lazy injections ๏ Provider injections ๏ Qualifiers

Slide 78

Slide 78 text

Dagger 2 The fastest Java DI Framework! https://google.github.io/dagger/

Slide 79

Slide 79 text

MapStruct Bean Mapping at Compile Time http://mapstruct.org/

Slide 80

Slide 80 text

public class Car { private String make; private int numberOfSeats; private CarType type; //constructor, getters, setters etc. }

Slide 81

Slide 81 text

@Mapper public interface CarMapper { CarMapper INSTANCE = Mappers.getMapper( CarMapper.class ); @Mappings({ @Mapping(source = "make", target = "manufacturer"), @Mapping(source = "numberOfSeats", target = "seatnt") }) CarDto carToCarDto(Car car); }

Slide 82

Slide 82 text

@Mapper public interface CarMapper { CarMapper INSTANCE = Mappers.getMapper( CarMapper.class ); @Mappings({ @Mapping(source = "make", target = "manufacturer"), @Mapping(source = "numberOfSeats", target = "seatCos") }) CarDto carToCarDto(Car car); }

Slide 83

Slide 83 text

@Mapper public interface CarMapper { CarMapper INSTANCE = Mappers.getMapper( CarMapper.class ); @Mappings({ @Mapping(source = "make", target = "manufacturer"), @Mapping(source = "numberOfSeats", target = "seats") }) CarDto carToCarDto(Car car); }

Slide 84

Slide 84 text

MapStruct http://mapstruct.org/ ๏ Nested mappings ๏ Updating existing bean instances

Slide 85

Slide 85 text

No content

Slide 86

Slide 86 text

Lombok Spice up your Java https://projectlombok.org/

Slide 87

Slide 87 text

@ToString @ToString(exclude="id") public class ToStringExample { private static final int STATIC_VAR = 10; private String name; private Shape shape = new Square(5, 10); private String[] tags; private int id; }

Slide 88

Slide 88 text

@EqualsAndHashCode @EqualsAndHashCode(exclude={"id", "shape"}) public class EqualsAndHashCodeExample { private transient int transientVar = 10; private String name; private double score; private Shape shape = new Square(5, 10); private String[] tags; private int id; }

Slide 89

Slide 89 text

@RequiredArgsConstructor @RequiredArgsConstructor(staticName = "of") @AllArgsConstructor(access = AccessLevel.PROTECTED) public class ConstructorExample { private int x, y; @NonNull private T description; @NoArgsConstructor public static class NoArgsExample { @NonNull private String field; } }

Slide 90

Slide 90 text

@Data & @Value @Data public class PoJo { private String name; private double score; private String[] tags; private int id; } @Value public class PoJo { private String name; private double score; private String[] tags; private int id; }

Slide 91

Slide 91 text

public String example() { val example = new ArrayList(); example.add("Hello, World!"); val foo = example.get(0); return foo.toLowerCase(); }

Slide 92

Slide 92 text

val !!11one public String example() { val example = new ArrayList(); example.add("Hello, World!"); val foo = example.get(0); return foo.toLowerCase(); }

Slide 93

Slide 93 text

@ExtensionMethod class Extensions { public static String toTitleCase(String in) { if (in.isEmpty()) return in; return "" + Character.toTitleCase(in.charAt(0)) + in.substring(1).toLowerCase(); } } @ExtensionMethod({java.util.Arrays.class, Extensions.class}) public class ExtensionMethodExample { public String test() {return “hELlO,WORlD!”.toTitleCase();} }

Slide 94

Slide 94 text

lombok.fieldDefaults.defaultPrivate = true Lombok.config

Slide 95

Slide 95 text

Lombok Spice up your Java https://projectlombok.org/ ๏@Builder ๏@Log ๏@Delegate

Slide 96

Slide 96 text

• https://github.com/bluelinelabs/LoganSquare • https://github.com/greenrobot/EventBus • https://www.jooq.org/ • … and more

Slide 97

Slide 97 text

Conclusion Annotation Processing

Slide 98

Slide 98 text

Conclusion Powerful Fast Sometimes Brittle Annotation Processing

Slide 99

Slide 99 text

Conclusion Powerful Fast Sometimes Brittle Annotation Processing

Slide 100

Slide 100 text

Conclusion Powerful Fast Sometimes Brittle Annotation Processing

Slide 101

Slide 101 text

Powerful Fast Sometimes a Life Saver Conclusion Powerful Fast Sometimes Brittle Annotation Processing

Slide 102

Slide 102 text

“Use it wisely we must!”

Slide 103

Slide 103 text

Q&A Thank You! @AlexeyBuzdin Follow me at I Post Regular News and Updates about Java, Android and DevOps