Java Annotation(since 5.0)

Annotation是从JDK5.0开始被引入的新特性。下面是Java文档中对该特性的一段描述:

The Java platform has always had various ad hoc annotation mechanisms. For example the transient modifier is an ad hoc annotation indicating that a field should be ignored by the serialization subsystem, and the @deprecated javadoc tag is an ad hoc annotation indicating that the method should no longer be used. As of release 5.0, the platform has a general purpose annotation (also known as metadata) facility that permits you to define and use your own annotation types. The facility consists of a syntax for declaring annotation types, a syntax for annotating declarations, APIs for reading annotations, a class file representation for annotations, and an annotation processing tool. 

Annotation特性是关于元数据(meatdata)的使用的技术,元数据(meatdata)是关于数据的数据,meatdata可以用于创建文档,跟踪代码的依赖关系,甚至执行基本的编译时检查。meatdata的使用价值可以分为三大类:文档、编译器检查(Override)和代码分析。编译器检查的例子是Override,它表示该方法重载了父类的一个方法,如果不小心把方法名字写错了,而父类中没有这个方法,编译时就会报错。

JSR 175, annotations "do not directly affect the semantics of a program.Development and deployment tools can,however, read these annotations and process them in some fashion, perhaps producing additional Java programming language source files, XML documents, or other artifacts to be used in conjunction with the program containing the annotations."

下面是一个使用Annotation的例子。

定义Annotation:

/**
 * 
 
*/

package  cn.ac.ict.hla.annotation;

import  java.lang.annotation. * ;
import  java.lang.annotation.Target;

/**
 * 
@author Jackie
 *
 * Indicates that the annotated method is a test method.
 * This annotation should be used only on parameterless static methods.
 
*/


@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public  @ interface  Test  {

}

被自定义的Annotation标注的类

/**
 * 
 
*/

package  cn.ac.ict.hla.annotation;

/**
 * 
@author Jackie
 *
 
*/

public   class  Foo  {
    
    @Test 
public static void m1() { }
    
public static void m2() { }
    @Test 
public static void m3() {
        
throw new RuntimeException("Boom");
    }

    
public static void m4() { }
    @Test 
public static void m5() { }
    
public static void m6() { }
    @Test 
public static void m7() {
        
throw new RuntimeException("Crash");
    }

    
public static void m8() { }

    
/**
     * 
     
*/

    
public Foo() {
        
// TODO Auto-generated constructor stub
    }


    
/**
     * 
@param args
     
*/

    
public static void main(String[] args) {
        
// TODO Auto-generated method stub

    }


}

使用被Annotation标注的类

/**
 * 
 
*/

package  cn.ac.ict.hla.annotation;

import  java.lang.reflect.Method;

/**
 * 
@author Jackie
 *
 
*/

public   class  RunTests  {

    
/**
     * 
@param args
     
*/

    
public static void main(String[] args) {
        
int passed = 0, failed = 0;
          
try {
            
for (Method m : Class.forName("cn.ac.ict.hla.annotation.Foo").getMethods()) {
                 
if (m.isAnnotationPresent(Test.class)) {
                    
try {
                       m.invoke(
null);
                       passed
++;
                    }
 catch (Throwable ex) {
                       System.out.printf(
"Test %s failed: %s %n", m, ex.getCause());
                       failed
++;
                    }

                 }

              }

        }
 catch (SecurityException e) {
            
// TODO Auto-generated catch block
            e.printStackTrace();
        }
 catch (ClassNotFoundException e) {
            
// TODO Auto-generated catch block
            e.printStackTrace();
        }

          System.out.printf(
"Passed: %d, Failed %d%n", passed, failed);
    }


}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值