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;
/** */
/** *@authorJackie
*
* 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;
/** */
/** *@authorJackie
*
*/
public
class
Foo
...
{
@Testpublicstaticvoidm1()...{ }
publicstaticvoidm2()...{ }
@Testpublicstaticvoidm3()...{
thrownewRuntimeException("Boom");
}
publicstaticvoidm4()...{ }
@Testpublicstaticvoidm5()...{ }
publicstaticvoidm6()...{ }
@Testpublicstaticvoidm7()...{
thrownewRuntimeException("Crash");
}
publicstaticvoidm8()...{ }
/** *//** *
*/
publicFoo()...{
//TODO Auto-generated constructor stub}
/** *//** *@paramargs
*/
publicstaticvoidmain(String[] args)...{
//TODO Auto-generated method stub }
}
使用被Annotation标注的类
/** */
/** *
*/
package
cn.ac.ict.hla.annotation;
import
java.lang.reflect.Method;
/** */
/** *@authorJackie
*
*/
public
class
RunTests
...
{
/** *//** *@paramargs
*/
publicstaticvoidmain(String[] args)...{
intpassed=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 blocke.printStackTrace();
}catch(ClassNotFoundException e)...{
//TODO Auto-generated catch blocke.printStackTrace();
} System.out.printf("Passed: %d, Failed %d%n", passed, failed);
}
}