java_annotation_02
通过反射取得Annotation
在一上节中,我们只是简单的创建了Annotation,如果要让一个Annotation起作用,则必须结合反射机制,在Class类上存在以下几种于Annotation有关的方法
-- public <A extends Annotation> A getAnnotation(class<A> annotationClass)
如果在一个元素中存在注释,则取得全部注释
-- public Annotation[] getAnnotations()
返回此元素上所有注释
-- public Annotation[] getDeclaredAnnotations()
返回直接存放在此元素上的所有注释
-- public boolean isAnnotation()
判断元素是否表示一个注释
-- public boolean isAnnotationPersent(Class<? extends Annotation> annotationClass)
判断一个元素上是否存在注释
例一:取得全部的Annotation
1.定义一个具体3个Annotation的类
public class MyTest{
@SuppressWarnings("unchecked")
@Deprecated
@Override
public String sayHello(){
return "Hello WangYang!!!" ;
}
};
2.想要出得上面的Annotation,则必须首先找到他们属于的方法
import java.lang.annotation.Annotation ;
import java.lang.reflect.Method ;
public class ReflectTest{
public static void main(String args[]) throws Exception{ // 所有异常抛出
Class <?> c = null ;
c = Class.forName("com.wy.MyTest") ;
Method toM = c.getMethod("sayHello") ; // 找到sayHello()方法
Annotation an[] = toM.getAnnotations() ; // 取得全部的Annotation
for(Annotation a:an){ // 使用 foreach输出
System.out.println(a) ;
}
}
};
注意:
上面虽然用了3个Annotation,但是最后真正得到的只有一个.这是因为只有@Deprecated使用了RUNTIME的声明方式
例二:我们不单单要取得了一个元素所声明的全部RUNTIME的Annotation,有时需要取得的是某个指定的Annotation,所以此时在取得之前就必须进行明确的判断,使用isAnnotationPresent()方法进行判断.
1.同样定义一个RUNTIME的Annotation
import java.lang.annotation.Retention ;
import java.lang.annotation.RetentionPolicy ;
@Retention(value=RetentionPolicy.RUNTIME) // 此Annotation在类执行时依然有效
public @interface MyAnnotation{
public String key() default "wang" ;
public String value() default "yang" ; }
2.定义一个类,并使用自定义的Annotation
public class MyAnnotationTest2{
@SuppressWarnings("unchecked")
@Deprecated
@Override
@MyDefaultAnnotationReflect(key="wang",value="www.yang.cn")
public String sayHello(){
return "Hello WangYang!!!" ;
}
};
3.取得指定的Annotation
import com.wy.MyAnnotation ;
import java.lang.annotation.Annotation ;
import java.lang.reflect.Method ;
public class MyTest{
public static void main(String args[]) throws Exception{ // 所有异常抛出
Class <?> c = null ;
c = Class.forName("com.wy.MyAnnotationTest2") ;
Method toM = c.getMethod("sayHello") ; // 找到sayHello()方法
if(toM.isAnnotationPresent(MyAnnotation .class)){
// 判断是否是指定的Annotation
MyAnnotation mda = null ;
mda = toM.getAnnotation(MyAnnotation .class) ; // 得到指定的Annotation
String key = mda.key() ; // 取出设置的key
String value = mda.value() ; // 取出设置的value
System.out.println("key = " + key) ;
System.out.println("value = " + value) ;
}
}
};
以上的方式,我们加以合理的应用,就能实现我们想要的注释
java_annotation_02的更多相关文章
随机推荐
- 数学计数原理(Pólya,高精度):SGU 294 He's Circles
He's Circles He wrote n letters "X" and "E" in a circle. He thought that there ...
- bzoj 1191 [HNOI2006]超级英雄Hero(最大基数匹配)
1191: [HNOI2006]超级英雄Hero Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2813 Solved: 1331[Submit][ ...
- [Locked] Missing Ranges
Missing Ranges Given a sorted integer array where the range of elements are [lower, upper] inclusive ...
- Linux 环境变量 $PATH
我们知道查阅文件属性的指令 ls 完整文件名为:/bin/ls(这是绝对路径),那为什么可以在任何地方执行/bin/ls 这个指令呢? 为什么在任何目录下输入 ls 就一定可以显示出一些讯息而不会说 ...
- .NET中的程序集(Assembly)
在.NET 中,新引入了一个程序集的概念,就是指经由编译器编译得到的,供CLR进一步编译执行的那个中间产物,在WINDOWS系统中,它一般表现为.dll,或者是.exe的格式,但是要注意,它们跟普通意 ...
- C++基础复习
1.Object-C也是面向对象的语言:2.#include<iostream> //#include是一个预处理指令3.using namespace std; //std是命名空间,u ...
- PHP学习之[第06讲]数组、多维数组和数组函数
一.数组 ①Array(“aa”,12,true,2.2,”test”,50); ②Array(“title”=>“aa”, ”age”=>20); 1.创建: $arr= array( ...
- 利用MyEclipse配置S2SH三大框架篇-Spring配置
1.配置完Struts2后,然后配置Spring 2.单击“MyEclipse->Project Capabilities->Add Spring Capabilities” 3.选择Sp ...
- MFC和Qt优缺点
在网上看到的,拿来和大家一起讨论下. 我曾经使用过来开发过软件,我想和大家分享我使用他们时所体会的不同之处. 我并非一个职业作家,这篇文章可能看起来不如专业的杂志和网站上的那么条理清晰.但是,我在这里 ...
- mysql @变量和变量的区别及怎么判断记录唯一性
DELIMITER// drop PROCEDURE if EXISTS test.express; create PROCEDURE test.express() BEGIN ) into @a f ...