ITPub博客

首页 > IT职业 > IT职场 > Java如何快速获取类附带的注解

Java如何快速获取类附带的注解

IT职场 作者:mpsky 时间:2021-09-09 09:21:12 0 删除 编辑

作者: 一字马胡  
转载标志 【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)) {
            System.out.println("Annotation Test B");
        }        if (hasAnnotation(cls, AnnotationTestC.class)) {
            System.out.println("Annotation Test C");
        }
    }

}@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@interface AnnotationTestA {}@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@interface AnnotationTestB {}@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@interface AnnotationTestC {}@AnnotationTestA@AnnotationTestB@AnnotationTestCclass Annotations {}

在上面的例子中,hasAnnotation方法的作用就是判断是否一个对象所属的类包含某个注解,可以分析一下具体的实现,首先,第一个参数类型为AnnotatedElement,代表具体需要判断的对象Class信息,需要注意的是,Class类实现了AnnotatedElement接口,所以传递一个Class对象给这个方法的第一个参数是合法的;第二个参数是注解类的Class信息,是需要检测的目标注解。可以看到,在代码中使用了AnnotatedElement的isAnnotationPresent方法来进行判断是否具有某个注解的功能,可以跟进去看一下具体的实现原理。

图片描述

    default boolean isAnnotationPresent(Class extends Annotation> annotationClass) {        return getAnnotation(annotationClass) != null;
    }    public  A getAnnotation(Class annotationClass) {
        Objects.requireNonNull(annotationClass);        return (A) annotationData().annotations.get(annotationClass);
    }

可以看一下annotationData方法的具体实现,参考下面的图片:

图片描述

其中首先会判断是否具有需要的数据,以及是否符合要求,如果数据不存在或者过期了,那么就需要重新获取,重新获取数据的关键方法是createAnnotationData,该方法的实现可以参考下面的代码:图片描述

annotationData方法获取到的是一个AnnotationData对象,可以看一下AnnotationData类的定义:

    // annotation data that might get invalidated when JVM TI RedefineClasses() is called
    private static class AnnotationData {        final Map, Annotation> annotations;        final Map, Annotation> declaredAnnotations;        // Value of classRedefinedCount when we created this AnnotationData instance
        final int redefinedCount;

        AnnotationData(Map, Annotation> annotations,
                       Map, Annotation> declaredAnnotations,                       int redefinedCount) {            this.annotations = annotations;            this.declaredAnnotations = declaredAnnotations;            this.redefinedCount = redefinedCount;
        }
    }

在getAnnotation方法中,该方法获取了AnnotationData字段的annotations字段来判断是否包含目标注解,更加深入的细节可以自行参考Class类的具体实现。本文是一个极短的java技术分享,提供一种获取一个对象的注解信息的思路,其实还有很多类型信息都是可以通过Class来获取到的,这方面的内容可以挖掘很多,日后会多往这方面做文章。




来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1806/viewspace-2809634/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: PHP面试01
请登录后发表评论 登录
全部评论

注册时间:2019-07-18

  • 博文量
    115
  • 访问量
    14709