[size=large][b]Annotation注解[/b][/size]
一.如何声明Antotation
注解的声明很象接口的声明,只不过加上了@标示符在interface关键字前,在语法上需要注意:
[size=medium]1.方法声明不可以包含任何参数或是throws关键字.
2.返回类型限制于:基本类型,String,Class,enums,annotations,或是他们的数组形式,方法可以包含默认值.[/size]
请看下面代码
一旦你声明了注解类型,那么怎么使用呢?
注解是一种比较特殊的类型,你可以在class,interface,enums等之前使用它,按照默认规则,请你在修饰符前使用它,请看下面代码,它使用上面声明的annotations.
注解根据参数类型可以分为:
1.Marker Annotations(标示注解):一个没有任何elements(元素)的注解.
2.single-valued annotations(单列注解):只包含一个元素的注解.需要注意的是,元素必须被强制声明为:”value”.请看下面代码:
我们也许简单写法,请看下面代码,无需将value写出来.
3.文章开头声明的属于multi-valued annotations ,请自己看代码.
4.Retention Policies
注解可以包含以下三种retention策略:
[i]1.Source-file retention
Annotations with source-file retention are read by the compiler during the compilation process, but are not rendered in the generated .class files.
2.Class-file retention
This is the default retention policy. Annotations with class-file retention are read by the compiler and also retained in the generated .class files.
3.Runtime retention
Annotations with runtime retention are read by the compiler, retained in the generated .class files, and also made available at runtime. [/i]
5.我们可以在运行时来访问注解类型:提供了2个法:getAnnotationget,Annotations方法,还有一个检测isAnnotationsPresents,请看下面代码:
一.如何声明Antotation
注解的声明很象接口的声明,只不过加上了@标示符在interface关键字前,在语法上需要注意:
[size=medium]1.方法声明不可以包含任何参数或是throws关键字.
2.返回类型限制于:基本类型,String,Class,enums,annotations,或是他们的数组形式,方法可以包含默认值.[/size]
请看下面代码
public @interface requestForEnhancement{
int id();
String sysnopsis();
String engineer(); default ”[unassigend]”
String date(); default ”[unimplemented]”
}
一旦你声明了注解类型,那么怎么使用呢?
注解是一种比较特殊的类型,你可以在class,interface,enums等之前使用它,按照默认规则,请你在修饰符前使用它,请看下面代码,它使用上面声明的annotations.
@requestForEnhancement(
id = 2868724,
sysnopsis = ”Enable time travle”,
engineer = “Mr . Peabody”,
date = “4/1/3007”
)
public static void travelThoughTime(Date destnation){....}
注解根据参数类型可以分为:
1.Marker Annotations(标示注解):一个没有任何elements(元素)的注解.
public @interface Preliminary{}
2.single-valued annotations(单列注解):只包含一个元素的注解.需要注意的是,元素必须被强制声明为:”value”.请看下面代码:
public @interface Copyright {String value();}
我们也许简单写法,请看下面代码,无需将value写出来.
3.文章开头声明的属于multi-valued annotations ,请自己看代码.
4.Retention Policies
注解可以包含以下三种retention策略:
[i]1.Source-file retention
Annotations with source-file retention are read by the compiler during the compilation process, but are not rendered in the generated .class files.
2.Class-file retention
This is the default retention policy. Annotations with class-file retention are read by the compiler and also retained in the generated .class files.
3.Runtime retention
Annotations with runtime retention are read by the compiler, retained in the generated .class files, and also made available at runtime. [/i]
5.我们可以在运行时来访问注解类型:提供了2个法:getAnnotationget,Annotations方法,还有一个检测isAnnotationsPresents,请看下面代码:
//首先声明一个注解
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Test{}
//上面这种在注解内部嵌套注解的形式,SUN官方文档称之为meta-annotation(元数据注解)
//上面第一句话的意思是:确认在VM中保存他,以便用于反射,动态获取.
//第二句话的意思:这个注解只可以使用在方法上.
public class foo{
@Test public static void m1(){}
public static void m2(){}
@Test public static void m3(){
throw new RuntimeException("boom");
}
@Test public static void m5() { }
public static void m6() { }
@Test public static void m7() {
throw new RuntimeException("Crash");
}
public static void m8() { }
}
//下面写个测试类
import java.lang.reflect.*;
public class RunTests {
public static void main(String[] args) throws Exception {
int passed = 0, failed = 0;
for (Method m : Class.forName(args[0]).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++;
}
}
}
System.out.printf("Passed: %d, Failed %d%n", passed, failed);
}
}
//输出结果如下
$ java RunTests Foo
Test public static void Foo.m3() failed: java.lang.RuntimeException: Boom
Test public static void Foo.m7() failed: java.lang.RuntimeException: Crash
Passed: 2, Failed 2