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());
        }

    }

}


   学习中的一个完整的例子。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值