07-Java8新特性 其他新特性
重复注解与类型注解
Java8对注解处理提供了两点该进,可重复的注解及可用于类型的注解
重复注解定义使用
新建注解
package com.dance.java8.day01.annotation; import java.lang.annotation.Repeatable; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import static java.lang.annotation.ElementType.*; import static java.lang.annotation.ElementType.LOCAL_VARIABLE; // 重复注解 必须使用@Repeatable修饰 并指定注解容器 @Repeatable(MyAnnotations.class) @Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE}) @Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotation { String value() default "flower"; }
新建注解容器
package com.dance.java8.day01.annotation; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import static java.lang.annotation.ElementType.*; @Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE}) @Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotations { MyAnnotation[] value(); }
新建测试类
package com.dance.java8.day01.annotation; import java.lang.reflect.Method; import java.util.Arrays; /** * 重复注解与类型注解 */ public class TestAnnotation { public static void main(String[] args) throws NoSuchMethodException { Class<TestAnnotation> testAnnotationClass = TestAnnotation.class; Method show = testAnnotationClass.getMethod("show"); Arrays.stream(show.getAnnotationsByType(MyAnnotation.class)).forEach(System.out::println); } @MyAnnotation("flower") @MyAnnotation("dance") public void show(){ } }
执行结果
@com.dance.java8.day01.annotation.MyAnnotation(value=flower) @com.dance.java8.day01.annotation.MyAnnotation(value=dance)
类型注解定义使用
修改注解,增加类型注解
package com.dance.java8.day01.annotation; import java.lang.annotation.*; import static java.lang.annotation.ElementType.*; import static java.lang.annotation.ElementType.LOCAL_VARIABLE; // 重复注解 必须使用@Repeatable修饰 并指定注解容器 @Repeatable(MyAnnotations.class) @Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, TYPE_PARAMETER}) @Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotation { String value() default "flower"; }
修改测试类
package com.dance.java8.day01.annotation; import java.lang.annotation.Annotation; import java.lang.reflect.Method; import java.util.Arrays; /** * 重复注解与类型注解 */ public class TestAnnotation { public static void main(String[] args) throws NoSuchMethodException { Class<TestAnnotation> testAnnotationClass = TestAnnotation.class; Method show = testAnnotationClass.getMethod("show",String.class); Arrays.stream(show.getAnnotationsByType(MyAnnotation.class)).forEach(System.out::println); Annotation[][] parameterAnnotations = show.getParameterAnnotations(); Arrays.stream(parameterAnnotations).forEach(x-> Arrays.stream(x).forEach(System.out::println)); } @MyAnnotation("flower") @MyAnnotation("dance") public void show(@MyAnnotation("string") String s){ } }
执行结果
@com.dance.java8.day01.annotation.MyAnnotation(value=flower) @com.dance.java8.day01.annotation.MyAnnotation(value=dance) @com.dance.java8.day01.annotation.MyAnnotation(value=string)
若有收获,就点个赞吧
作者:彼岸舞
时间:2021\12\13
内容关于:Java
本文属于作者原创,未经允许,禁止转发
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」