From 81adb94e54c2c80a5e22d9cc9358dfe9b2231b7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=8B=E8=BE=9E=E6=9C=AA=E5=AF=92?= <545073804@qq.com> Date: Fri, 13 Mar 2026 15:39:47 +0800 Subject: [PATCH] =?UTF-8?q?add=20=E6=96=B0=E5=A2=9E=E6=B3=A8=E8=A7=A3?= =?UTF-8?q?=E7=B1=BB=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/utils/reflect/AnnotationUtils.java | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/utils/reflect/AnnotationUtils.java diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/utils/reflect/AnnotationUtils.java b/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/utils/reflect/AnnotationUtils.java new file mode 100644 index 000000000..e9b558d74 --- /dev/null +++ b/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/utils/reflect/AnnotationUtils.java @@ -0,0 +1,63 @@ +package org.dromara.common.core.utils.reflect; + +import cn.hutool.core.annotation.AnnotationUtil; +import cn.hutool.core.exceptions.UtilException; +import cn.hutool.core.lang.Dict; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +import java.lang.annotation.Annotation; +import java.lang.reflect.AnnotatedElement; +import java.util.Map; + +/** + * 注解工具类 + * + * @author 秋辞未寒 + */ +@Slf4j +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class AnnotationUtils extends AnnotationUtil { + + /** + * 获取指定注解 + * + * @param annotationEle {@link AnnotatedElement},可以是Class、Method、Field、Constructor、ReflectPermission + * @param annotationTypeName 注解类型名称 + * @return 注解对象 + */ + @SuppressWarnings("unchecked") + public static Annotation getAnnotation(AnnotatedElement annotationEle, String annotationTypeName) { + try { + return AnnotationUtil.getAnnotation(annotationEle, (Class) Class.forName(annotationTypeName)); + } catch (final ClassNotFoundException | ClassCastException e) { + // ignore + log.error("AnnotationUtils.getAnnotation(AnnotatedElement, String) error.", e); + return null; + } + } + + /** + * 获取指定注解中所有属性值
+ * 如果无指定的属性方法返回null + * + * @param annotationEle {@link AnnotatedElement},可以是Class、Method、Field、Constructor、ReflectPermission + * @param annotationTypeName 注解类型名称 + * @return 注解对象所有属性键值 + * @throws UtilException 调用注解中的方法时执行异常 + */ + @SuppressWarnings("unchecked") + public static Dict getAnnotationValueMap(AnnotatedElement annotationEle, String annotationTypeName) throws UtilException { + try { + Map annotationValueMap = AnnotationUtil.getAnnotationValueMap(annotationEle, (Class) Class.forName(annotationTypeName)); + return new Dict(annotationValueMap); + } catch (final ClassNotFoundException | ClassCastException e) { + // ignore + log.error("AnnotationUtils.getAnnotationValueMap(AnnotatedElement, String) error.", e); + return null; + } + } + + +}