作者: 一字马胡
转载标志 【2018-01-04】
更新日志日期更新内容备注2018-01-04创建分析文档java技术干货分享
Java技术干货分享
如何快速获取或者判断一个类是否有某个注解呢?可以使用对象的Class信息来获取,下面是一个例子:
public class AnnotationDemo { public static boolean hasAnnotation(AnnotatedElement element,
Class extends Annotation> annotationType) { if (element.isAnnotationPresent(annotationType)) { return true;
} return false;
} public static void main(String[] args) {
Annotations annotations = new Annotations();
Class> cls = annotations.getClass(); if (hasAnnotation(cls, AnnotationTestA.class)) {
System.out.println("Annotation Test A");
} if (hasAnnotation(cls, AnnotationTestB.class)) {