在Java中自定义Annotation的方法如下:
可通过反射获取annotation并进行操作:
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface CaseDesc {
public static enum Level {
Low(0), Normal(1), High(2);
private int value;
public int getValue() {
return value;
}
private Level(int value) {
this.value = value;
}
}
Level level();
String comment() default "";
}
可通过反射获取annotation并进行操作:
CaseDesc caseDesc = method.getAnnotation(CaseDesc.class);
if (caseDesc.level().getValue() >= level) {
System.out.println(caseDesc.comment());
}