Java Annotation Trinea www trinea cn Annotation Annotation

  • Slides: 30
Download presentation
Java Annotation Trinea www. trinea. cn

Java Annotation Trinea www. trinea. cn

目� 一. Annotation 示例 二. Annotation 概念及作用 三. Annotation 分� 四. Annotation 自定� 五.

目� 一. Annotation 示例 二. Annotation 概念及作用 三. Annotation 分� 四. Annotation 自定� 五. Annotation 解析 六. 几个 Android 开源� Annotation 原理�析 www. trinea. cn

Annotation 示例 • Override @Override public void on. Create(Bundle saved. Instance. State); • Retrofit

Annotation 示例 • Override @Override public void on. Create(Bundle saved. Instance. State); • Retrofit @GET("/users/{username}") User get. User(@Path("username") String username); • Butter Knife @Inject. View(R. id. user) Edit. Text username; • Active. Android @Column(name = “Name") public String name; www. trinea. cn

目� 一. Annotation 示例 二. Annotation 概念及作用 三. Annotation 分� 四. Annotation 自定� 五.

目� 一. Annotation 示例 二. Annotation 概念及作用 三. Annotation 分� 四. Annotation 自定� 五. Annotation 解析 六. 几个 Android 开源� Annotation 原理�析 www. trinea. cn

Annotation 是什么 An annotation is a form of metadata, that can be added to

Annotation 是什么 An annotation is a form of metadata, that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated. Annotations have no direct effect on the operation of the code they annotate. 能�添加到 Java 源代�的�法元数据。�、方 法、�量、参数、包都可以被注解,可用来将信息 元数据与程序元素�行关� Annotation 中文常�� “注解” www. trinea. cn

public class Person { private int id; private String name; public Person(int id, String

public class Person { private int id; private String name; public Person(int id, String name) { this. id = id; this. name = name; } public boolean equals(Person person) { return person. id == id; } 运行�果如何? public int hash. Code() { return id; } public static void main(String[] args) { Set<Person> set = new Hash. Set<Person>(); for (int i = 0; i < 10; i++) { set. add(new Person(1, "Jim")); } System. out. println(set. size()); } } www. trinea. cn

public class Person { private int id; private String name; public Person(int id, String

public class Person { private int id; private String name; public Person(int id, String name) { this. id = id; this. name = name; } @Override public boolean equals(Object obj) { return (obj instanceof Person) && (((Person)obj). id == id); } @Override public int hash. Code() { return id; } } www. trinea. cn

目� 一. Annotation 示例 二. Annotation 概念及作用 三. Annotation 分� 四. Annotation 自定� 五.

目� 一. Annotation 示例 二. Annotation 概念及作用 三. Annotation 分� 四. Annotation 自定� 五. Annotation 解析 六. 几个 Android 开源� Annotation 原理�析 www. trinea. cn

Annotation 分� • �准 Annotation Override, Deprecated, Suppress. Warnings • 元 Annotation @Retention, @Target,

Annotation 分� • �准 Annotation Override, Deprecated, Suppress. Warnings • 元 Annotation @Retention, @Target, @Inherited, @Documented • 自定� Annotation www. trinea. cn

目� 一. Annotation 示例 二. Annotation 概念及作用 三. Annotation 分� 四. Annotation 自定� 五.

目� 一. Annotation 示例 二. Annotation 概念及作用 三. Annotation 分� 四. Annotation 自定� 五. Annotation 解析 六. 几个 Android 开源� Annotation 原理�析 www. trinea. cn

Annotation 自定� —� 用 public class App { @Method. Info( author = “trinea. cn+android@gmail.

Annotation 自定� —� 用 public class App { @Method. Info( author = “trinea. cn+android@gmail. com”, date = "2014/02/14", version = 2) public String get. App. Name() { return "trinea"; } } www. trinea. cn

Annotation 自定� —定 � @Documented @Retention(Retention. Policy. RUNTIME) @Target(Element. Type. METHOD) @Inherited public @interface

Annotation 自定� —定 � @Documented @Retention(Retention. Policy. RUNTIME) @Target(Element. Type. METHOD) @Inherited public @interface Method. Info { String author() default "trinea@gmail. com"; String date(); int version() default 1; } www. trinea. cn

目� 一. Annotation 示例 二. Annotation 概念及作用 三. Annotation 分� 四. Annotation 自定� 五.

目� 一. Annotation 示例 二. Annotation 概念及作用 三. Annotation 分� 四. Annotation 自定� 五. Annotation 解析 六. 几个 Android 开源� Annotation 原理�析 www. trinea. cn

运行� Annotation 解析 运行� Annotation 指 @Retention � RUNTIME 的 Annotation,可手��用下面 API 解析 常用

运行� Annotation 解析 运行� Annotation 指 @Retention � RUNTIME 的 Annotation,可手��用下面 API 解析 常用 API a. method. get. Annotation(Annotation. Name. class); b. method. get. Annotations(); • c. method. is. Annotation. Present(Annotation. Name. cla ss); 其他 @Target 如 Field,Class 方法�似 www. trinea. cn

运行� Annotation 解析 public static void main(String[] args) { try { Class cls =

运行� Annotation 解析 public static void main(String[] args) { try { Class cls = Class. for. Name("cn. trinea. java. test. annotation. App"); for (Method method : cls. get. Methods()) { Method. Info method. Info = method. get. Annotation( Method. Info. class); if (method. Info != null) { System. out. println("method name: " + method. get. Name()); System. out. println("method author: " + method. Info. author()); System. out. println("method version: " + method. Info. version()); System. out. println("method date: " + method. Info. date()); } } } catch (Class. Not. Found. Exception e) { e. print. Stack. Trace(); } } www. trinea. cn

��� Annotation 解析 ��� Annotation 指 @Retention � CLASS 的 Annotation,� apt 自�解析。 需要做的

��� Annotation 解析 ��� Annotation 指 @Retention � CLASS 的 Annotation,� apt 自�解析。 需要做的 a. 自定��集成自 Abstract. Processor b. 重写其中的 process 函数 • www. trinea. cn

��� Annotation 解析 @Supported. Annotation. Types({ "cn. trinea. java. test. annotation. Method. Info" })

��� Annotation 解析 @Supported. Annotation. Types({ "cn. trinea. java. test. annotation. Method. Info" }) public class Method. Info. Processor extends Abstract. Processor { @Override public boolean process(Set<? extends Type. Element> annotations, Round. Environment env) { Hash. Map<String, String> map = new Hash. Map<String, String>(); for (Type. Element te : annotations) { for (Element element : env. get. Elements. Annotated. With(te)) { Method. Info method. Info = element. get. Annotation(Method. Info. class); map. put(element. get. Enclosing. Element(). to. String(), method. Info. author()); } } return false; } } www. trinea. cn

目� 一. Annotation 示例 二. Annotation 概念及作用 三. Annotation 分� 四. Annotation 自定� 五.

目� 一. Annotation 示例 二. Annotation 概念及作用 三. Annotation 分� 四. Annotation 自定� 五. Annotation 解析 六. 几个 Android 开源� Annotation 原理�析 www. trinea. cn

Annotation — Retrofit • �用 @GET("/users/{username}") User get. User(@Path("username") String username); • 定� @Documented

Annotation — Retrofit • �用 @GET("/users/{username}") User get. User(@Path("username") String username); • 定� @Documented @Target(METHOD) @Retention(RUNTIME) @Rest. Method("GET") public @interface GET { String value(); } www. trinea. cn

Annotation — Retrofit • 原理 www. trinea. cn

Annotation — Retrofit • 原理 www. trinea. cn

Annotation — Butter Knife • �用 @Inject. View(R. id. user) Edit. Text username; •

Annotation — Butter Knife • �用 @Inject. View(R. id. user) Edit. Text username; • 定� @Retention(CLASS) @Target(FIELD) public @interface Inject. View { int value(); } www. trinea. cn

Annotation — Butter Knife • 原理 www. trinea. cn

Annotation — Butter Knife • 原理 www. trinea. cn

Annotation — Active. Android • �用 @Column(name = “Name") public String name; • 定�

Annotation — Active. Android • �用 @Column(name = “Name") public String name; • 定� @Target(Element. Type. FIELD) @Retention(Retention. Policy. RUNTIME) public @interface Column { …… } www. trinea. cn

Annotation — Active. Android • 原理 www. trinea. cn

Annotation — Active. Android • 原理 www. trinea. cn

回� 一. Annotation 示例 二. Annotation 概念及作用 三. Annotation 分� 四. Annotation 自定� 五.

回� 一. Annotation 示例 二. Annotation 概念及作用 三. Annotation 分� 四. Annotation 自定� 五. Annotation 解析 六. 几个 Android 开源� Annotation 原理�析 www. trinea. cn

QA • 微博:trinea • 博客:http: //www. trinea. cn/ • Git. Hub:https: //github. com/Trinea www.

QA • 微博:trinea • 博客:http: //www. trinea. cn/ • Git. Hub:https: //github. com/Trinea www. trinea. cn