由开发者自定义Annotation类型。 下面是一个使用annotation进行方法测试的sample: AnnotationDefineForTestFunction类型定义如下:
import java.lang.annotation.*;
//加载在VM中,在运行时进行映射
@Retention(RetentionPolicy.RUNTIME)
//限定此annotation只能标示方法
@Target(ElementType.METHOD)
public @interface AnnotationDefineForTestFunction{}
测试annotation的代码如下:
import java.lang.reflect.*;
public class UsingAnnotation {
@AnnotationDefineForTestFunction public static void method01(){}
public static void method02(){}
@AnnotationDefineForTestFunction public static void method03(){
throw new RuntimeException("method03");
}
public static void method04(){
throw new RuntimeException("method04");
}
public static void main(String[] argv) throws Exception{
int passed = 0, failed = 0;
//被检测的类名
String className="com.bjinfotech.practice.annotation.UsingAnnotation";
//逐个检查此类的方法,当其方法使用annotation声明时调用此方法
for (Method m : Class.forName(className).getMethods()) {
if (m.isAnnotationPresent(AnnotationDefineForTestFunction.class)) {
try {
m.invoke(null);
passed++;
} catch (Throwable ex) {
System.out.printf("测试 %s 失败: %s %n", m, ex.getCause());
failed++;
}
}
}
System.out.printf("测试结果: 通过: %d, 失败: %d%n", passed, failed);
}
}