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 等
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 等
相关文章推荐
- “老人之心”
- 国家气象局提供的天气预报接口及使用说明
- MatLab2012b/MatLab2013b 分类器大全(svm,knn,随机森林等)
- apache+tomcat集群说明(一)
- 安装 VS2012/2013 完成时报错“无法建立到信任根颁发机构的证书链”
- Swift IOS中的常用操作(打开网页、发短信、打电话、发邮件)
- IDEA快捷键
- 从2-3-4树谈到Red-Black Tree(红黑树) http://www.cnblogs.com/guoyiqi/archive/2011/06/08/2129310.html
- IOS图层Layer学习笔记(四)—— CAShapeLayer
- LSA潜在语义分析
- 自学cocos2d js游戏开发应该按什么步骤进行
- JavaScript String 字符串类型
- 青岛理工大学邀请赛总结
- 高性能服务器运行程序配置文件的使用
- What does information_schema.TABLES.DATA_FREE mean in MySQL?
- Android 图片优化
- android资源目录之drawable
- Akka性能测试
- Spring+Quartz实现定时任务的配置方法
- JS和AS交互