marvel of annotation preprocessing in java by alexey buzdin
TRANSCRIPT
![Page 1: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/1.jpg)
Marvel of Annotationby Alexey BuzdinPreprocessing
![Page 2: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/2.jpg)
@AlexeyBuzdin
GDGRiga.lv RigaDevDay.lv
![Page 3: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/3.jpg)
๏ What? ๏ Why? ๏ How? ๏ Dagger ๏ Lombok ๏ MapStruct ๏ Retrofit
![Page 4: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/4.jpg)
๏ What? ๏ Why? ๏ How? ๏ Dagger ๏ MapStruct ๏ Retrofit ๏ Lombok
![Page 5: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/5.jpg)
“Developer loves to write code”
![Page 6: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/6.jpg)
“Developer loves to write code”
- noone ever
![Page 7: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/7.jpg)
“Developer loves complex tasks”
![Page 8: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/8.jpg)
“Developer loves complex tasks”
- probably you
![Page 9: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/9.jpg)
![Page 10: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/10.jpg)
CRUD
![Page 11: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/11.jpg)
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
![Page 12: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/12.jpg)
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?
![Page 13: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/13.jpg)
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
![Page 14: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/14.jpg)
never let go of yourD R E A M S
![Page 15: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/15.jpg)
In Practice
![Page 16: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/16.jpg)
In PracticeRouting XML, JSON Building Response
![Page 17: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/17.jpg)
public class SimpleServlet extends GenericServlet { public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { // do something in here } }
Generic Servlet
![Page 18: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/18.jpg)
In PracticeRouting XML, JSON Building Response
![Page 19: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/19.jpg)
In PracticeMap Validate
Routing XML, JSON Building Response
![Page 20: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/20.jpg)
In PracticeConstruct Query
Map Validate
Routing XML, JSON Building Response
![Page 21: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/21.jpg)
Reflection and Runtime Code Generation
Everywhere!Spring, Hibernate, GSON, Jersy, Dozer,
Guice, Weld, etc…
![Page 22: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/22.jpg)
Reflection - Slow?
![Page 23: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/23.jpg)
Reflection - Slow?
http://docs.oracle.com/javase/tutorial/reflect/index.html
Performance OverheadBecause 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.
![Page 24: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/24.jpg)
Reflection - Slow?
http://docs.oracle.com/javase/tutorial/reflect/index.html
Performance OverheadBecause 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.
![Page 25: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/25.jpg)
Reflection - Slow?
http://docs.oracle.com/javase/tutorial/reflect/index.html
Performance OverheadBecause 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.
![Page 26: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/26.jpg)
Code Generation?
https://zeroturnaround.com/rebellabs/how-to-make-java-more-dynamic-with-runtime-code-generation/
![Page 27: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/27.jpg)
Code Generation?
https://zeroturnaround.com/rebellabs/how-to-make-java-more-dynamic-with-runtime-code-generation/
Generates Java Bytecode at Runtime
![Page 28: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/28.jpg)
Code Generation?
https://zeroturnaround.com/rebellabs/how-to-make-java-more-dynamic-with-runtime-code-generation/
Generates Java Bytecode at Runtime
Not always ok
![Page 29: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/29.jpg)
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
![Page 30: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/30.jpg)
Annotation Processing
![Page 31: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/31.jpg)
Annotation Processing
A tool build in javac for scanning and processing annotations at compile time
![Page 32: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/32.jpg)
How?public class MyProcessor extends AbstractProcessor {
@Override public synchronized void init(ProcessingEnvironment env){ } @Override public boolean process(
Set<? extends TypeElement> annotations, RoundEnvironment env) { } @Override public Set<String> getSupportedAnnotationTypes() { } @Override public SourceVersion getSupportedSourceVersion() { }
}
http://hannesdorfmann.com/annotation-processing/annotationprocessing101/
![Page 33: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/33.jpg)
How?public class MyProcessor extends AbstractProcessor {
@Override public synchronized void init(ProcessingEnvironment env){ } @Override public boolean process(
Set<? extends TypeElement> annotations, RoundEnvironment env) { } @Override public Set<String> getSupportedAnnotationTypes() { } @Override public SourceVersion getSupportedSourceVersion() { }
}
http://hannesdorfmann.com/annotation-processing/annotationprocessing101/
![Page 34: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/34.jpg)
How?public class MyProcessor extends AbstractProcessor {
@Override public synchronized void init(ProcessingEnvironment env){ } @Override public boolean process(
Set<? extends TypeElement> annotations, RoundEnvironment env) { } @Override public Set<String> getSupportedAnnotationTypes() { } @Override public SourceVersion getSupportedSourceVersion() { }
}
http://hannesdorfmann.com/annotation-processing/annotationprocessing101/
![Page 35: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/35.jpg)
How?public class MyProcessor extends AbstractProcessor {
@Override public synchronized void init(ProcessingEnvironment env){ } @Override public boolean process(
Set<? extends TypeElement> annotations, RoundEnvironment env) { } @Override public Set<String> getSupportedAnnotationTypes() { } @Override public SourceVersion getSupportedSourceVersion() { }
}
http://hannesdorfmann.com/annotation-processing/annotationprocessing101/
![Page 36: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/36.jpg)
How?public class MyProcessor extends AbstractProcessor {
@Override public synchronized void init(ProcessingEnvironment env){ } @Override public boolean process(
Set<? extends TypeElement> annotations, RoundEnvironment env) { } @Override public Set<String> getSupportedAnnotationTypes() { } @Override public SourceVersion getSupportedSourceVersion() { }
}
http://hannesdorfmann.com/annotation-processing/annotationprocessing101/
![Page 37: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/37.jpg)
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
![Page 38: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/38.jpg)
Examplepublic 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 + "'"); } }
![Page 39: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/39.jpg)
Examplepublic class PizzaStore {
private MealFactory factory = new MealFactory();
public Meal order(String mealName) { return factory.create(mealName); }
}
![Page 40: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/40.jpg)
@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; } }
![Page 41: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/41.jpg)
How?public class MyProcessor extends AbstractProcessor {
@Override public synchronized void init(ProcessingEnvironment env){ } @Override public boolean process(
Set<? extends TypeElement> annotations, RoundEnvironment env) { } @Override public Set<String> getSupportedAnnotationTypes() { } @Override public SourceVersion getSupportedSourceVersion() { }
}
![Page 42: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/42.jpg)
@Override public Set<String> getSupportedAnnotationTypes() { Set<String> annotataions = new LinkedHashSet<String>(); annotataions.add(Factory.class.getCanonicalName()); return annotataions; }
@Override public SourceVersion getSupportedSourceVersion() { return SourceVersion.latestSupported(); }
![Page 43: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/43.jpg)
@Override public synchronized void init(ProcessingEnvironment processingEnv) { super.init(processingEnv); typeUtils = processingEnv.getTypeUtils(); elementUtils = processingEnv.getElementUtils(); filer = processingEnv.getFiler(); messager = processingEnv.getMessager(); }
![Page 44: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/44.jpg)
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 ) {} }
![Page 45: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/45.jpg)
@Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment env) {
// Itearate over all @Factory annotated elements for (Element annotatedElement : env.getElementsAnnotatedWith(Factory.class)){ ... } }
![Page 46: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/46.jpg)
@Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment env) {
// Itearate over all @Factory annotated elements for (Element annotatedElement : env.getElementsAnnotatedWith(Factory.class)){ if (annotatedElement.getKind() != ElementKind.CLASS) { messager.printMessage(Diagnostic.Kind.ERROR, “Ooops!”, annotatedElement); return true; } ... } }
![Page 47: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/47.jpg)
public class FactoryAnnotatedClass {
private TypeElement annotatedClassElement;; private String simpleTypeName; // Class name private String id; // “Margherita”
public FactoryAnnotatedClass(TypeElement classElement) throws Exception { Factory annotation = classElement.getAnnotation(Factory.class); … } }
![Page 48: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/48.jpg)
try { Class<?> clazz = annotation.type(); qualifiedGroupClassName = clazz.getCanonicalName(); simpleFactoryGroupName = clazz.getSimpleName(); } catch (MirroredTypeException mte) { DeclaredType classTypeMirror = (DeclaredType) mte.getTypeMirror(); TypeElement classTypeElement = (TypeElement) classTypeMirror.asElement(); qualifiedGroupClassName = classTypeElement.getQualifiedName().toString(); simpleFactoryGroupName = classTypeElement.getSimpleName().toString(); }
![Page 49: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/49.jpg)
public void generateCode(Elements elementUtils, Filer filer) throws IOException { MethodSpec.Builder method = MethodSpec.methodBuilder("create") .addModifiers(Modifier.PUBLIC) .addParameter(String.class, "id") .returns(TypeName.get(superClassName.asType()));
// Foreach 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
![Page 50: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/50.jpg)
Processing Rounds
![Page 51: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/51.jpg)
Annotation Processing
A tool build in javac for scanning and processing annotations at compile time
![Page 52: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/52.jpg)
Dependency Injection
![Page 53: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/53.jpg)
Dagger 2The fastest Java DI Framework!
https://google.github.io/dagger/
![Page 54: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/54.jpg)
import javax.inject.Inject;
class CoffeeMaker { @Inject Heater heater; @Inject Pump pump; ... }
Dagger 2
![Page 55: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/55.jpg)
@Component(modules = DripCoffeeModule.class) interface CoffeeShop { CoffeeMaker maker(); }
@Moduleclass DripCoffeeModule { @Provides static Heater provideHeater() { return new ElectricHeater(); }
@Provides static Pump providePump(Thermosiphon pump) { return pump; } }
![Page 56: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/56.jpg)
import javax.inject.Inject;
class CoffeeMaker { @Inject Heater heater; @Inject Pump pump; ... }
Dagger 2
CoffeeShop coffeeShop = DaggerCoffeeShop.create();
![Page 57: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/57.jpg)
Dagger 2
๏ Singletons and Scoped Bindings ๏ Lazy injections ๏ Provider injections ๏ Qualifiers
![Page 58: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/58.jpg)
Dagger 2The fastest Java DI Framework!
https://google.github.io/dagger/
![Page 60: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/60.jpg)
public class Car { private String make; private int numberOfSeats; private CarType type; //constructor, getters, setters etc. }
![Page 61: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/61.jpg)
@Mapperpublic interface CarMapper { CarMapper INSTANCE = Mappers.getMapper( CarMapper.class ); @Mappings({ @Mapping(source = "make", target = "manufacturer"), @Mapping(source = "numberOfSeats", target = "seatCount") }) CarDto carToCarDto(Car car); }
![Page 62: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/62.jpg)
@Mapperpublic interface CarMapper { CarMapper INSTANCE = Mappers.getMapper( CarMapper.class ); @Mappings({ @Mapping(source = "make", target = "manufacturer"), @Mapping(source = "numberOfSeats", target = "seatCount") }) CarDto carToCarDto(Car car); }
![Page 63: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/63.jpg)
@Mapperpublic interface CarMapper { CarMapper INSTANCE = Mappers.getMapper( CarMapper.class ); @Mappings({ @Mapping(source = "make", target = "manufacturer"), @Mapping(source = "numberOfSeats", target = "seatCount") }) CarDto carToCarDto(Car car); }
![Page 64: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/64.jpg)
MapStruct
http://mapstruct.org/
๏ Nested mappings ๏ Updating existing bean instances
![Page 65: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/65.jpg)
RetrofitA type-safe HTTP client for Java
https://square.github.io/retrofit/
![Page 66: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/66.jpg)
Retrofit
public interface WeatherData {
@GET(“weather?”) Observable<WeatherData> getWeatherData(@Query(“q”) String city);
}
![Page 67: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/67.jpg)
Retrofit retrofit = new Retrofit.Builder() .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .baseUrl("http://api.openweathermap.org/data/2.5/") .build();
WeatherService weatherService = retrofit.create(WeatherService.class); Observable<WeatherData> london = weatherService.getWeatherData("Riga");
![Page 68: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/68.jpg)
Retrofit
๏ Header Manipulation ๏ Sync and Asyn calls ๏ Custom Converters
![Page 69: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/69.jpg)
![Page 71: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/71.jpg)
@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; }
![Page 72: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/72.jpg)
@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; }
![Page 73: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/73.jpg)
@RequiredArgsConstructor@RequiredArgsConstructor(staticName = "of")@AllArgsConstructor(access = AccessLevel.PROTECTED)public class ConstructorExample<T> { private int x, y; @NonNull private T description; @NoArgsConstructor public static class NoArgsExample { @NonNull private String field; }}
![Page 74: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/74.jpg)
@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; }
![Page 75: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/75.jpg)
public String example() { val example = new ArrayList<String>(); example.add("Hello, World!"); val foo = example.get(0); return foo.toLowerCase();}
![Page 76: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/76.jpg)
public String example() { val example = new ArrayList<String>(); example.add("Hello, World!"); val foo = example.get(0); return foo.toLowerCase();}
val !!11one
![Page 77: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/77.jpg)
@ExtensionMethodclass 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(); } }
![Page 78: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/78.jpg)
lombok.fieldDefaults.defaultPrivate = true
Lombok.config
![Page 79: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/79.jpg)
LombokSpice up your Java
https://projectlombok.org/
๏@Builder ๏@Log ๏@Delegate
![Page 80: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/80.jpg)
• https://github.com/bluelinelabs/LoganSquare • https://github.com/greenrobot/EventBus • … and more
![Page 81: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/81.jpg)
ConclusionAnnotation Processing
![Page 82: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/82.jpg)
Conclusion
Powerful Fast
Sometimes Brittle
Annotation Processing
![Page 83: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/83.jpg)
Conclusion
Powerful Fast
Sometimes Brittle
Annotation Processing
![Page 84: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/84.jpg)
Conclusion
Powerful Fast
Sometimes Brittle
Annotation Processing
![Page 85: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/85.jpg)
Powerful Fast
Sometimes a Life Saver
Conclusion
Powerful Fast
Sometimes Brittle
Annotation Processing
![Page 86: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/86.jpg)
“Use it wisely we must!”
![Page 87: Marvel of Annotation Preprocessing in Java by Alexey Buzdin](https://reader031.vdocuments.net/reader031/viewer/2022030118/589f486f1a28abec418b4605/html5/thumbnails/87.jpg)
Q&AThank You!
@AlexeyBuzdinFollow me at