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

Droidcon madrid CFP

Droidcon madrid CFP

Suraj Shirvankar

June 09, 2016
Tweet

More Decks by Suraj Shirvankar

Other Decks in Technology

Transcript

  1. Annotations, a form of metadata, provide data about a program

    that is not part of the program itself. Annotations have no direct effect on the operation of the code they annotate http://docs.oracle.com/javase/1.5.0/docs/guide/apt/mirror/overview-summary.html
  2. TEXT ANNOTATIONS ALLOW THE DEVELOPER TO ▸ Information for the

    compiler — Annotations can be used by the compiler to detect errors or suppress warnings. ▸ Compile-time and deployment-time processing — Software tools can process annotation information to generate code, XML files, and so forth. ▸ Runtime processing — Some annotations are available to be examined at runtime. https://docs.oracle.com/javase/tutorial/java/annotations/
  3. Annotation Processing is a technique that hooks into the Java

    compile process. It allows to produce compiler errors and warnings and to generate source code and byte code.
  4. EXTEND ABSTRACT PROCESSOR public class MyProcessor extends AbstractProcessor { @Override

    public synchronized void init(ProcessingEnvironment env){} @Override public boolean process(Set<? extends TypeElement> annoations, RoundEnvironment env) { } @Override public Set<String> getSupportedAnnotationTypes() { } @Override public SourceVersion getSupportedSourceVersion() { } }
  5. ▸ Package your processor into Jar ▸ Create a file

    called javax.annotation.processing.Processor in the META-INF/services directory ▸ Write the fully qualified names of the processors in the javax.annotation.processing.Processor file. Steps
  6. JAVAPOET https://github.com/square/javapoet package com.example.helloworld; public final class HelloWorld { public

    static void main(String[] args) { System.out.println("Hello, JavaPoet!"); } }
  7. Using JavaPoet MethodSpec main = MethodSpec.methodBuilder("main") .addModifiers(Modifier.PUBLIC, Modifier.STATIC) .returns(void.class) .addParameter(String[].class,

    "args") .addStatement("$T.out.println($S)", System.class, "Hello, JavaPoet!") .build(); TypeSpec helloWorld = TypeSpec.classBuilder(“HelloWorld") .addModifiers(Modifier.PUBLIC, Modifier.FINAL) .addMethod(main) .build(); JavaFile javaFile = JavaFile.builder("com.example.hellowo rld", helloWorld).build(); javaFile.writeTo(System.out);