Java Annotation

Annotation是JDK1.5的新特性,它能够让程序开发更加的快速方便,特别是体如今取代xml配置文件方面。
JDK1.5以后自带了三个已经实现的Annotation,分别是@Override, @Deprecated, @SuppressWarnings. 
对于自定义Annotation,编译器帮咱们实现了不少内部细节,好比实现了Annotation接口(固然还不止这个),咱们不用也不能再去实现
Annotation接口或者其余任何接口了。这是JDK的规范所规定的。
主要用到的API包括:
  • java.lang.reflect.AnnotatedElement 接口
  • public Annotation getAnnotation(Class annotationType)
  • public Annotation[] getAnnotations()
  • public Annotation[] getDeclaredAnnotations()
  • public boolean isAnnotationPresent(Class annotationType)
下面直接看例子:
package com.ec.test;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * 自定义Annotation
 * Retention是标注这个Annotation将被保持多久。其所保持的时间保存在RetentionPolicy中
 * 默认状况下的保持时间是RetentionPolicy.CLASS,即到class文件中,可是不被虚拟机所读取
 * RetentionPolicy.RUNTIME 是保持到class文件中 ,而且能够被虚拟机读取
 * RetentionPolicy.SOURCE 是只在编译期,事后即被废弃
 * @author nange
 * @version 1.0
 */
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
    String hello() default "nange";        //定义了一个hello字段,默认值为nange
    String world();        //定义了一个world字段,没有默认值

}
--------------------------------------------------------------------------------------------------------
package com.ec.test;

/**
 * 此类中有一个output方法上面使用了自定义的MyAnnotation
 * @author nange
 * @version 1.0
 */
public class MyTest {
    //对MyAnnotation中的属性赋值,将覆盖默认的值
    @MyAnnotation(hello="beijing", world="shanghai")
    public void output(int i, int j) {
        System.out.println("output something..." + i + j);
    }

}
--------------------------------------------------------------------------------------------------------
package com.ec.test;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
 * 定义MyReflection类,利用反射机制获得在MyTest类中的output
 * 方法上面使用的MyAnnotation
 * @author nange
 * @version 1.0
 */
public class MyReflection {

    public static void main(String[] args) throws Exception {
        MyTest myTest = new MyTest();//获得myTest对象,用于后面的invoke调用方法测试
        Class<MyTest> c = MyTest.class;//获得MyTest的类对象
        //获得output方法对象,getMethod方法的对一个参数是方法的名称,第二个参数是方法参数类型列表
        //保存在一个Class对象数组中
        Method method = c.getMethod("output", new Class[]{int.class, int.class});
        if (method.isAnnotationPresent(MyAnnotation.class)) {//判断在此方法上是否有MyAnnotation注解
            //利用反射执行这个方法,第一个参数是这个方法所在的对象,第二个参数是这个方法的执行的参数列表
            //保存在一个Object对象数组中
            method.invoke(myTest, new Object[]{1,2});
            MyAnnotation myAnnotation = method.getAnnotation(MyAnnotation.class);  //一样利用反射获得MyAnnotation对象
            String hello = myAnnotation.hello();//获得hello的值
            String world = myAnnotation.world();//获得world的值
            System.out.println(hello);//打印
            System.out.println(world);
        }
        Annotation[] annotations = method.getAnnotations();//获得在此方法上定义的所用注解对象
        for (Annotation annotation : annotations) {
            //循环遍历注解对象,一样利用反射机制,打印出这些注解的完整名称(包括包路径)
            System.out.println(annotation.annotationType().getName());
        }

    }

}
相关文章
相关标签/搜索