More Related Content Similar to Java annotation (20) More from Trinea Trinea (6) Java annotation3. www.trinea.cn
示例
@Override
public void onCreate(Bundle savedInstanceState);
@GET("/user/getFriends/{phone}")
String getFriends(@Path("phone") String phone);
@InjectView(R.id.user)
EditText username;
@Column(name = "Name")
public String name;
Override
Retrofit
Butter Knife
ActiveAndroid
5. www.trinea.cn
是什么——注解
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源代码的语法元数据。类、方法、变量、参数、包都可以被
注解,可用来将信息元数据与程序元素进行关联
作用
标记,用于告诉编译器一些信息
编译时动态生成代码
运行时处理逻辑
7. www.trinea.cn
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 hashCode() {
return id;
}
}
public static void main(String[] args) {
Set<Person> appSet = new
HashSet<Person>();
for (int i = 0; i < 10; i++) {
appSet.add(new Person(1, "Jim"));
}
System.out.println(appSet.size());
}
运行结果如何?
8. www.trinea.cn
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 hashCode() {
return id;
}
}
11. www.trinea.cn
自定义Annotation
public class App {
@MethodInfo(
author = “gengxinwu@anjuke.com",
date = "2014/02/14",
version = 2)
public String getAppName() {
return "anjuke";
}
}
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Inherited
public @interface MethodInfo {
String author() default "jim@anjuke.com";
String date();
int version() default 1;
}
1、注解名即为自定义注解名
2、注解配置参数名为注解类的方法名
12. www.trinea.cn
自定义Annotation
1、通过@interface定义
2、方法
a. 所有方法没有方法体,没有参数没有修饰
符,实际只允许public & abstract修饰符,默
认为public,不允许抛异常
b. 方法返回值只能是基本类型,String, Class,
annotation, enumeration或者是他们的一维
数组
c. 只有一个默认属性,直接用value()函数。
一个属性没有表示为Mark Annotation
3、可以加default表示默认值
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Inherited
public @interface MethodInfo {
String author() default "jim@anjuke.com";
String date();
int version() default 1;
}
13. www.trinea.cn
元Annotation
@Documented 是否会保存到Javadoc文档
@Target – Anonotation可以用来修饰哪些程序元素。TYPE, METHOD,
CONSTRUCTOR, FIELD, PARAMETER等,未标注则表示所有
@Inherited – 是否可以被继承,默认为false
@Retention – 保留时间,可选值SOURCE, CLASS and RUNTIME,默认为CLASS
16. 自定义Annotation-ActiveAndroid
www.trinea.cn
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Column {
public enum ForeignKeyAction {
SET_NULL, SET_DEFAULT, CASCADE, RESTRICT, NO_ACTION
}
public String name() default "";
public int length() default -1;
public boolean notNull() default false;
public ForeignKeyAction onUpdate() default
ForeignKeyAction.NO_ACTION;
}
@Column(name = "Name")
public String name;
18. www.trinea.cn
解析Annotation——运行时解析
利用getAnnotation函数得到Annotation信息
public static void main(String[] args) {
try {
Class cls = Class.forName("cn.trinea.java.test.annotation.App");
for (Method method : cls.getMethods()) {
MethodInfo methodInfo = method.getAnnotation(MethodInfo.class);
if (methodInfo != null) {
System.out.println("method name:" + method.getName());
System.out.println("method author:" + methodInfo.author());
System.out.println("method version:" + methodInfo.version());
System.out.println("method date:" + methodInfo.date());
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
19. www.trinea.cn
解析Annotation——运行时解析
常用函数
method.getAnnotation(MethodInfo.class);
method.getAnnotations();
method.isAnnotationPresent(MethodInfo.class);
其他Field Class类似
运行时解析必须要Retention为RUNTIME
20. www.trinea.cn
解析Annotation——编译时解析
自定义AbstractProcessor
@SupportedAnnotationTypes({ "cn.trinea.java.test.annotation.MethodInfo" })
public class MethodInfoProcessor extends AbstractProcessor {
@Override
public boolean process(Set<? extends TypeElement> elements, RoundEnvironment env) {
HashMap<String, String> map = new HashMap<String, String>();
for (TypeElement te : elements) {
for (Element element : env.getElementsAnnotatedWith(te)) {
MethodInfo methodInfo = element.getAnnotation(MethodInfo.class);
map.put(element.getEnclosingElement().toString(), methodInfo.author());
}
}
return false;
}
}
Editor's Notes ANNOTATION_TYPE, CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE ButterKnifeProcessor.java