随笔 - 145  文章 - 0  评论 - 6  阅读 - 18万

元注解之@Repeatable

简介

@Repeatable注解从JDK 1.8开始引入,为了解决注解不能在同一个地方重复使用而出现的。在JDK 1.8以前,下面这段代码将不能通过编译。

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Identity {

    String value();
}

// 不能重复使用
@Identity("父亲")
@Identity("教师")
public class RepeatableTest {

}

使用

先定义注解

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(Identities.class)
@Documented
public @interface Identity {

    String value();
}

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Identities {

    Identity[] value();
}

为了使得@Identity注解可以重复使用,需要注意以下几点。

  • 在定义时用@Repeatable标记一个容器注解@Identities
  • 该容器注解@Identities必须要声明一个value方法,并且返回值是一个Identity[]类型,否则编译报错。

测试类

@Identity("父亲")
@Identity("教师")
public class RepeatableTest {

    public static void main(String[] args) {
        // 获取注解信息
        // 值得注意的是参数传的是Identities.class,而不是Identity.class。
        // 原因看下面原理介绍
        Identities identitiesAnnotation = RepeatableTest.class.
            getAnnotation(Identities.class);
        Identity[] identities = identitiesAnnotation.value();
        Arrays.stream(identities).forEach(identity -> 
           System.out.println(identity.value()));
    }
}

原理

如果我们将编译后的RepeatableTest.class文件反编译下,将得到以下源代码。

@Identities({@Identity("父亲"), @Identity("教师")})
public class RepeatableTest {
    public RepeatableTest() {
    }
    
    // 省略main方法
}

可以看到重复使用@Identity注解其实是一个语法糖,这也是为什么上面getAnnotation方法传的参数是Identities.class的原因。继续使用Class类提供的方法做检测。

@Identity("父亲")
@Identity("教师")
public class RepeatableTest {

    public static void main(String[] args) {
        // 返回false
        System.out.println(RepeatableTest.class.isAnnotationPresent(Identity.class));
        // 返回null
        System.out.println(RepeatableTest.class.getAnnotation(Identity.class));
        // 由此可见RepeatableTest类中确实没有被Identity直接注解。
        
        // 另外一种简便的方法获取注解信息
        // 使用getAnnotationsByType方法, 此方法为JDK 1.8新增
        Identity[] identities = RepeatableTest.class
            .getAnnotationsByType(Identity.class);
        Arrays.stream(identities).forEach(identity ->
                System.out.println(identity.value()));

        Identities[] identitiesAnnotation = RepeatableTest.class.
            getAnnotationsByType(Identities.class);
        // 输出父亲
        System.out.println(identitiesAnnotation[0].value()[0].value());
    }
}
posted on   wastonl  阅读(1049)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
历史上的今天:
2016-11-05 mybatis3.4与spring3.2.5整合出现的问题
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示