Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Annotation Processing Tools - Droidcon Madrid 2015
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Rubén Serrano
April 25, 2015
Programming
180
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Annotation Processing Tools - Droidcon Madrid 2015
Rubén Serrano
April 25, 2015
More Decks by Rubén Serrano
See All by Rubén Serrano
Clean Architecture the Right Way. Or almost...
akelael
3
230
Flutter, the road to Fuchsia (DevFest Tarragona 2018)
akelael
0
83
Architecting the Redbooth Android App - GDG Barcelona Android Community Day
akelael
2
170
Unclean Architecture in Android - Droidcon Madrid 2016
akelael
0
110
From Legacy to Hexagonal (Codemotion Madrid 2014)
akelael
5
380
Evolución Android (v3.0, DevFest Tarragona 2014)
akelael
0
57
From Legacy to Hexagonal Android (Droidcon London 2014)
akelael
1
180
Evolución Android
akelael
0
130
Other Decks in Programming
See All in Programming
AI駆動開発で崩れていくコードベースを立て直す
kyoko_nr_nr
1
440
Spec-Driven Development with AI-Agents: From High-Level Requirements to Working Software
antonarhipov
2
470
3Dシーンの圧縮
fadis
1
670
Observability in Practice:Grafana 與 Edge Device SRE 的那些事
blueswen
0
130
Modding RubyKaigi for Myself
yui_knk
0
890
Spec Driven Development | AI Summit Lisbon
danielsogl
PRO
0
160
生成AI時代にこそ効くGo | Why Go Works in the Age of Generative AI
mom0tomo
8
3.1k
AIとRubyの静的型付け
ukin0k0
0
540
TypeSpec で繋ぐ複数プロダクトの型安全
maroon8021
1
420
代数的データ型って何が嬉しいの? #frontend_phpcon_do
kajitack
8
3.2k
Javaの型とAI時代に型が大事な理由 / java types and type in AI era
kishida
2
110
Why Laravel apps break—Mastering the fundamentals to keep them maintainable
kentaroutakeda
1
340
Featured
See All Featured
We Have a Design System, Now What?
morganepeng
55
8.2k
SEO for Brand Visibility & Recognition
aleyda
0
4.6k
Marketing to machines
jonoalderson
1
5.4k
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.5k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
201
75k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.2k
HTML-Aware ERB: The Path to Reactive Rendering @ RubyCon 2026, Rimini, Italy
marcoroth
1
160
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
0
310
Documentation Writing (for coders)
carmenintech
77
5.4k
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.6k
Why You Should Never Use an ORM
jnunemaker
PRO
61
9.9k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
250
1.3M
Transcript
Annotation Processing Tools 101
Txus Ballesteros @txusballesteros Rubén Serrano @akelael
Agenda 1. Annotation Processing Tool 2. Discusión 3. Caso práctico
¿Qué es eso de APT?
public @interface DummyAnnotiation {} Anotación
Retención • Constructor • Atributo • Variable • Método •
Parámetro • Etc. • Source • Class • Runtime Objetivo
Anotación @Retention(CLASS) @Target(FIELD) public @interface InjectView { int value(); }
Runtime Class tiene la magia: • getAnnotation(Class<T> annotationType) • getAnnotations()
• getDeclaredAnnotations() • isAnnotationPresent(Class<T> annotationType)
Mirror API http://docs.oracle.com/javase/1.5.0/docs/guide/apt/mirror/overview-summary.html
Discusión
Caso Práctico
Android Transformer https://github.com/txusballesteros/android-transformer
Creando Anotaciones @Retention(RetentionPolicy.SOURCE) @Target(ElementType.TYPE) public @interface Mappable { Class<?>
with(); }
Creando Anotaciones @Retention(RetentionPolicy.SOURCE) @Target(ElementType.TYPE) public @interface Mappable { Class<?>
with(); }
Configurando las Anotaciones @Retention(RetentionPolicy.SOURCE) @Target(ElementType.TYPE) public @interface Mappable {
Class<?> with(); }
Configurando las Anotaciones @Retention(RetentionPolicy.SOURCE) @Target(ElementType.TYPE) public @interface Mappable {
Class<?> with(); }
@SupportedSourceVersion(SourceVersion.RELEASE_7) @SupportedAnnotationTypes({ "com.mobandme.android.transformer.compiler.Mapping", "com.mobandme.android.transformer.compiler.Mappable", "com.mobandme.android.transformer.compiler.Parse" }) public class AnnotationsProcessor extends
AbstractProcessor { RoundEnvironment roundEnvironment; Map<String, MapperInfo> mappersList; @Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { return true; } Creando el Procesador
@SupportedSourceVersion(SourceVersion.RELEASE_7) @SupportedAnnotationTypes({ "com.mobandme.android.transformer.compiler.Mapping", "com.mobandme.android.transformer.compiler.Mappable", "com.mobandme.android.transformer.compiler.Parse" }) public class AnnotationsProcessor extends
AbstractProcessor { RoundEnvironment roundEnvironment; Map<String, MapperInfo> mappersList; @Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { return true; } Creando el Procesador
@SupportedSourceVersion(SourceVersion.RELEASE_7) @SupportedAnnotationTypes({ "com.mobandme.android.transformer.compiler.Mapping", "com.mobandme.android.transformer.compiler.Mappable", "com.mobandme.android.transformer.compiler.Parse" }) public class AnnotationsProcessor extends
AbstractProcessor { RoundEnvironment roundEnvironment; Map<String, MapperInfo> mappersList; @Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { return true; } Configurando el Procesador
Implementando el Procesador @SupportedSourceVersion(SourceVersion.RELEASE_7) @SupportedAnnotationTypes({ "com.mobandme.android.transformer.compiler.Mapping", "com.mobandme.android.transformer.compiler.Mappable", "com.mobandme.android.transformer.compiler.Parse" }) public
class AnnotationsProcessor extends AbstractProcessor { RoundEnvironment roundEnvironment; Map<String, MapperInfo> mappersList; @Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { return true; }
Implementando el Procesador private void processMappedAnnotationElements() { Set<? extends Element>
elements = roundEnvironment.getElementsAnnotatedWith(Mapped.class); for (Element mappedElement : elements) { if (mappedElement.getKind() == ElementKind.FIELD) { Mapped mappedAnnotation = mappedElement.getAnnotation(Mapped.class); String toFieldName = mappedAnnotation.toField(); } } }
Implementando el Procesador private void processMappedAnnotationElements() { Set<? extends Element>
elements = roundEnvironment.getElementsAnnotatedWith(Mapped.class); for (Element mappedElement : elements) { if (mappedElement.getKind() == ElementKind.FIELD) { Mapped mappedAnnotation = mappedElement.getAnnotation(Mapped.class); String toFieldName = mappedAnnotation.toField(); } } }
Implementando el Procesador private void processMappedAnnotationElements() { Set<? extends Element>
elements = roundEnvironment.getElementsAnnotatedWith(Mapped.class); for (Element mappedElement : elements) { if (mappedElement.getKind() == ElementKind.FIELD) { Mapped mappedAnnotation = mappedElement.getAnnotation(Mapped.class); String toFieldName = mappedAnnotation.toField(); } } }
Implementando el Procesador private void processMappedAnnotationElements() { Set<? extends Element>
elements = roundEnvironment.getElementsAnnotatedWith(Mapped.class); for (Element mappedElement : elements) { if (mappedElement.getKind() == ElementKind.FIELD) { Mapped mappedAnnotation = mappedElement.getAnnotation(Mapped.class); String toFieldName = mappedAnnotation.toField(); } } }
Generando el Código JavaFileObject file = processingEnv.getFiler().createSourceFile(name); BufferedWriter buffer =
new BufferedWriter(file.openWriter()); //… buffer.close();
Configurando el Entorno com.mobandme.android.transformer.compiler.internal.AnnotationsProcessor
Configuración de Gradle para el Compilador buildscript { repositories {
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" } jcenter() } dependencies { classpath 'com.jimdo.gradle:gradle-apt-plugin:0.5-SNAPSHOT' } } apply plugin: 'java' apply plugin: 'apt'
El resultado final
¿Preguntas?