Java魔法堂:自定义和解析注解

一、前言                            

  注解(Annotation)作为元数据的载体,为程序代码本身提供额外的信息,使用过 MyBatis 等 ORM 框架的朋友对 @Insert 的注解应该不陌生了,这是 MyBatis 自定义的注解,显然我们也可以按需求自定义一些注解,然后对其进行解析获取元数据,进而实现通过代码生成代码的操作。

 

二、自定义注解                         

 只需通过 关键字 @interface 即可自定义注解

// 标识注解(就是无属性的注解)
public @interface AnnotationWithoutProperty{
}

// 带 value 属性的注解
public @interface AnnotationWithVal{
String value();
}

// 带 myVal 属性的注解
public @interface AnnotationWithMyVal{
String[] myValue();
}

// 带 value 和 myVal 属性的注解
public @interface AnnotationWith2Val{
String value();
String[] myValue();
}

// 带缺省值的 myVal 属性的注解
public @interface AnnotationWithDefaultVal{
String myVal()
default "hello world!";
}

使用方式如下:

@AnnotationWithoutProperty
@AnnotationWithVal("hello world") // value 属性赋值时,不用显式写出属性名
@AnnotationWithMyVal(myValue={"hello", "world"}) // 其他属性赋值时,必须显示写出属性名
@AnnotationWith2Val(value="hello world", myVal={"hello", "world"})
@AnnotationWithDefaultVal // 属性拥有缺省值时,不必显示设置属性值
@AnnotationWithDefaultVal("new value")
public void test(){}

 

三、注解的注解                              

  注解的注解就是为注解本身提供额外的信息,从而约束或增强注解的能力。其中包含有 @Documented 、 @Inherited 、 @Target 、 Retention 4 种注解。

   @Target 注解 :用于约束被描述的注解的使用范围,当被描述的注解超出使用范围则编译失败。

// 约束 @MyAnnotation 的作用范围是函数和构造函数
@Target(ElementType.METHOD, ElementType.CONSTRUCTOR)
public @interface MyAnnotation{}

   @Retention 注解 :用于约束被描述的注解的作用范围,注解的作用范围有三个,分别为

        1.  RetentionPolicy.SOURCE ,作用范围为源码,就是仅存在于 java 文件中,当执行 javac 命令时将会去除该注解。

        2.  RetentionPolicy.CLASS ,作用范围为二进制码,就是存在于 class 文件中,当执行 java 命令时会去除该注解。

        3.  RetentionPolicy.RUNTIME ,作用范围为运行时,就是我们可以通过反射动态获取该注解。

@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation{}

    @Documented 注解 :用于指定 javadoc 生成 API 文档时显示该注解信息

    @Inherited 注解 :用于指定被描述的注解可以被其所描述的类的子类继承。默认情况

// 默认注解不会被子类继承
@MyAnnotation
public class Parent{}

// Son 并没有继承注解 MyAnnotation
public class Son extends Parent{}

通过 @Inherited 子类将会继承父类的 @MyAnnoation 注解 。

 

四、读取注解                                  

  通过反射我们可以获取类、函数等上的注解信息。

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.CLASS)
@Documented
public @interface MyAnnotaion{String value() default "hello world";
}

@MyAnnotation
public class Test{
public static void main(String[] args){
MyAnnotation ma
= Test.class.getAnnotation(MyAnnotation.class);
System.
out.println(ma.value());

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取自身和从父类继承的注解</span>
Annotation[] annotations = Test.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">.getAnnotations();
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 仅获取自身的注解</span>
Annotation[] annotations = Test.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">.getDeclaredAnnotations();</span><span style="color: rgba(0, 0, 0, 1)">

}
}

尊重原创,转载请注明来自:http://www.cnblogs.com/fsjohnhuang/p/4040929.html ^_^ 肥仔 John

 

五、参考                                

http://www.cnblogs.com/liubiqu/archive/2008/06/01/1211503.html