Annotation之系统内建的 注释Annotation的注释



java Annotation之系统内建的 注释Annotation的注释。

一、@Target注释之前定义的Annotation,如果明确说明,可以在任意位置使用。

例:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
@MyTargetAnntation(key=”MLDN”,value=”www.mldnjava.cn”)
public class SimpleBean{
@MyTargetAnntation(key=”MLDN”,value=”www.mldnjava.cn”)
public String toString(){
return “Hello LiXingHua!!!” ;
}
};

如果现在需要指定其使用范围的话,则必须使用@Target注释。
@Target(value=Element.属性)

Element的保存范围

No. 范围 描述
1 ANNOTATION_TYPE 只能用在注释上
2 CONSTRUCTOR 只能用在构造方法声明上
3 FIELD 只能用在属性(包括枚举常量)上
4 LOCAL_VARIABLE 只能用在局部变量上
5 METHOD 只能用在方法上
6 PACKAGE 只能用在包的声明上
7 PARAMETER 只能用在参数上
8 TYPE 只能用在类、接口、枚举上 如果想让Annotation可以使用在多个地方,则必须同时制定多个值
例:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
import java.lang.annotation.Target ;
import java.lang.annotation.ElementType ;
import java.lang.annotation.Retention ;
import java.lang.annotation.RetentionPolicy ;
@Target({ElementType.TYPE,ElementType.METHOD}) // 此注释只能用在类上
@Retention(value=RetentionPolicy.RUNTIME)
public @interface MyTargetAnntation{
public String key() default “LXH” ;
public String value() default “李兴华” ;
}


二、@Documented注释
@Documented可以在任何Annotation上使用,所有的Annotation默认情况下都是使用@Documented进行注释的,而且在生成javadoc的时候可以通过@Documented设置一些说明信息。

例:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
import java.lang.annotation.Documented ;
@Documented
public @interface MyDocumentedAnntation{
public String key() default “LXH” ;
public String value() default “李兴华” ;
}
成功之后,在使用此Annotation的时候可以增加一些信息上去
[java] view plaincopy在CODE上查看代码片派生到我的代码片
@MyDocumentedAnntation(key=”MLDN”,value=”www.mldnjava.cn”)
public class SimpleBeanDocumented{
/**
* 此方法在对象输出时调用,返回对象信息
*/
@MyDocumentedAnntation(key=”MLDN”,value=”www.mldnjava.cn”)
public String toString(){
return “Hello LiXingHua!!!” ;
}
};
之后通过javadoc的命令,生成java.doc的文档,便可以看到已添加的注释说明。

三、@inherited注释

该注释表明一个Annotation是否可以被继承下来

例:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
import java.lang.annotation.Retention ;
import java.lang.annotation.RetentionPolicy ;
import java.lang.annotation.Documented ;
import java.lang.annotation.Inherited ;
@Documented
@Inherited
@Retention(value=RetentionPolicy.RUNTIME)
public @interface MyInheritedAnnotation{
public String name() ;

}
定义一个父类,在此父类上使用Annotation
[java] view plaincopy在CODE上查看代码片派生到我的代码片
package org.lxh.demo16.inheriteddemo ;
@MyInheritedAnnotation(name=”李兴华”)
public class Person{
};
然后定义一个子类继承该父类
[java] view plaincopy在CODE上查看代码片派生到我的代码片
package org.lxh.demo16.inheriteddemo ;
public class Student extends Person{
};
然后写程序来验证是否该子类继承了该父类
[java] view plaincopy在CODE上查看代码片派生到我的代码片
import java.lang.annotation.Annotation ;
import org.lxh.demo16.inheriteddemo.MyInheritedAnnotation ;
public class ReflectInheritedDemo{
public static void main(String args[]) throws Exception{
Class<?> c = null ;
c = Class.forName(“org.lxh.demo16.inheriteddemo.Student”) ;
Annotation ann[] = c.getAnnotations() ; // 取得全部的Annotation
for(Annotation a:ann){ // 输出
System.out.println(a) ;
}
// 继续取得此Annotation设置的内容
if(c.isAnnotationPresent(MyInheritedAnnotation.class)){
MyInheritedAnnotation mda = null ;
mda = c.getAnnotation(MyInheritedAnnotation.class) ;
String name = mda.name() ; // 取出name的内容
System.out.println(“name = ” + name) ;
}
}
}
运行程序可以发现 通过@inherited注释的Annotation确实可以被继承….