Slide 1

Slide 1 text

Introduction to APT Masayuki IZUMI - @izumin5210 potatotips #19

Slide 2

Slide 2 text

Ƅ Rekimoto Lab. at the University of Tokyo (2008-2015: Akashi-NCT) Ɠ Enginner at Wantedly, Inc. (2014.9-2015.2: Dmetlabel, Inc.) 2

Slide 3

Slide 3 text

pry(main)> izumin.skill_ratio Ruby JavaScript Android Design

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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/

Slide 13

Slide 13 text

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/

Slide 14

Slide 14 text

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/

Slide 15

Slide 15 text

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/

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

「便利 annotation ライブラリを作ろう!」 Pluggable Annotataion Processing API と仲良くなろう!

Slide 18

Slide 18 text

ライブラリの構成 * api: 利用者が触るやつ - annotations - クラスとか生成したりするやつ(e.g. RestAdapter) * processor: コンパイラが触るやつ - AbstractProcessor のサブクラス - その他コード生成のロジック

Slide 19

Slide 19 text

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... }

Slide 20

Slide 20 text

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... }

Slide 21

Slide 21 text

AutoService[2] * javac のために入り口をつくる - main/resources/META-INF/services に自分で登録 - AutoService さんにお願い [2] https://github.com/google/auto/tree/master/service

Slide 22

Slide 22 text

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... }

Slide 23

Slide 23 text

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... }

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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