Introduction to APT

Introduction to APT

【第19回】potatotips(iOS/Android開発Tips共有会) 発表資料 http://connpass.com/event/16295/

9eed44f137609e6ce3b6f1e14f80b9e1?s=128

Masayuki Izumi

July 14, 2015
Tweet

Transcript

  1. Introduction to APT Masayuki IZUMI - @izumin5210 potatotips #19

  2. Ƅ Rekimoto Lab. at the University of Tokyo (2008-2015: Akashi-NCT)

    Ɠ Enginner at Wantedly, Inc. (2014.9-2015.2: Dmetlabel, Inc.) 2
  3. pry(main)> izumin.skill_ratio Ruby JavaScript Android Design

  4. None
  5. None
  6. None
  7. None
  8. None
  9. class ExampleActivity extends Activity { @Bind(R.id.user) EditText username; @OnClick(R.id.submit) void

    submit() { // do something... } } APT (Annotation Processor Tool)
  10. class ExampleActivity extends Activity { @Bind(R.id.user) EditText username; @OnClick(R.id.submit) void

    submit() { // do something... } } APT (Annotation Processor Tool)
  11. class ExampleActivity extends Activity { @Bind(R.id.user) EditText username; @OnClick(R.id.submit) void

    submit() { // do something... } } APT (Annotation Processor Tool)
  12. APT (Annotation Processor Tool) Note: The apt tool and its

    associated API contaiined in the pakcage com.sun.mirror have been deprecated since Java SE 7. Use the options available in the javac tool and the APIs contained in the packages javax.annotation.processing and javax.lang.model to process annotations[1] . [1] http://docs.oracle.com/javase/7/docs/technotes/guides/apt/
  13. APT (Annotation Processor Tool) Note: The apt tool and its

    associated API contaiined in the pakcage com.sun.mirror have been deprecated since Java SE 7. Use the options available in the javac tool and the APIs contained in the packages javax.annotation.processing and javax.lang.model to process annotations[1] . [1] http://docs.oracle.com/javase/7/docs/technotes/guides/apt/
  14. APT (Annotation Processor Tool) This functionality is accessed through new

    options to the javac command; by including JSR 269 support, javac now acts analogously to the apt command in JDK 5[1] . [1] http://docs.oracle.com/javase/7/docs/technotes/guides/apt/
  15. APT (Annotation Processor Tool) This functionality is accessed through new

    options to the javac command; by including JSR 269 support, javac now acts analogously to the apt command in JDK 5[1] . [1] http://docs.oracle.com/javase/7/docs/technotes/guides/apt/
  16. None
  17. 「便利 annotation ライブラリを作ろう!」 Pluggable Annotataion Processing API と仲良くなろう!

  18. ライブラリの構成 * api: 利用者が触るやつ - annotations - クラスとか生成したりするやつ(e.g. RestAdapter) *

    processor: コンパイラが触るやつ - AbstractProcessor のサブクラス - その他コード生成のロジック
  19. Processor @AutoService(javax.annotation.processing.Processor.class) public class Processor extends AbstractProcessor { @Override public

    boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { // do something... return true; } // do something... }
  20. Processor @AutoService(javax.annotation.processing.Processor.class) public class Processor extends AbstractProcessor { @Override public

    boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { // do something... return true; } // do something... }
  21. AutoService[2] * javac のために入り口をつくる - main/resources/META-INF/services に自分で登録 - AutoService さんにお願い

    [2] https://github.com/google/auto/tree/master/service
  22. Processor @AutoService(javax.annotation.processing.Processor.class) public class Processor extends AbstractProcessor { @Override public

    boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { // do something... return true; } // do something... }
  23. Processor @AutoService(javax.annotation.processing.Processor.class) public class Processor extends AbstractProcessor { @Override public

    boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { // do something... return true; } // do something... }
  24. Code generation: JavaPoet[3] MethodSpec toString = MethodSpec.methodBuilder("toString") .addAnnotation(Override.class) .returns(String.class) .addModifiers(Modifier.PUBLIC)

    .addStatement("return $S", "Hoverboard") .build(); [3] https://github.com/square/javapoet
  25. Code generation: JavaPoet[3] MethodSpec toString = MethodSpec.methodBuilder("toString") .addAnnotation(Override.class) .returns(String.class) .addModifiers(Modifier.PUBLIC)

    .addStatement("return $S", "Hoverboard") .build(); [3] https://github.com/square/javapoet
  26. Code generation: JavaPoet[3] MethodSpec toString = MethodSpec.methodBuilder("toString") .addAnnotation(Override.class) .returns(String.class) .addModifiers(Modifier.PUBLIC)

    .addStatement("return $S", "Hoverboard") .build(); [3] https://github.com/square/javapoet
  27. Code generation: JavaPoet[3] MethodSpec toString = MethodSpec.methodBuilder("toString") .addAnnotation(Override.class) .returns(String.class) .addModifiers(Modifier.PUBLIC)

    .addStatement("return $S", "Hoverboard") .build(); [3] https://github.com/square/javapoet
  28. Code generation: JavaPoet[3] MethodSpec toString = MethodSpec.methodBuilder("toString") .addAnnotation(Override.class) .returns(String.class) .addModifiers(Modifier.PUBLIC)

    .addStatement("return $S", "Hoverboard") .build(); [3] https://github.com/square/javapoet
  29. Code generation: JavaPoet[3] MethodSpec toString = MethodSpec.methodBuilder("toString") .addAnnotation(Override.class) .returns(String.class) .addModifiers(Modifier.PUBLIC)

    .addStatement("return $S", "Hoverboard") .build(); [3] https://github.com/square/javapoet
  30. API: Annotation @Target(ElementType.TYPE) public @interface KokoroPyonPyon() { int resId(); String

    name(); }
  31. API: Annotation @Target(ElementType.TYPE) public @interface KokoroPyonPyon() { int resId(); String

    name(); }
  32. API: Annotation @Target(ElementType.TYPE) public @interface KokoroPyonPyon() { int resId(); String

    name(); }
  33. test: Compile Testing[3] public ProcessorTest { @Test public void testProcessor()

    { assert_().about(javaSource()) .that(JavaFileObjects.forSourceString("PyonPyon", "final class PyonPyon {}")) .processedWith(new MyAnnotationProcessor()) .compilesWithoutError(); } } [3] https://github.com/google/compile-testing
  34. test: Compile Testing public ProcessorTest { @Test public void testProcessor()

    { assert_().about(javaSource()) .that(JavaFileObjects.forSourceString("PyonPyon", "final class PyonPyon {}")) .processedWith(new MyAnnotationProcessor()) .compilesWithoutError(); } }
  35. test: Compile Testing public ProcessorTest { @Test public void testProcessor()

    { assert_().about(javaSource()) .that(JavaFileObjects.forSourceString("PyonPyon", "final class PyonPyon {}")) .processedWith(new MyAnnotationProcessor()) .compilesWithoutError(); } }
  36. Conclusion * Annotation は API と Processor から成る - API:

    ユーザが触る - Processor: javac が触る * Google と Square に足を向けて寝られない - AutoService - Compile Testing - JavaPoet