1.2.4 Java Annotation 提要
(本文是介绍依赖注入容器Spring和分析JUnit源码的准备知识)
Java Annotation(标注)
java.lang.annotation.Annotation是全部Java标注的父接口。它除了override/改写Object的equals(Object)、hashCode()和toString()外,仅有一个方法
Class<? extends Annotation> annotationType()
返回本标注的的类型。@Target(value=METHOD)
@Retention(value=SOURCE)
public @interface Override{}再如java.lang. FunctionalInterface。@Documented @Retention(value=RUNTIME) @Target(value=TYPE) public @interface FunctionalInterface ......这些样例反映标注的一些基本内容:
有一些用于修饰自己定义标注的标注——称为元标注/meta-annotation,如Override样例中的@Target、@Retention和@Documented。其后是括号,括号里是使用逗号切割的名值对。(name=value)。
@Target说明被修饰的标注的适用场合/目标。
其值由枚举java.lang.annotation.ElementType限定,包含ANNOTATION_TYPE , CONSTRUCTOR , FIELD , LOCAL_VARIABLE , METHOD ,PACKAGE , PARAMETER , TYPE和TYPE_PARAMETER。
比如Target自己的适用场合是@Target(value=ANNOTATION_TYPE);假设标注仅仅有单一属性成员(名值对),而并成员名为"value="能够省略为@Target(ANNOTATION_TYPE)。
@Retention说明被修饰的标注的保留策略,由RetentionPolicy枚举。
- SOURCE仅仅会保留在程序源代码里。或者说仅对编译器有效,如@Override。编译器检查
- 默认或指定CLASS时,指定该标注写入class文件,可是不会把这些信息载入到JVM中;
- RUNTIME表示能够通过反射机制获得该标注。
可是,程序能够解析RUNTIME标注。并决定代码的行为。
样例:JUnit4自己定义的annotation
org.junit.Test
org.junit.Ignore @Target({ElementType.METHOD, ElementType.TYPE})
@Before和@After标示的方法仅仅能各有一个,代替了JUnit曾经版本号中的setUp和tearDown方法
org.junit.BeforeClass @Target(ElementType.METHOD)
org.junit.Before @Target(ElementType.METHOD)
org.junit.AfterClass @Target(ElementType.METHOD)
org.junit.After @Target(ElementType.METHOD)
org.junit.runner.RunWith
org.junit.runners.Suite.SuiteClasses
org.junit.runners.Parameterized.Parameters
解析RUNTIME标注
package MyTest;
import java.lang.annotation.*;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation{
String value();
}②标注的使用类package MyTest;
public class HelloWorld {
@MyAnnotation("Test")
public double add(double m,double n){
return m+n;
}
@MyAnnotation("Ignore")
public double add2(double m,double n){
return m+n;
}
}
package MyTest;
import java.lang.annotation.*;
import java.lang.reflect.*;
public class TestHelloWorld{
public static void main(String[] args)throws Exception{
//直接使用反射机制
Class<?> c = MyTest.HelloWorld.class;
HelloWorld h =(HelloWorld)c.newInstance();
Method[] methods = c.getMethods();
for(Method method:methods){
Annotation[] annotations = method.getDeclaredAnnotations();
for(Annotation annotation : annotations){
if(annotation instanceof MyAnnotation){
MyAnnotation myAnnotation = (MyAnnotation) annotation;
System.out.println("value: " + myAnnotation.value());
if(myAnnotation.value().equals("Test") ){
double d = (Double)method.invoke(h,1,2.0);
System.out.println(d);
}
}
}
}
}
}输出:3.0
value: Ignore
浙公网安备 33010602011771号