Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Annotation Processing Tools - Droidcon Madrid 2015

Annotation Processing Tools - Droidcon Madrid 2015

Rubén Serrano

April 25, 2015
Tweet

More Decks by Rubén Serrano

Other Decks in Programming

Transcript

  1. Retención • Constructor • Atributo • Variable • Método •

    Parámetro • Etc. • Source • Class • Runtime Objetivo
  2. Runtime Class tiene la magia: • getAnnotation(Class<T> annotationType) • getAnnotations()

    • getDeclaredAnnotations() • isAnnotationPresent(Class<T> annotationType)
  3. @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
  4. @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
  5. @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
  6. 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;
 }
  7. 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();
 }
 }
 }
  8. 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();
 }
 }
 }
  9. 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();
 }
 }
 }
  10. 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();
 }
 }
 }
  11. 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'