通过反射取得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的更多相关文章

随机推荐

  1. 【HDOJ】2822 Dogs

    bfs. /* 2822 */ #include <iostream> #include <cstdio> #include <cstring> #include ...

  2. java学习之数组排序一:选择排序

    在讲完java中数组定义的两种方式之外,这里需要讲一下对数组常用的方法,第一个就是排序. 加入我们现在有个数组:int[] arr = {12,87,34,3,98,33,103}; 思路1: 1.首 ...

  3. 五种I/O 模式——阻塞(默认IO模式),非阻塞(常用语管道),I/O多路复用(IO多路复用的应用场景),信号I/O,异步I/O

    五种I/O 模式——阻塞(默认IO模式),非阻塞(常用语管道),I/O多路复用(IO多路复用的应用场景),信号I/O,异步I/O 五种I/O 模式:[1]        阻塞 I/O          ...

  4. 有关linux下redis overcommit_memory的问题(转)

    一.背景 公司的redis有时background save db不成功,通过log发现下面的告警,很可能由它引起的: [13223] 17 Mar 13:18:02.207 # WARNING ov ...

  5. (转载)PHP中刷新输出缓冲

    (转载)http://www.cnblogs.com/mutuan/archive/2012/03/18/2404957.html PHP中刷新输出缓冲buffer是一个内存地址空间,Linux系统默 ...

  6. 【离线】【深搜】【树】Codeforces 707D Persistent Bookcase

    题目链接: http://codeforces.com/problemset/problem/707/D 题目大意: 一个N*M的书架,支持4种操作 1.把(x,y)变为有书. 2.把(x,y)变为没 ...

  7. HDOJ(HDU) 2161 Primes(素数打表)

    Problem Description Write a program to read in a list of integers and determine whether or not each ...

  8. 浅谈JavaScript函数

    JavaScript作为一种基于对象(非严格面向对象)的语言,函数在JS中的地位非同一般:用函数声明类和对象.甚至函数本身也是对象. 一.函数的三种声明方式辨析. 1.function命令 funct ...

  9. 代码审查工具之PMD操作指南

    上周客户要求对OA系统的代码质量进行了一个整体审查,并且要出一份报告给领导. 为此花了半天时间把代码审查工具PMD琢磨了下,现将具体操作步骤描述如下,以供大家参考! 1 前言 质量是衡量一个软件是否成 ...

  10. 对List

     class MyCompare implements Comparator//自定义比较方式 要实现Conparator的 compare 方法 {     public int compare(O ...