JAVA 注解搜索工具类与注解原理讲解(获取方法和类上所有的某个注解,父类继承的注解也支持获取)

本文主要是介绍JAVA 注解搜索工具类与注解原理讲解(获取方法和类上所有的某个注解,父类继承的注解也支持获取),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • JAVA 注解搜索工具类与注解原理讲解(获取方法和类上所有的某个注解,父类继承的注解也支持获取)
    • 代码
    • 测试
      • 方法上加注解,类上不加
      • 类上加注解、方法上加注解
    • 注解原理
    • 性能测试

JAVA 注解搜索工具类与注解原理讲解(获取方法和类上所有的某个注解,父类继承的注解也支持获取)

基于Spring的AnnotatedElementUtils工具,支持从当前类、父类、接口搜索(支持限制最大深度),支持传入搜索配置、启用缓存。

在这里插入图片描述

代码

import cn.hutool.core.collection.CollectionUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.springframework.core.annotation.AnnotatedElementUtils;import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Executable;
import java.lang.reflect.Member;
import java.lang.reflect.Parameter;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;/*** @author chenfuxing* date: 2024/6/19* description: 注解工具类**/
public class AnnotationUtil extends AnnotatedElementUtils {/*** 默认注解搜索配置*/private static final AnnotationSearchConfig DEFAULT_SEARCH_CONFIG = new AnnotationSearchConfig();/*** 缓存*/private static final Map<String, Set<? extends Annotation>> CACHE = new ConcurrentHashMap<>();/*** 注解搜索配置*/@Data@Accessors(chain = true)@AllArgsConstructor@NoArgsConstructor@ToStringpublic static class AnnotationSearchConfig {/*** 从类上搜索注解*/private boolean searchFromClass = true;/*** 从父类上搜索注解*/private boolean searchSuperClass = true;/*** 从父类上搜索注解的最大深度*/private int searchSuperMaxDepth = 3;/*** 从类的接口上搜索注解*/private boolean searchFromInterfaces = true;/*** 允许使用缓存*/private boolean enableCache = true;}/*** 获取缓存key** @param element* @param annotationType* @param config* @return*/public static String getCacheKey(AnnotatedElement element, Class<?> annotationType, AnnotationSearchConfig config) {StringBuilder sb = new StringBuilder();sb.append(element != null ? element.toString() : "null");sb.append("-");sb.append(annotationType != null ? annotationType.getName() : "null");sb.append("-");sb.append(config != null ? config.toString() : "null");return sb.toString();}/*** 获取注解* 若有多个则只会返回第一个注解(方法上的优先于类上的)** @param element* @param annotationType* @param <A>* @return*/public static <A extends Annotation> A getAnnotation(AnnotatedElement element, Class<A> annotationType) {Set<A> allAnnotations = getAllAnnotations(element, annotationType);return CollectionUtil.isEmpty(allAnnotations) ? null : allAnnotations.iterator().next();}/*** 获取注解* 若有多个则只会返回第一个注解(方法上的优先于类上的)** @param element* @param annotationType* @param <A>* @return*/public static <A extends Annotation> A getAnnotation(AnnotatedElement element, Class<A> annotationType, AnnotationSearchConfig config) {Set<A> allAnnotations = getAllAnnotations(element, annotationType, config);return CollectionUtil.isEmpty(allAnnotations) ? null : allAnnotations.iterator().next();}/*** 获取方法和类上的注解** @param element* @return*/public static <A extends Annotation> Set<A> getAllAnnotations(AnnotatedElement element, Class<A> annotationType) {return getAllAnnotations(element, annotationType, DEFAULT_SEARCH_CONFIG);}/*** 获取方法和类上的注解** @param element* @return*/public static <A extends Annotation> Set<A> getAllAnnotations(AnnotatedElement element, Class<A> annotationType, AnnotationSearchConfig config) {if (config == null) {throw new NullPointerException("config can not be null");}if (config.isEnableCache()) {return (Set<A>) CACHE.computeIfAbsent(getCacheKey(element, annotationType, config), k -> getAllAnnotations(element, annotationType, config.getSearchSuperMaxDepth(), config));}return getAllAnnotations(element, annotationType, config.getSearchSuperMaxDepth(), config);}/*** 获取方法和类上的注解** @param element* @return*/private static <A extends Annotation> Set<A> getAllAnnotations(AnnotatedElement element, Class<A> annotationType, int depth, AnnotationSearchConfig config) {if (element == null || annotationType == null || depth <= 0 || config == null) {return Collections.emptySet();}boolean isClass = element instanceof Class;Class<?> declaringClass = isClass ? (Class<?>) element : element.getClass();if (element instanceof Executable) {declaringClass = ((Executable) element).getDeclaringClass();}if (element instanceof Member) {declaringClass = ((Member) element).getDeclaringClass();}if (element instanceof Parameter) {declaringClass = ((Parameter) element).getDeclaringExecutable().getDeclaringClass();}Class<?>[] interfaces = declaringClass.getInterfaces();Class<?> superclass = declaringClass.getSuperclass();// 自身的注解Set<A> methodAnnotationSet = isClass ? Collections.emptySet() : getAllMergedAnnotations(element, annotationType);// 类上的注解Set<A> classAnnotationSet = config.isSearchFromClass() ? getAllMergedAnnotations(declaringClass, annotationType) : Collections.emptySet();// 接口上的注解Set<A> interfaceAnnotationSet = config.isSearchFromInterfaces() ? (interfaces.length > 0 ? Arrays.stream(interfaces).map(interfaceCls -> getAllMergedAnnotations(interfaceCls, annotationType)).flatMap(Set::stream).collect(Collectors.toSet()) : Collections.emptySet()) : Collections.emptySet();// 父类上的注解Set<A> superClassAnnotationSet = config.isSearchSuperClass() ? (!Object.class.equals(superclass) ? getAllAnnotations(superclass, annotationType, depth - 1, config) : Collections.emptySet()) : Collections.emptySet();return Stream.of(methodAnnotationSet, classAnnotationSet, interfaceAnnotationSet, superClassAnnotationSet).flatMap(Set::stream).collect(Collectors.toSet());}
}

测试

定义了一个类,检查登录类型,用于限制某些端的用户只能访问某些端自己的接口
在这里插入图片描述
额外定义了多个快捷注解,快捷注解上标注了@CheckLoginType,预设了值。
在这里插入图片描述
在这里插入图片描述

方法上加注解,类上不加

在这里插入图片描述
调试结果
在这里插入图片描述

类上加注解、方法上加注解

在这里插入图片描述
运行调试
在这里插入图片描述
都能正常获取到方法、类上该注解(包括继承来的)

注解原理

注解基类Annotation,注释里写了从1.5版本引入的,所有的注解都会继承这个类,且不需要在定义的时候标明继承自这个类,只提供了annotationType来返回这个注解的Class,以及每个对象都有的equals、hashcode、toString()方法,那我们平时经常用的getAnnotation、isAnnotationPresent这些方法是哪里定义的呢?
在这里插入图片描述
我们通过方法找到接口为AnnotationElement,可以看到我们平时反射获取注解时的方法是定义在这个接口里的。
在这里插入图片描述

在这里插入图片描述
我们再看这个接口有哪些实现类,可以看到我们平时常用的类、方法、参数、构造方法等都实现了这个接口,因此这些地方都是可以加注解并通过反射拿到加了的注解的。

在这里插入图片描述
那我们看下在方法上获取注解时底层是怎么实现的,打开Method对象,可以看到它是调了父类的实现,继续往父类看
在这里插入图片描述
父类是Executable类
在这里插入图片描述
这个类是一个抽象类,上门写了是提供一些Method类和Constructor类公共使用的方法,它是一个共享的父类。
在这里插入图片描述
declaredAnnotations()的实现是,当前类有声明注解则直接返回当前类的declaredAnnotations这个Map,而这个map是记录的当前这个Method对象定义的一些注解,若有则直接返回这个,没有的话就会看有没有root,有root则返回root声明的所有注解的map。
在这里插入图片描述
而这个root对象,只有在执行Method.copy的时候才会有,那你可以当做root一直是个null的
在这里插入图片描述
因此平时你利用反射获取method的注解时其实就只获取到了这个方法上直接声明的注解,类上的,当前方法注解里继承的注解都不会返回的。这就是JDK方式的实现,因此spring在实现的时候提供了工具类去解决这个问题

性能测试

执行10000次,总耗时:240ms,平均耗时:0.024ms

在这里插入图片描述

在这里插入图片描述

这篇关于JAVA 注解搜索工具类与注解原理讲解(获取方法和类上所有的某个注解,父类继承的注解也支持获取)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1077453

相关文章

SpringBoot 获取请求参数的常用注解及用法

《SpringBoot获取请求参数的常用注解及用法》SpringBoot通过@RequestParam、@PathVariable等注解支持从HTTP请求中获取参数,涵盖查询、路径、请求体、头、C... 目录SpringBoot 提供了多种注解来方便地从 HTTP 请求中获取参数以下是主要的注解及其用法:1

HTTP 与 SpringBoot 参数提交与接收协议方式

《HTTP与SpringBoot参数提交与接收协议方式》HTTP参数提交方式包括URL查询、表单、JSON/XML、路径变量、头部、Cookie、GraphQL、WebSocket和SSE,依据... 目录HTTP 协议支持多种参数提交方式,主要取决于请求方法(Method)和内容类型(Content-Ty

深度解析Java @Serial 注解及常见错误案例

《深度解析Java@Serial注解及常见错误案例》Java14引入@Serial注解,用于编译时校验序列化成员,替代传统方式解决运行时错误,适用于Serializable类的方法/字段,需注意签... 目录Java @Serial 注解深度解析1. 注解本质2. 核心作用(1) 主要用途(2) 适用位置3

深入浅出Spring中的@Autowired自动注入的工作原理及实践应用

《深入浅出Spring中的@Autowired自动注入的工作原理及实践应用》在Spring框架的学习旅程中,@Autowired无疑是一个高频出现却又让初学者头疼的注解,它看似简单,却蕴含着Sprin... 目录深入浅出Spring中的@Autowired:自动注入的奥秘什么是依赖注入?@Autowired

Spring 依赖注入与循环依赖总结

《Spring依赖注入与循环依赖总结》这篇文章给大家介绍Spring依赖注入与循环依赖总结篇,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. Spring 三级缓存解决循环依赖1. 创建UserService原始对象2. 将原始对象包装成工

Java中如何正确的停掉线程

《Java中如何正确的停掉线程》Java通过interrupt()通知线程停止而非强制,确保线程自主处理中断,避免数据损坏,线程池的shutdown()等待任务完成,shutdownNow()强制中断... 目录为什么不强制停止为什么 Java 不提供强制停止线程的能力呢?如何用interrupt停止线程s

SpringBoot请求参数传递与接收示例详解

《SpringBoot请求参数传递与接收示例详解》本文给大家介绍SpringBoot请求参数传递与接收示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋... 目录I. 基础参数传递i.查询参数(Query Parameters)ii.路径参数(Path Va

SpringBoot路径映射配置的实现步骤

《SpringBoot路径映射配置的实现步骤》本文介绍了如何在SpringBoot项目中配置路径映射,使得除static目录外的资源可被访问,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一... 目录SpringBoot路径映射补:springboot 配置虚拟路径映射 @RequestMapp

Java MCP 的鉴权深度解析

《JavaMCP的鉴权深度解析》文章介绍JavaMCP鉴权的实现方式,指出客户端可通过queryString、header或env传递鉴权信息,服务器端支持工具单独鉴权、过滤器集中鉴权及启动时鉴权... 目录一、MCP Client 侧(负责传递,比较简单)(1)常见的 mcpServers json 配置

GSON框架下将百度天气JSON数据转JavaBean

《GSON框架下将百度天气JSON数据转JavaBean》这篇文章主要为大家详细介绍了如何在GSON框架下实现将百度天气JSON数据转JavaBean,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录前言一、百度天气jsON1、请求参数2、返回参数3、属性映射二、GSON属性映射实战1、类对象映