java元数据的学习

Annotation简介:
          Annotation提供一种机制,将程序中元素(如类、方法、属性等)和元数据联系起来。这样编译器可以将元数据保存的class文件中。代码分析工具就可以使用这些元数据执行的额外任务。注释采用“at”标记形式 ( @ ),后面是注释名称。
       自定义 annotationDebug.java
/**
 * 
 
*/
package annotation.test;

import java.lang.annotation.Documented;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
/**
 * 
@author windfree
 *
 
*/
    
/*
     * annotation类型是一种接口,能够通过java反射API的方式提供对其信息的访问
     
*/
    
/*
     * Documented 产生Java Doc文件,这次可以看到文件中包括了@Debug的信息
     
*/

    
/*
     * 限定 annotation 使用对象 - Target 
     * 在定义annotation时,使用java.lang.annotation.Target可以定义annotation使用的时机。
     
*/
    
/*
     * @Documented Java Doc文件
     
*/

    
/*
     * 
     * public enum ElementType {
     *   TYPE, // 适用 class, interface, enum
     *   FIELD, // 适用 field
     *   METHOD, // 适用 method
     *   PARAMETER, // 适用 method 上之 parameter
     *   CONSTRUCTOR, // 适用 constructor
     *   LOCAL_VARIABLE, // 适用区域变量
     *   ANNOTATION_TYPE, // 适用 annotation 型态
     *   PACKAGE // 适用 package
     *   } 
     
*/

    
/*
     * Retention 在你自定义的形态中,指定编译器如何处理自定义的annotation
     * 告知编译器如何处理 annotaion - Retention 
     
*/

    
/*
     * RetentionPolicy
     *         SOURCE        编译器处理完Annotation信息后就没事了
     *         CLASS        编译器将Annotation储存于class档中,预设
     *         RUNTIME        编译器将Annotation储存于class檔中,可由VM读入
     *                     使用java设计的一个程序代码工具,从VM中读出Annotation信息,已在分析程序中使用。
     *                     搭配Reflection机制,就可以实现。
     *         
     *         J2SE 5.0中新增了java.lang.reflect.AnnotatedElement这个接口,当中定义有四个方法:
     *                 public Annotation getAnnotation(Class annotationType);
     *                 public Annotation[] getAnnotations();
     *                 public Annotation[] getDeclaredAnnotations();
     *                 public boolean isAnnotationPresent(Class annotationType);
     *         Class、Constructor、Field、Method、Package等类别,都实作了 AnnotatedElement这个接口,
     *         所以您可以从这些类别的实例上,分别取得标示于其上的Annotation与其信息,
     *         如果 RetentionPolicy为RUNTIME的话。

     
*/

    
/*
     * @Inherited 子类是否继承父类的 annotation 
     
*/
@Documented
@Target({ElementType.CONSTRUCTOR, ElementType.METHOD})
//指定目标
@Retention(RetentionPolicy.RUNTIME)//设置保持性
@Inherited
public @interface Debug {
    
//定义了value()方法,编译器在编译时会自动帮您产生一个value的变量成员,接着在使用Debug Annotation时要指定值
    
//注释类型的数据成员被设置成使用有限信息进行工作
    
//定义数据成员不需要分别定义访问和修改的方法,只需定义一个方法,以数据成员的名称命名它,数据类型应该是该方法返回值的类型.
    String value()default "windfree";//默认值的类型必需与成员变量的申明类型一置
    String name();
}

在程序中使用自定义的annotation
   TestDebug.java
   
      
package annotation.test;

public class TestDebug {
    @Debug(
               name 
= "zgliu"
            )
     public void doSomething() {
        

        }

}

使用java中反射API读取class中的元数据的信息
      DebugTool.java:
      
         
package annotation.test;

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

public class DebugTool {
    
public static void main(String []args)throws NoSuchMethodException {
        Class
<TestDebug> c=TestDebug.class;
        Method method 
= c.getMethod("doSomething");
        
if(method.isAnnotationPresent(Debug.class)) {
            System.out.println(
"@Debug is found.");
            Debug debug 
= method.getAnnotation(Debug.class);
            System.out.println(
"\tvalue = " + debug.value());
            System.out.println(
"\tname = " + debug.name());
        }

        
else {
            System.out.println(
"@Debug is not found.");
        }


        Annotation[] annotations 
= method.getAnnotations();
        
for(Annotation annotation : annotations) {
            System.out.println(
                    annotation.annotationType().getName());
        }

    }

}


   学习中的一个完整的例子。
posted on 2006-12-12 10:51 windfree 阅读(1954) 评论(3)  编辑  收藏 所属分类: java

评论

# re: java元数据的学习 2007-05-04 15:07 BeanSoft  回复  更多评论   

学习了! 支持一下, 尤其是如何解析 Annotation 部分的代码. 受益了!

# re: java元数据的学习 2007-05-22 16:40 久城  回复  更多评论   

看完了,试了下。终于有了些思路。谢谢BZ。
只是有些疑惑,我们什么时候能用到annotation呢?

# re: java元数据的学习 2012-07-12 10:10 得到  回复  更多评论   

本草药王

只有注册用户登录后才能发表评论。


网站导航: