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
Lean は証明の正しさを確認するためだけのツールって思ってませんか?
inoueasei
1
110
AIキャラアプリkaiwaの低遅延音声通話基盤をどう作ったか - AWS Gravitonで支える低遅延・低コストAI Agent基盤
mogamit
0
190
FDEが実現するAI駆動経営の現在地
gonta
2
220
関数型プログラミングのメリットって何だろう?
wanko_it
0
190
Apache Hive: Toward a Cloud Native Lakehouse
okumin
0
160
act1-costs.pdf
sumedhbala
0
250
なぜ関数型プログラミングで「型」と「証明」が語られるのか #fp_matsuri
kajitack
3
1k
【やさしく解説 設計編 #0】DDDのコード、読めるのに分からない人へ
panda728
PRO
2
280
광주소프트웨어마이스터고등학교 DevFest 특강 - 바이브 코딩 시대에서 주니어 개발자로 살아남는 방법
utilforever
1
150
これからAgentCoreを触る方へトレンドはGatewayです
har1101
6
500
Welcome to the "Parametricity" 🏙️ − Generic だけど Specific な世界 −
guvalif
PRO
1
180
人間の目はかわらない、だからJPEGは30年もつ
yuzneri
9
15k
Featured
See All Featured
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
430
Deep Space Network (abreviated)
tonyrice
0
230
Optimizing for Happiness
mojombo
378
71k
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
1
270
Mobile First: as difficult as doing things right
swwweet
225
10k
The World Runs on Bad Software
bkeepers
PRO
72
12k
Speed Design
sergeychernyshev
33
1.9k
Utilizing Notion as your number one productivity tool
mfonobong
4
460
[SF Ruby Conf 2025] Rails X
palkan
2
1.2k
Six Lessons from altMBA
skipperchong
29
4.3k
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
270
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
1.2k
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?