`
chenxu_8456
  • 浏览: 40690 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

Java自定义Annotation

 
阅读更多
在Java中自定义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());
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics