SlideShare a Scribd company logo
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?

More Related Content

PDF
Java Generics Introduction - Syntax Advantages and Pitfalls
PPTX
Java Annotations
PDF
On Parameterised Types and Java Generics
PDF
Java Generics - by Example
PPTX
C# Generics
PPTX
Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 3)
PDF
Scoping Tips and Tricks
DOCX
Problemas secuenciales.
Java Generics Introduction - Syntax Advantages and Pitfalls
Java Annotations
On Parameterised Types and Java Generics
Java Generics - by Example
C# Generics
Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 3)
Scoping Tips and Tricks
Problemas secuenciales.

What's hot (16)

PPT
Generics collections
KEY
Test-Driven Development of Xtext DSLs
PPTX
Java best practices
PPTX
Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 4)
ODP
Code Analysis and Refactoring with CDT
PPT
Prospector Osq 2004 Final
PPT
Introduction to Intermediate Java
PPT
Java Tut1
PPTX
Oop2011 actor presentation_stal
PDF
Javascript basic course
PPTX
Oop2010 Scala Presentation Stal
PPTX
Qcon2011 functions rockpresentation_f_sharp
PDF
Automatic Migration of Legacy Java Method Implementations to Interfaces
PDF
Java 8 features
PPTX
Modern C++
PPT
Introduction to Java Programming Part 2
Generics collections
Test-Driven Development of Xtext DSLs
Java best practices
Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 4)
Code Analysis and Refactoring with CDT
Prospector Osq 2004 Final
Introduction to Intermediate Java
Java Tut1
Oop2011 actor presentation_stal
Javascript basic course
Oop2010 Scala Presentation Stal
Qcon2011 functions rockpresentation_f_sharp
Automatic Migration of Legacy Java Method Implementations to Interfaces
Java 8 features
Modern C++
Introduction to Java Programming Part 2
Ad

Similar to Annotations Processor Tools (APT) (20)

PPTX
Annotation processing
PDF
Aspect oriented programming_with_spring
PDF
Annotation Processing in Android
PPT
Java Annotation
ODP
Lambda Chops - Recipes for Simpler, More Expressive Code
PDF
Aspect oriented programming with spring
PDF
Java Annotation Processing: A Beginner Walkthrough
PDF
NLJUG University Sessie: Java Reborn, Powered by Ordina
PPT
Eclipse Day India 2011 - Extending JDT
PDF
Write code that writes code! A beginner's guide to Annotation Processing - Ja...
PDF
Write code that writes code!
PDF
IPC2010SE Doctrine2 Enterprise Persistence Layer for PHP
PDF
Ejb3 Dan Hinojosa
PPT
Introducing Struts 2
PPTX
Cloud native programming model comparison
PDF
Java Deserialization Vulnerabilities - The Forgotten Bug Class (RuhrSec Edition)
PPTX
Java 8 Feature Preview
PDF
S313937 cdi dochez
PPT
比XML更好用的Java Annotation
PDF
Annotation processing in android
Annotation processing
Aspect oriented programming_with_spring
Annotation Processing in Android
Java Annotation
Lambda Chops - Recipes for Simpler, More Expressive Code
Aspect oriented programming with spring
Java Annotation Processing: A Beginner Walkthrough
NLJUG University Sessie: Java Reborn, Powered by Ordina
Eclipse Day India 2011 - Extending JDT
Write code that writes code! A beginner's guide to Annotation Processing - Ja...
Write code that writes code!
IPC2010SE Doctrine2 Enterprise Persistence Layer for PHP
Ejb3 Dan Hinojosa
Introducing Struts 2
Cloud native programming model comparison
Java Deserialization Vulnerabilities - The Forgotten Bug Class (RuhrSec Edition)
Java 8 Feature Preview
S313937 cdi dochez
比XML更好用的Java Annotation
Annotation processing in android
Ad

Recently uploaded (20)

PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Big Data Technologies - Introduction.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Empathic Computing: Creating Shared Understanding
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Network Security Unit 5.pdf for BCA BBA.
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Big Data Technologies - Introduction.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
Programs and apps: productivity, graphics, security and other tools
Understanding_Digital_Forensics_Presentation.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Empathic Computing: Creating Shared Understanding
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Network Security Unit 5.pdf for BCA BBA.
The AUB Centre for AI in Media Proposal.docx
Advanced methodologies resolving dimensionality complications for autism neur...
Diabetes mellitus diagnosis method based random forest with bat algorithm
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Spectral efficient network and resource selection model in 5G networks
Review of recent advances in non-invasive hemoglobin estimation
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Mobile App Security Testing_ A Comprehensive Guide.pdf
Encapsulation_ Review paper, used for researhc scholars

Annotations Processor Tools (APT)