编写Annotation:
package com.eric.news.cha06;
import Java.lang.annotation.Documented;
import Java.lang.annotation.Inherited;
import Java.lang.annotation.Retention;
import Java.lang.annotation.RetentionPolicy;
@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface GroupTODO {
public enum Severity {CRITICAL, IMPORTANT, TRIVIAL, DOCUMENTATION};
Severity severity() default Severity.IMPORTANT;
String item();
String assignedTo();
}
---------------------
运用annotation:
package com.eric.news.cha06;
@InProgress
public class TestInProgress {
@GroupTODO(
severity=GroupTODO.Severity.CRITICAL,
item="Figure out the amount of interest per month",
assignedTo="Eric Zhou"
)
public void calculateInterest(float amount, float rate) {
}
}
---------------------
测试:
package com.eric.news.cha06;
import Java.lang.reflect.Method;
public class TestAnnotation {
public static void main(String[] args) throws SecurityException, NoSuchMethodException {
Class c = TestInProgress.class;
Method element = c.getMethod("calculateInterest", float.class, float.class);
GroupTODO groupTodo = element.getAnnotation(GroupTODO.class);
String assignedTo = groupTodo.assignedTo();
System.out.println(assignedTo);
System.out.println(groupTodo.item());
}
}
---------------------
结果:
Eric Zhou
Figure out the amount of interest per month
posted on 2008-04-30 15:25
周锐 阅读(192)
评论(0) 编辑 收藏 所属分类:
Java