Annotation Processing @potatochips#43

A374f41eab3f73c50d8bab0652cb207a?s=47 TakuSemba
August 25, 2017

Annotation Processing @potatochips#43

A374f41eab3f73c50d8bab0652cb207a?s=128

TakuSemba

August 25, 2017
Tweet

Transcript

  1. 6.

    Annotation Processing Generate .java files when you compile Merits ɾDo

    not need to write repeatedly ɾTrust the generated code
  2. 7.

    Annotation Processing Generate .java files when you compile Merits Components

    ɾDo not need to write repeatedly ɾTrust the generated code
  3. 8.

    Annotation Processing Generate .java files when you compile Merits Components

    ɾDo not need to write repeatedly ɾTrust the generated code ɾAnnotation
  4. 9.

    Annotation Processing Generate .java files when you compile Merits Components

    ɾDo not need to write repeatedly ɾTrust the generated code ɾAnnotation ɾProcessor
  5. 11.

    public final class UserBuilder { private String name; private int

    age; public UserBuilder name(String name) { this.name = name; return this; } …. public User build() { User user = new User(); user.name = this.name; user.age = this.age; return target; } } class User { String name; int age; }
  6. 12.

    class User { String name; int age; } User user

    = new UserBuilder() .age(23) .name("taku") .build();
  7. 13.

    @Buildable class User { @BuilderField String name; @BuilderField int age;

    } User user = new UserBuilder() .age(23) .name("taku") .build();
  8. 14.

    // BuildField.java @Target(ElementType.FIELD) @Retention(RetentionPolicy.SOURCE) public @interface BuilderField { } //

    Buildable.java @Target(ElementType.TYPE) @Retention(RetentionPolicy.SOURCE) public @interface Buildable { } Annotation
  9. 15.

    Processor // BuilderProcessor.java @Override public synchronized void init(ProcessingEnvironment processingEnv) {

    // implementation } @Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { // implementation }
  10. 16.

    Processor // BuilderProcessor.java private Filer filer; private Messager messager; private

    Elements elements; @Override public synchronized void init(ProcessingEnvironment processingEnv) { super.init(processingEnv); this.messager = processingEnv.getMessager(); this.filer = processingEnv.getFiler(); this.elements = processingEnv.getElementUtils(); }
  11. 18.
  12. 20.

    Processor MethodSpec methodSpec = MethodSpec .methodBuilder(”name”) .addModifiers(Modifier.PUBLIC) .returns(User.class) .addParameter(String.class, ”name”)

    .addStatement("this.$N = $N", ”name”, ”name”) .addStatement("return this") .build(); public UserBuilder name(String name) { this.name = name; return this; }
  13. 22.

    Processor JavaFile file = JavaFile .builder(builderClass.packageName(), classSpec) .build(); try {

    file.writeTo(filer); } catch (IOException e) { messager.printMessage(Diagnostic.Kind.ERROR, "Failed to create file " + builderClass.packageName()); } > app/build/generated/source/apt/debug/your/package/UserBuilder.java
  14. 24.

    @Buildable class User { @BuilderField String name; @BuilderField int age;

    } public final class UserBuilder { private String name; private int age; …. public User build() { User user = new User(); user.name = this.name; user.age = this.age; return target; } }
  15. 25.

    @Buildable class User { @BuilderField String name; @BuilderField int age;

    } public final class UserBuilder { private String name; private int age; …. public User build() { User user = new User(); user.name = this.name; user.age = this.age; return target; } } User user = new UserBuilder().age(23).name("taku").build();