More Related Content
Similar to 僕の考えるAPT開発の常識 ぐだ生 2011/04/09版 (20)
More from Masahiro Wakame (20)
僕の考えるAPT開発の常識 ぐだ生 2011/04/09版
- 1. APT
Annotation Processing Tool
( ╹ω╹ ) 2011/04/09
2011 4 9
- 6. Annotation
@JsonModel
public class Message {
• Annotation =
@JsonKey
String id;
•
@JsonKey
String room;
•
@JsonKey
String text;
• @Override
@JsonKey
String timestamp;
2011 4 9
- 8. APT
• Annotation Processing Tool
•
•
•
•
2011 4 9
- 9. APT
• JSON ⇔ Model
• Map<String, Object>
POJO
•
• Reflection
2011 4 9
- 10. ← →
• Reflection •
• •
• •
• •
• •
2011 4 9
- 12. APT Version
•
• com.sun.mirror
• Java5
• Slim3
2011 4 9
- 13. APT Version
• JSR 269
• javax.lang.model
• Java6 (JSR270)
• JsonPullParser
• Project Coin(JSR334)
2011 4 9
- 15. Annotation
• @interface
• Retention ( Annotation )
• Target ( Annotation )
@Retention(RetentionPolicy.SOURCE)
@Target({
ElementType.FIELD
}) JsonModel
public @interface JsonKey {
JsonKey
public String value() default "";
public boolean in() default true;
public boolean out() default true;
public boolean decamelize() default false;
}
2011 4 9
- 16. AbstractProcessor
•
@SupportedSourceVersion(SourceVersion.RELEASE_6)
@SupportedAnnotationTypes("net.vvakame.util.jsonpullparser.annotation.*")
public class JsonAnnotationProcessor extends AbstractProcessor {
• EntryPoint
• process
• RoundEnviroment Element
JsonAnnotationProcessor
2011 4 9
- 17. RoundEnviroment
• Element
•
•
•
• Retention
SOURCE Annotation …
2011 4 9
- 18. Element
•
Element
•
• Annotation Element
•
• (ex setter, getter )
•
ClassGenerateHelper#ValueExtractVisitor
2011 4 9
- 19. TypeMirror
•
• Element→TypeMirror→
Element
• …
• Element …
2011 4 9
- 20. Annotation
• Annotation
APT
@JsonModel(treatUnknownKeyAsError = true, String getElementKeyString(Element element) {
decamelize = true) JsonKey key = element.getAnnotation(JsonKey.class);
public class Hoge extends RecvGeoInfoLog { JsonModel model = element.getEnclosingElement()
.getAnnotation(JsonModel.class);
@JsonKey("foga") String keyStr;
String piyo; if (!"".equals(key.value())) {
keyStr = key.value();
@JsonKey(converter = DateConverter.class) } else if ("".equals(key.value()) && key.decamelize()) {
Date at; keyStr = decamelize(element.toString());
} } else if ("".equals(key.value()) && model.decamelize()) {
keyStr = decamelize(element.toString());
} else {
keyStr = element.toString();
}
return keyStr;
}
2011 4 9
- 21. Message
•
• Element |
JsonKey key = el.getAnnotation(JsonKey.class);
String setter = getElementSetter(el);
if (key.in() && setter == null) {
Log.e("can't find setter method", el);
encountError = true;
return defaultAction(t, el);
}
Log
2011 4 9
- 22. JavaFileObject
• Java
• ProcessingEnvironment Filer
•
ClassGenerateHelper#write
2011 4 9
- 23. • Util
• Elements
Element
• Types
• TypeMirror
• Visitor
• Annotation, Element, TypeMirror
ClassGenerateHelper#ValueExtractVisitor
2011 4 9
- 24. • APT jar
• META-INF/services/
javax.annotation.processing.Processor
• Processor FQN
FQN = package +
/src/main/resources/
2011 4 9
- 26. • Aptina
• UnitTest
• TemplateEngine (MVEL)
•
• Eclipse
• maven
• jar
2011 4 9
- 27. Aptina
• Aptina
• Processor JUnit
• Try&Error
• UnitTest …
•
• …
JsonAnnotationProcessorTest
2011 4 9
- 29. maven
• jar
•
• Eclipse
•
• ...
pom.xml
2011 4 9
- 30. Formatter...?
• Java
• ( )
•
UnitTest …
• FQN import …
} else
if ("place".equals(key)) {
net.vvakame.sample.twitter.Place tmp =
net.vvakame.sample.twitter.PlaceGenerated.get(parser);
obj.setPlace(tmp);
if (listener != null) {
listener.onAdd(tmp);
}
2011 4 9
- 31. • core
• jar
• apt
• APT jar
• APT UnitTest
• usage
• APT UnitTest
•
2011 4 9
- 34. JsonPullParser
• https://github.com/vvakame/JSONPullParser
• APT Json→POJO Java
• Android http://bit.ly/hFTbtP
• Tweet →1Tweet
• SengokuDroid server-side
2011 4 9
- 36. • Android Slim3
• HamsterDB..? → GPL...
• SQLite..? → KVS ...
• MsgpackParser
• Java Formatter
2011 4 9