您的位置:首页 > 其它

annotation的获取方式(类、方法、域、参数、构造器)

2015-11-26 17:31 337 查看
先贴代码:

import java.lang.annotation.Annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class AnnotationTest {

/**
* @param args
* @throws NoSuchMethodException
* @throws SecurityException
* @throws NoSuchFieldException
*/
public static void main(String[] args) throws SecurityException,
NoSuchMethodException, NoSuchFieldException {

Class<?> userAnnClass = UseMyAnnotation.class;
if (userAnnClass.isAnnotationPresent(MyAnnotation.class)) {
System.out.println(userAnnClass.getAnnotation(MyAnnotation.class).value());
}

Constructor<?>[] constructorList = userAnnClass.getConstructors();
for (Constructor<?> c : constructorList) {
if (c.isAnnotationPresent(MyAnnotation.class)) {
System.out.println(c.getAnnotation(MyAnnotation.class).value());
} else {
System.out.println("这个构造器没有注释");
}
}

Method method = userAnnClass.getDeclaredMethod("getName");
if (method.isAnnotationPresent(MyAnnotation.class)) {
System.out.println(method.getAnnotation(MyAnnotation.class).value());
}

Method method1 = userAnnClass.getDeclaredMethod("setName", String.class);
Annotation[][] parAnn = method1.getParameterAnnotations();
for (Annotation[] par : parAnn) {
for (Annotation p : par) {
MyAnnotation my = (MyAnnotation) p;
System.out.println(my.value());
}
}
Field field = userAnnClass.getDeclaredField("name");
if (field.isAnnotationPresent(MyAnnotation.class)) {
System.out.println(field.getAnnotation(MyAnnotation.class).value());
}

}

}

/*
* 为节约篇幅 将所有的方法写到一起了 ElementType.CONSTRUCTOR 作用于构造器 ElementType.FIELD 作用于域/属性
* ElementType.METHOD 作用于方法 ElementType.PARAMETER 用于方法的参数 ElementType.TYPE
* 用于描述类、接口(包括注解类型) 或enum声明,最常用
*
* ElementType.PACKAGE 用于描述包 Package annotations must be in file
* package-info.java
* //ElementType.LOCAL_VARIABLE是方法中的本地变量。但是目前的javac不会在bytecode中的local
* variable中保存annotation信息,
* 所以就无法在runtime时获取该annotaion。也就是说ElementType.LOCAL_VARIABLE只能用在RetentionPolicy
* .SOURCE情况下。 ElementType.LOCAL_VARIABLE 方法中的本地变量
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.PACKAGE, ElementType.TYPE, ElementType.CONSTRUCTOR,
ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD })
@Inherited
@interface MyAnnotation {
String value() default "";
}

@MyAnnotation("我来自TYPE注解")
class UseMyAnnotation {
@MyAnnotation("我来自FIELD注解")
private String name = "苹果";
private int price = 0;

@MyAnnotation("我来自CONSTRUCTOR(无参)注解")
public UseMyAnnotation() {
}

@MyAnnotation("我来自CONSTRUCTOR(有参)注解")
public UseMyAnnotation(String name) {
this.name = name;
}

public UseMyAnnotation(String name, int price) {
this.name = name;
this.price = price;
}

@MyAnnotation("我来自METHOD注解")
public String getName() {
return name;
}

@MyAnnotation("我来自METHOD注解")
public void setName(@MyAnnotation("我来自PARAMETER注解") String name) {
this.name = name;
}

public int getPrice() {
return price;
}

public void setPrice(int price) {
this.price = price;
}
}

执行结果:

我来自TYPE注解
我来自CONSTRUCTOR(无参)注解
我来自CONSTRUCTOR(有参)注解
这个构造器没有注释
我来自METHOD注解
我来自PARAMETER注解
我来自FIELD注解

说明:JAVA注解是元数据 相当于对JAVA的方法、类、域进行标注。 如果不进行处理 那么相当于没有 ,如果进行处理,可以根据注解配置 可以用反射对原来的类、方法、域进行处理。

RetentionPolicy.RUNTIME 注解会在class字节码文件中存在,在运行时可以通过反射获取到,用的比较多,特别在系统架构的时候

RetentionPolicy.CLASS 默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得

RetentionPolicy.SOURCE 注解仅存在于源码中,在class字节码文件中不包含 @override 等
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: