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
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
87
Architecting the Redbooth Android App - GDG Barcelona Android Community Day
akelael
2
180
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
59
From Legacy to Hexagonal Android (Droidcon London 2014)
akelael
1
180
Evolución Android
akelael
0
140
Other Decks in Programming
See All in Programming
Performance Engineering for Everyone
elenatanasoiu
0
260
IBM Bobを活用したレガシーアプリの最新化
oniak3ibm
PRO
1
240
トークンをケチるな、設計しろ:GitHub Copilotを賢く使うコンテキスト戦略
ochtum
0
300
Skillsは効率化、Agentsは"自分の拡張"——Builder時代のエージェント編成(CC Night 2026)
wemra
1
200
Go1.27で導入されるジェネリクスメソッドでできること
mackee
0
250
初めてのKubernetes 本番運用でハマった話
oku053
0
120
使用 Meilisearch 建立新聞搜尋工具
johnroyer
0
110
LLMによるContent Moderationの本番運用の裏側と品質担保への挑戦
suikabar
3
830
Vite+ Unified Toolchain for the Web
naokihaba
0
700
【やさしく解説 設計編 #0】DDDのコード、読めるのに分からない人へ
panda728
PRO
2
230
才能?センス?知らん、 続けたもん勝ちだ。-- 結婚・出産・癌を越えてなお、私がプロダクトを創り続ける理由
16bitidol
2
800
アルゴリズムは何を圧縮しているのか ─ Haskell から育った「圧縮代数」というメンタルモデル
naoya
15
3k
Featured
See All Featured
How to Think Like a Performance Engineer
csswizardry
28
2.7k
Site-Speed That Sticks
csswizardry
13
1.2k
Learning to Love Humans: Emotional Interface Design
aarron
275
41k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.2k
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
610
GraphQLとの向き合い方2022年版
quramy
50
15k
Navigating Weather and Climate Data
rabernat
0
280
We Have a Design System, Now What?
morganepeng
55
8.2k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
16k
The Cult of Friendly URLs
andyhume
79
6.9k
Rails Girls Zürich Keynote
gr2m
96
14k
The Spectacular Lies of Maps
axbom
PRO
1
850
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?