首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Java自定义诠注(原理和API)初探

2013-08-04 
Java自定义注解(原理和API)初探?}??@Override?public String toString() {??return MyTest [field + fi

Java自定义注解(原理和API)初探
?}
?
?@Override
?public String toString() {
??return "MyTest [field=" + field + "]";
?}

?/**
? * 测试方法
? * @param args
? */
?public static void main(String[] args) throws Exception {
??// 实例化对象
??MyTest test = new MyTest();
??// 获取Class类型
??Class cls = MyTest.class;
??// 根据方法名获取方法
??Method method = cls.getDeclaredMethod("method", null);
??// 获取属性
??Field field = cls.getDeclaredField("field");
??// 判断如果该方法中包含有MyAnnotation类型的注解,则进行如下操作
??if (method.isAnnotationPresent(AnnotationTest.class)) {
???// 取消Java访问性检查
???field.setAccessible(true);
???method.setAccessible(true);
???// 给test对象上的field属性赋值,并执行method方法
???field.set(test, "我是肖云,大家好!");
???method.invoke(test, null);
???
???// 根据注解类型获取特定的注解并打印到控制台
???Annotation annotation = method.getAnnotation(AnnotationTest.class);
???System.out.println(annotation.annotationType() + "\n" + annotation.annotationType().getName());
??}
??
??// 获取所有的注解并逐次迭代(这需要注解中Retention中的value为RetentionPolicy.Runntime)
??Annotation[] annotations = method.getAnnotations();
??if (annotations.length > 0) {
???System.out.println("------------打印所有注解--------------");
???for (Annotation annotation : annotations) {
????System.out.println(annotation.annotationType().getCanonicalName());
???}
??}
?}
}
??? 上面一个例子主要说明的是java.lang.annotation.Annotation接口中的Retention属性(JVM是否保留注释,是否可用反射读取)TargetAnnotationDemo ,接下来第二个例子用到了剩下的三个属性(Documented(不是重点,生成API文档时用),Target,Inherited),下面开始介绍:

首先还是定义注解:

TargetAnnotationDemo.java

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
?* @Target注解
?*
?* @author XiaoYun 2013-07-20
?*/
@Target({ElementType.TYPE, ElementType.METHOD})? //程序类型的元素有类、接口、枚举类型;方法
@Retention(RetentionPolicy.RUNTIME) //运行时可用反射调用
@Inherited //子类可以继承父类的注解
@Documented //生成doc文档时显示该注解
public @interface TargetAnnotationDemo {
?String value() default "hello";
?String value1();
}

第二步,写测试用例:

@TargetAnnotationDemo(value1 = "good")
public class TargetTest {
?@TargetAnnotationDemo(value1 = "good")
?public int add(int a, int b) {
??return a + b;
?}
}

/**

*子类,继承自TargetTest ,同时继承父类的TargetAnnotationDemo

*@author xiaoyun 2013-07-21

?

*/
class TargetTestChild extends TargetTest {
?@Override
?public int add(int a, int b) {
??// TODO Auto-generated method stub
??return super.add(a, b);
?}
}

第三步:

写测试类,TestTarget.java

import java.lang.annotation.Annotation;

public class TestTarget {
?public static void main(String[] args) {
??//实例化父类
??TargetTest parent = new TargetTest();
??//实例化子类
??TargetTestChild child = new TargetTestChild();
??//获取父类的类型
??Class cls = TargetTest.class;
??//获取子类的类型
??Class childCls = TargetTestChild.class;
??//获取父类的注解
??Annotation[] pAnnotations = cls.getAnnotations();
??if (pAnnotations.length > 0) {
???System.out.println("父类的注解:");
???for (Annotation annotation : pAnnotations) {
????System.out.println(annotation.annotationType().getName());???
???}
??}
??//获取子类的注解
??Annotation[] cAnnotations = childCls.getAnnotations();
??if (cAnnotations.length > 0) {
???System.out.println("子类的注解:");
???for (Annotation annotation : cAnnotations) {
????System.out.println(annotation.annotationType().getName());
???}
??}
?}
}

? 该例子主要用到了java.lang.annotation.Annotation中的所有注解类型。对于Retention,Target,Inherited和Documented这是个注解类型,需要重点掌握前三个,第四个很少用到。

?? 这上面的两个例子都能正常运行,我的环境依然是,JDK1.6。对于生成doc文档的问题,你也可以通过javadoc命令来生成,但是如果你的开发工具时myeclipse8.6的话,可以通过project->Generate Javadoc来生成doc文档,通过去掉和加上@Documented注解类型来观察在文档中是否有对应的说明(当然去掉或加上后需要重新生成下)。

???这次先写到这里,关于注解在程序和框架中的应用案例,后面等我学习后会进行更新和发表,谢谢各位同行关注。

??? 欢迎各位同行和前辈们提出问题,对程序或者我组织的语言进行批评指导。

?

?

?

热点排行