当前页面: 开发资料首页 → Java 专题 → Java Annotation入门
摘要: Java Annotation入门
/**
* Describes the Request-For-Enhancement(RFE) that led
* to the presence of the annotated API element.
*/
public @interface RequestForEnhancement {
int id();
String synopsis();
String engineer() default "[unassigned]";
String date(); default "[unimplemented]";
}
@RequestForEnhancement(
id = 2868724,
synopsis = "Enable time-travel",
engineer = "Mr. Peabody",
date = "4/1/3007"
)
public static void travelThroughTime(Date destination) { ... }
/**
* Indicates that the specification of the annotated API element
* is preliminary and subject to change.
*/
public @interface Preliminary { }
@Preliminary public class TimeTravel { ... }
/**
* Associates a copyright notice with the annotated API element.
*/
public @interface Copyright {
String value();
}
@Copyright("2002 Yoyodyne Propulsion Systems")
public class OscillationOverthruster { ... }
import java.lang.annotation.*;
/**
* 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 { }
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() { }
}
Here is the testing tool:
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
package com.bjinfotech.practice.annotation;
/**
* 演示如何使用java5内建的annotation
* 参考资料:
* http://java.sun.com/docs/books/tutorial/java/javaOO/annotations.html
* http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html
* http://mindprod.com/jgloss/annotations.html
* @author cleverpig
*
*/
import java.util.List;
public class UsingBuiltInAnnotation {
//食物类
class Food{}
//干草类
class Hay extends Food{}
//动物类
class Animal{
Food getFood(){
return null;
}
//使用Annotation声明Deprecated方法
@Deprecated
void deprecatedMethod(){
}
}
//马类-继承动物类
class Horse extends Animal{
//使用Annotation声明覆盖方法
@Override
Hay getFood(){
return new Hay();
}
//使用Annotation声明禁止警告
@SuppressWarnings({"deprecation","unchecked"})
void callDeprecatedMethod(List horseGroup){
Animal an=new Animal();
an.deprecatedMethod();
horseGroup.add(an);
}
}
}
package com.bjinfotech.practice.annotation;
import java.lang.annotation.*;
/**
* 定义annotation
* @author cleverpig
*
*/
//加载在VM中,在运行时进行映射
@Retention(RetentionPolicy.RUNTIME)
//限定此annotation只能标示方法
@Target(ElementType.METHOD)
public @interface AnnotationDefineForTestFunction{}
package com.bjinfotech.practice.annotation;
import java.lang.reflect.*;
/**
* 一个实例程序应用前面定义的Annotation:AnnotationDefineForTestFunction
* @author cleverpig
*
*/
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);
}
}