/**
* 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);
}
}
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器