From ff30bdf1ea8986b7a38b96fa65054376ee28fa50 Mon Sep 17 00:00:00 2001 From: huangchengxing <841396397@qq.com> Date: Tue, 14 Jun 2022 15:56:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B3=A8=E8=A7=A3=E5=B7=A5=E5=85=B7=E7=B1=BB?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=8E=B7=E5=8F=96=E5=90=88=E5=B9=B6=E6=B3=A8?= =?UTF-8?q?=E8=A7=A3=E7=9A=84=E6=96=B9=E6=B3=95;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/annotation/AnnotationUtil.java | 42 +++++++++++++++++-- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/annotation/AnnotationUtil.java b/hutool-core/src/main/java/cn/hutool/core/annotation/AnnotationUtil.java index 1a5f24450..5c3f217aa 100755 --- a/hutool-core/src/main/java/cn/hutool/core/annotation/AnnotationUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/annotation/AnnotationUtil.java @@ -1,5 +1,6 @@ package cn.hutool.core.annotation; +import cn.hutool.core.annotation.scanner.*; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.exceptions.UtilException; import cn.hutool.core.lang.Assert; @@ -19,9 +20,7 @@ import java.lang.annotation.Target; import java.lang.reflect.AnnotatedElement; import java.lang.reflect.Method; import java.lang.reflect.Proxy; -import java.util.HashMap; -import java.util.Map; -import java.util.Set; +import java.util.*; import java.util.function.Function; import java.util.function.Predicate; import java.util.stream.Collectors; @@ -356,6 +355,43 @@ public class AnnotationUtil { return (T) Proxy.newProxyInstance(annotationType.getClassLoader(), new Class[]{annotationType}, new AnnotationProxy<>(annotation)); } + /** + * 将指定注解实例与其元注解转为合成注解 + * + * @param annotation 注解 + * @param annotationType 注解类型 + * @param 注解类型 + * @return 合成注解 + * @see SyntheticAnnotation + */ + public static T getSynthesisAnnotation(Annotation annotation, Class annotationType) { + return SyntheticAnnotation.of(annotation).getAnnotation(annotationType); + } + + /** + * 获取元素上所有指定注解 + *
    + *
  • 若元素是类,则递归解析全部父类和全部父接口上的注解;
  • + *
  • 若元素是方法、属性或注解,则只解析其直接声明的注解;
  • + *
+ * + * @param annotatedElement 可注解元素 + * @param annotationType 注解类型 + * @param 注解类型 + * @return 注解 + * @see SyntheticAnnotation + */ + public static List getAllSynthesisAnnotations(AnnotatedElement annotatedElement, Class annotationType) { + AnnotationScanner[] scanners = new AnnotationScanner[] { + new MateAnnotationScanner(), new TypeAnnotationScanner(), new MethodAnnotationScanner(), new FieldAnnotationScanner() + }; + return AnnotationScanner.scanByAnySupported(annotatedElement, scanners).stream() + .map(SyntheticAnnotation::of) + .map(annotation -> annotation.getAnnotation(annotationType)) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + } + /** * 方法是否为注解属性方法。
* 方法无参数,且有返回值的方法认为是注解属性的方法。