将AnnotationUtil获取合成注解的方法统一改为以SynthesizedAnnotation结尾

This commit is contained in:
huangchengxing 2022-07-09 15:23:00 +08:00
parent 18c7a78062
commit b29b0c3932
3 changed files with 30 additions and 30 deletions

View File

@ -353,34 +353,11 @@ public class AnnotationUtil {
* @return 合成注解 * @return 合成注解
* @see SynthesizedAnnotationAggregator * @see SynthesizedAnnotationAggregator
*/ */
public static <T extends Annotation> T getSynthesisAnnotation(Annotation annotation, Class<T> annotationType) { public static <T extends Annotation> T getSynthesizedAnnotation(Annotation annotation, Class<T> annotationType) {
// TODO 缓存合成注解信息避免重复解析 // TODO 缓存合成注解信息避免重复解析
return SynthesizedAnnotationAggregator.of(annotation).synthesize(annotationType); return SynthesizedAnnotationAggregator.of(annotation).synthesize(annotationType);
} }
/**
* 获取元素上所有指定注解
* <ul>
* <li>若元素是类则递归解析全部父类和全部父接口上的注解;</li>
* <li>若元素是方法属性或注解则只解析其直接声明的注解;</li>
* </ul>
*
* @param annotatedEle {@link AnnotatedElement}可以是ClassMethodFieldConstructorReflectPermission
* @param annotationType 注解类
* @param <T> 注解类型
* @return 合成注解
* @see SynthesizedAnnotationAggregator
*/
public static <T extends Annotation> List<T> getAllSynthesisAnnotations(AnnotatedElement annotatedEle, Class<T> annotationType) {
AnnotationScanner[] scanners = new AnnotationScanner[]{
new MetaAnnotationScanner(), new TypeAnnotationScanner(), new MethodAnnotationScanner(), new FieldAnnotationScanner()
};
return AnnotationScanner.scanByAnySupported(annotatedEle, scanners).stream()
.map(annotation -> getSynthesisAnnotation(annotation, annotationType))
.filter(Objects::nonNull)
.collect(Collectors.toList());
}
/** /**
* 获取元素上距离指定元素最接近的合成注解 * 获取元素上距离指定元素最接近的合成注解
* <ul> * <ul>
@ -394,7 +371,7 @@ public class AnnotationUtil {
* @return 合成注解 * @return 合成注解
* @see SynthesizedAnnotationAggregator * @see SynthesizedAnnotationAggregator
*/ */
public static <T extends Annotation> T getSyntheticAnnotation(AnnotatedElement annotatedEle, Class<T> annotationType) { public static <T extends Annotation> T getSynthesizedAnnotation(AnnotatedElement annotatedEle, Class<T> annotationType) {
T target = annotatedEle.getAnnotation(annotationType); T target = annotatedEle.getAnnotation(annotationType);
if (ObjectUtil.isNotNull(target)) { if (ObjectUtil.isNotNull(target)) {
return target; return target;
@ -403,12 +380,35 @@ public class AnnotationUtil {
new MetaAnnotationScanner(), new TypeAnnotationScanner(), new MethodAnnotationScanner(), new FieldAnnotationScanner() new MetaAnnotationScanner(), new TypeAnnotationScanner(), new MethodAnnotationScanner(), new FieldAnnotationScanner()
}; };
return AnnotationScanner.scanByAnySupported(annotatedEle, scanners).stream() return AnnotationScanner.scanByAnySupported(annotatedEle, scanners).stream()
.map(annotation -> getSynthesisAnnotation(annotation, annotationType)) .map(annotation -> getSynthesizedAnnotation(annotation, annotationType))
.filter(Objects::nonNull) .filter(Objects::nonNull)
.findFirst() .findFirst()
.orElse(null); .orElse(null);
} }
/**
* 获取元素上所有指定注解
* <ul>
* <li>若元素是类则递归解析全部父类和全部父接口上的注解;</li>
* <li>若元素是方法属性或注解则只解析其直接声明的注解;</li>
* </ul>
*
* @param annotatedEle {@link AnnotatedElement}可以是ClassMethodFieldConstructorReflectPermission
* @param annotationType 注解类
* @param <T> 注解类型
* @return 合成注解
* @see SynthesizedAnnotationAggregator
*/
public static <T extends Annotation> List<T> getAllSynthesizedAnnotations(AnnotatedElement annotatedEle, Class<T> annotationType) {
AnnotationScanner[] scanners = new AnnotationScanner[]{
new MetaAnnotationScanner(), new TypeAnnotationScanner(), new MethodAnnotationScanner(), new FieldAnnotationScanner()
};
return AnnotationScanner.scanByAnySupported(annotatedEle, scanners).stream()
.map(annotation -> getSynthesizedAnnotation(annotation, annotationType))
.filter(Objects::nonNull)
.collect(Collectors.toList());
}
/** /**
* 扫描注解类以及注解类的{@link Class}层级结构中的注解将返回除了{@link #META_ANNOTATIONS}中指定的JDK默认注解外 * 扫描注解类以及注解类的{@link Class}层级结构中的注解将返回除了{@link #META_ANNOTATIONS}中指定的JDK默认注解外
* 按元注解对象与{@code annotationType}的距离和{@link Class#getAnnotations()}顺序排序的注解对象集合 * 按元注解对象与{@code annotationType}的距离和{@link Class#getAnnotations()}顺序排序的注解对象集合

View File

@ -80,7 +80,7 @@ class SyntheticAnnotationProxy implements InvocationHandler {
void loadMethods() { void loadMethods() {
methods.put("toString", (method, args) -> proxyToString()); methods.put("toString", (method, args) -> proxyToString());
methods.put("hashCode", (method, args) -> proxyHashCode()); methods.put("hashCode", (method, args) -> proxyHashCode());
methods.put("getSyntheticAnnotation", (method, args) -> proxyGetSyntheticAnnotation()); methods.put("getSynthesizedAnnotation", (method, args) -> proxyGetSyntheticAnnotation());
methods.put("getSynthesizedAnnotation", (method, args) -> proxyGetSynthesizedAnnotation()); methods.put("getSynthesizedAnnotation", (method, args) -> proxyGetSynthesizedAnnotation());
methods.put("getRoot", (method, args) -> annotation.getRoot()); methods.put("getRoot", (method, args) -> annotation.getRoot());
methods.put("getVerticalDistance", (method, args) -> annotation.getVerticalDistance()); methods.put("getVerticalDistance", (method, args) -> annotation.getVerticalDistance());

View File

@ -24,7 +24,7 @@ class SyntheticAnnotationUtil {
*/ */
static Link getLink(AnnotationAttribute attribute, RelationType... relationTypes) { static Link getLink(AnnotationAttribute attribute, RelationType... relationTypes) {
return Opt.ofNullable(attribute) return Opt.ofNullable(attribute)
.map(t -> AnnotationUtil.getSyntheticAnnotation(attribute.getAttribute(), Link.class)) .map(t -> AnnotationUtil.getSynthesizedAnnotation(attribute.getAttribute(), Link.class))
.filter(a -> ArrayUtil.contains(relationTypes, a.type())) .filter(a -> ArrayUtil.contains(relationTypes, a.type()))
.get(); .get();
} }