diff --git a/hutool-core/src/main/java/cn/hutool/core/annotation/CacheableSynthesizedAnnotationAttributeProcessor.java b/hutool-core/src/main/java/cn/hutool/core/annotation/CacheableSynthesizedAnnotationAttributeProcessor.java index 3329bc2f1..8461ac2ac 100644 --- a/hutool-core/src/main/java/cn/hutool/core/annotation/CacheableSynthesizedAnnotationAttributeProcessor.java +++ b/hutool-core/src/main/java/cn/hutool/core/annotation/CacheableSynthesizedAnnotationAttributeProcessor.java @@ -12,27 +12,25 @@ import java.util.Comparator; * 构建时需要传入比较器,获取属性值时将根据比较器对合成注解进行排序, * 然后选择具有所需属性的,排序最靠前的注解用于获取属性值 * - * @param 合成注解类型 * @author huangchengxing */ -public class CacheableSynthesizedAnnotationAttributeProcessor> implements - SynthesizedAnnotationAttributeProcessor { +public class CacheableSynthesizedAnnotationAttributeProcessor implements SynthesizedAnnotationAttributeProcessor { private final Table, Object> valueCaches = new RowKeyTable<>(); - private final Comparator annotationComparator; + private final Comparator annotationComparator; /** * 创建一个带缓存的注解值选择器 * * @param annotationComparator 注解比较器,排序更靠前的注解将被优先用于获取值 */ - public CacheableSynthesizedAnnotationAttributeProcessor(Comparator annotationComparator) { + public CacheableSynthesizedAnnotationAttributeProcessor(Comparator annotationComparator) { this.annotationComparator = annotationComparator; } @SuppressWarnings("unchecked") @Override - public T getAttributeValue(String attributeName, Class attributeType, Collection synthesizedAnnotations) { + public T getAttributeValue(String attributeName, Class attributeType, Collection synthesizedAnnotations) { Object value = valueCaches.get(attributeName, attributeType); // 此处理论上不可能出现缓存值为nul的情况 if (ObjectUtil.isNotNull(value)) { diff --git a/hutool-core/src/main/java/cn/hutool/core/annotation/SynthesizedAnnotation.java b/hutool-core/src/main/java/cn/hutool/core/annotation/SynthesizedAnnotation.java index 5c093ee6c..8d81701b0 100644 --- a/hutool-core/src/main/java/cn/hutool/core/annotation/SynthesizedAnnotation.java +++ b/hutool-core/src/main/java/cn/hutool/core/annotation/SynthesizedAnnotation.java @@ -7,14 +7,14 @@ import java.lang.annotation.Annotation; * * @author huangchengxing */ -public interface SynthesizedAnnotation extends Annotation { +public interface SynthesizedAnnotation extends Annotation { /** * 获取该合成注解对应的根节点 * * @return 数据源 */ - T getRoot(); + Object getRoot(); /** * 该合成注解是为根对象 diff --git a/hutool-core/src/main/java/cn/hutool/core/annotation/SynthesizedAnnotationAttributeProcessor.java b/hutool-core/src/main/java/cn/hutool/core/annotation/SynthesizedAnnotationAttributeProcessor.java index 022afb8ce..83f7c274b 100644 --- a/hutool-core/src/main/java/cn/hutool/core/annotation/SynthesizedAnnotationAttributeProcessor.java +++ b/hutool-core/src/main/java/cn/hutool/core/annotation/SynthesizedAnnotationAttributeProcessor.java @@ -8,7 +8,7 @@ import java.util.Collection; * @author huangchengxing */ @FunctionalInterface -public interface SynthesizedAnnotationAttributeProcessor> { +public interface SynthesizedAnnotationAttributeProcessor { /** * 从一批被合成注解中,获取指定名称与类型的属性值 @@ -16,9 +16,9 @@ public interface SynthesizedAnnotationAttributeProcessor 属性类型 + * @param 属性类型 * @return 属性值 */ - T getAttributeValue(String attributeName, Class attributeType, Collection synthesizedAnnotations); + R getAttributeValue(String attributeName, Class attributeType, Collection synthesizedAnnotations); } diff --git a/hutool-core/src/main/java/cn/hutool/core/annotation/SynthesizedAnnotationSelector.java b/hutool-core/src/main/java/cn/hutool/core/annotation/SynthesizedAnnotationSelector.java index 65b76cf61..b6383fe30 100644 --- a/hutool-core/src/main/java/cn/hutool/core/annotation/SynthesizedAnnotationSelector.java +++ b/hutool-core/src/main/java/cn/hutool/core/annotation/SynthesizedAnnotationSelector.java @@ -36,14 +36,14 @@ public interface SynthesizedAnnotationSelector { * @param newAnnotation 新获取的注解,该参数不允许为空 * @return 被合成的注解 */ - > A choose(A oldAnnotation, A newAnnotation); + T choose(T oldAnnotation, T newAnnotation); /** * 返回距离根对象更近的注解,当距离一样时优先返回旧注解 */ class NearestAndOldestPrioritySelector implements SynthesizedAnnotationSelector { @Override - public > A choose(A oldAnnotation, A newAnnotation) { + public T choose(T oldAnnotation, T newAnnotation) { return newAnnotation.getVerticalDistance() < oldAnnotation.getVerticalDistance() ? newAnnotation : oldAnnotation; } } @@ -53,7 +53,7 @@ public interface SynthesizedAnnotationSelector { */ class NearestAndNewestPrioritySelector implements SynthesizedAnnotationSelector { @Override - public > A choose(A oldAnnotation, A newAnnotation) { + public T choose(T oldAnnotation, T newAnnotation) { return newAnnotation.getVerticalDistance() <= oldAnnotation.getVerticalDistance() ? newAnnotation : oldAnnotation; } } @@ -63,7 +63,7 @@ public interface SynthesizedAnnotationSelector { */ class FarthestAndOldestPrioritySelector implements SynthesizedAnnotationSelector { @Override - public > A choose(A oldAnnotation, A newAnnotation) { + public T choose(T oldAnnotation, T newAnnotation) { return newAnnotation.getVerticalDistance() > oldAnnotation.getVerticalDistance() ? newAnnotation : oldAnnotation; } } @@ -73,7 +73,7 @@ public interface SynthesizedAnnotationSelector { */ class FarthestAndNewestPrioritySelector implements SynthesizedAnnotationSelector { @Override - public > A choose(A oldAnnotation, A newAnnotation) { + public T choose(T oldAnnotation, T newAnnotation) { return newAnnotation.getVerticalDistance() >= oldAnnotation.getVerticalDistance() ? newAnnotation : oldAnnotation; } } diff --git a/hutool-core/src/main/java/cn/hutool/core/annotation/SyntheticAnnotation.java b/hutool-core/src/main/java/cn/hutool/core/annotation/SyntheticAnnotation.java index 084c12566..420e0f5c9 100644 --- a/hutool-core/src/main/java/cn/hutool/core/annotation/SyntheticAnnotation.java +++ b/hutool-core/src/main/java/cn/hutool/core/annotation/SyntheticAnnotation.java @@ -6,10 +6,9 @@ import java.lang.reflect.AnnotatedElement; /** * 表示基于特定规则聚合的一组注解 * - * @param 合成注解类型 * @author huangchengxing */ -public interface SyntheticAnnotation> extends Annotation, AnnotatedElement { +public interface SyntheticAnnotation extends Annotation, AnnotatedElement { /** * 获取合成注解选择器 @@ -23,16 +22,15 @@ public interface SyntheticAnnotation> extends * * @return 合成注解属性处理器 */ - SynthesizedAnnotationAttributeProcessor getAttributeProcessor(); + SynthesizedAnnotationAttributeProcessor getAttributeProcessor(); /** * 获取已合成的注解 * * @param annotationType 注解类型 - * @param 注解类型 * @return 已合成的注解 */ - SynthesizedAnnotation getSynthesizedAnnotation(Class annotationType); + SynthesizedAnnotation getSynthesizedAnnotation(Class annotationType); /** * 获取当前的注解类型 @@ -96,8 +94,8 @@ public interface SyntheticAnnotation> extends * @param 注解类型 * @return 合成注解 */ - static SyntheticAnnotation of(T rootAnnotation) { - return new SyntheticMetaAnnotation<>(rootAnnotation); + static SyntheticAnnotation of(T rootAnnotation) { + return new SyntheticMetaAnnotation(rootAnnotation); } } diff --git a/hutool-core/src/main/java/cn/hutool/core/annotation/SyntheticAnnotationProxy.java b/hutool-core/src/main/java/cn/hutool/core/annotation/SyntheticAnnotationProxy.java index 101835f47..d27a650f4 100644 --- a/hutool-core/src/main/java/cn/hutool/core/annotation/SyntheticAnnotationProxy.java +++ b/hutool-core/src/main/java/cn/hutool/core/annotation/SyntheticAnnotationProxy.java @@ -20,16 +20,15 @@ import java.util.stream.Stream; /** * 合成注解代理类 * - * @param 代理的注解类型 * @author huangchengxing */ -class SyntheticAnnotationProxy implements InvocationHandler { +class SyntheticAnnotationProxy implements InvocationHandler { - private final SyntheticAnnotation syntheticAnnotation; - private final SynthesizedAnnotation annotation; + private final SyntheticAnnotation syntheticAnnotation; + private final SynthesizedAnnotation annotation; private final Map> methods; - SyntheticAnnotationProxy(SyntheticAnnotation syntheticAnnotation, SynthesizedAnnotation annotation) { + SyntheticAnnotationProxy(SyntheticAnnotation syntheticAnnotation, SynthesizedAnnotation annotation) { this.syntheticAnnotation = syntheticAnnotation; this.annotation = annotation; this.methods = new HashMap<>(9); @@ -45,18 +44,17 @@ class SyntheticAnnotationProxy implements InvocationHandle * * @param annotationType 注解类型 * @param syntheticAnnotation 合成注解 - * @param 代理的注解类型 * @return 代理注解 */ @SuppressWarnings("unchecked") - static A create( - Class annotationType, SyntheticAnnotation syntheticAnnotation) { - final SynthesizedAnnotation annotation = syntheticAnnotation.getSynthesizedAnnotation(annotationType); - final SyntheticAnnotationProxy proxyHandler = new SyntheticAnnotationProxy<>(syntheticAnnotation, annotation); + static T create( + Class annotationType, SyntheticAnnotation syntheticAnnotation) { + final SynthesizedAnnotation annotation = syntheticAnnotation.getSynthesizedAnnotation(annotationType); + final SyntheticAnnotationProxy proxyHandler = new SyntheticAnnotationProxy(syntheticAnnotation, annotation); if (ObjectUtil.isNull(annotation)) { return null; } - return (A) Proxy.newProxyInstance( + return (T) Proxy.newProxyInstance( annotationType.getClassLoader(), new Class[]{annotationType, SyntheticProxyAnnotation.class}, proxyHandler @@ -122,21 +120,21 @@ class SyntheticAnnotationProxy implements InvocationHandle * * @author huangchengxing */ - interface SyntheticProxyAnnotation> extends SynthesizedAnnotation { + interface SyntheticProxyAnnotation extends SynthesizedAnnotation { /** * 获取该注解所属的合成注解 * * @return 合成注解 */ - SyntheticAnnotation getSyntheticAnnotation(); + SyntheticAnnotation getSyntheticAnnotation(); /** * 获取该代理注解对应的已合成注解 * * @return 理注解对应的已合成注解 */ - SynthesizedAnnotation getSynthesizedAnnotation(); + SynthesizedAnnotation getSynthesizedAnnotation(); } diff --git a/hutool-core/src/main/java/cn/hutool/core/annotation/SyntheticMetaAnnotation.java b/hutool-core/src/main/java/cn/hutool/core/annotation/SyntheticMetaAnnotation.java index 708fc8fe1..d4ef3ce95 100644 --- a/hutool-core/src/main/java/cn/hutool/core/annotation/SyntheticMetaAnnotation.java +++ b/hutool-core/src/main/java/cn/hutool/core/annotation/SyntheticMetaAnnotation.java @@ -43,17 +43,17 @@ import java.util.Map; * @see AnnotationUtil * @see SynthesizedAnnotationSelector */ -public class SyntheticMetaAnnotation implements SyntheticAnnotation { +public class SyntheticMetaAnnotation implements SyntheticAnnotation { /** * 根注解,即当前查找的注解 */ - private final A source; + private final Annotation source; /** * 包含根注解以及其元注解在内的全部注解实例 */ - private final Map, MetaAnnotation> metaAnnotationMap; + private final Map, SynthesizedAnnotation> metaAnnotationMap; /** * 合成注解选择器 @@ -63,7 +63,7 @@ public class SyntheticMetaAnnotation implements SyntheticA /** * 合成注解属性处理器 */ - private final SynthesizedAnnotationAttributeProcessor attributeProcessor; + private final SynthesizedAnnotationAttributeProcessor attributeProcessor; /** * 基于指定根注解,为其层级结构中的全部注解构造一个合成注解。 @@ -72,12 +72,12 @@ public class SyntheticMetaAnnotation implements SyntheticA * * @param source 源注解 */ - public SyntheticMetaAnnotation(A source) { + public SyntheticMetaAnnotation(Annotation source) { this( source, SynthesizedAnnotationSelector.NEAREST_AND_OLDEST_PRIORITY, - new CacheableSynthesizedAnnotationAttributeProcessor<>( - Comparator.comparing(MetaAnnotation::getVerticalDistance) - .thenComparing(MetaAnnotation::getHorizontalDistance) + new CacheableSynthesizedAnnotationAttributeProcessor( + Comparator.comparing(SynthesizedAnnotation::getVerticalDistance) + .thenComparing(SynthesizedAnnotation::getHorizontalDistance) ) ); } @@ -89,9 +89,9 @@ public class SyntheticMetaAnnotation implements SyntheticA * @param annotationSelector 合成注解选择器 */ public SyntheticMetaAnnotation( - A annotation, + Annotation annotation, SynthesizedAnnotationSelector annotationSelector, - SynthesizedAnnotationAttributeProcessor attributeProcessor) { + SynthesizedAnnotationAttributeProcessor attributeProcessor) { Assert.notNull(annotation, "annotation must not null"); Assert.notNull(annotationSelector, "annotationSelector must not null"); Assert.notNull(attributeProcessor, "attributeProcessor must not null"); @@ -108,7 +108,7 @@ public class SyntheticMetaAnnotation implements SyntheticA * * @return 根注解 */ - public A getSource() { + public Annotation getSource() { return source; } @@ -117,7 +117,7 @@ public class SyntheticMetaAnnotation implements SyntheticA * * @return 已解析的元注解信息 */ - Map, MetaAnnotation> getMetaAnnotationMap() { + Map, SynthesizedAnnotation> getMetaAnnotationMap() { return metaAnnotationMap; } @@ -137,8 +137,8 @@ public class SyntheticMetaAnnotation implements SyntheticA * @return 合成注解属性处理器 */ @Override - public SynthesizedAnnotationAttributeProcessor getAttributeProcessor() { - return null; + public SynthesizedAnnotationAttributeProcessor getAttributeProcessor() { + return this.attributeProcessor; } /** @@ -149,8 +149,8 @@ public class SyntheticMetaAnnotation implements SyntheticA */ @SuppressWarnings("unchecked") @Override - public SynthesizedAnnotation getSynthesizedAnnotation(Class annotationType) { - return (SynthesizedAnnotation)metaAnnotationMap.get(annotationType); + public SynthesizedAnnotation getSynthesizedAnnotation(Class annotationType) { + return metaAnnotationMap.get(annotationType); } /** @@ -187,7 +187,7 @@ public class SyntheticMetaAnnotation implements SyntheticA public T getAnnotation(Class annotationType) { return Opt.ofNullable(annotationType) .map(metaAnnotationMap::get) - .map(MetaAnnotation::getAnnotation) + .map(SynthesizedAnnotation::getAnnotation) .map(annotationType::cast) .orElse(null); } @@ -244,8 +244,8 @@ public class SyntheticMetaAnnotation implements SyntheticA metaAnnotationMap.put(source.annotationType(), new MetaAnnotation(source, source, 0, 0)); new MetaAnnotationScanner().scan( (index, annotation) -> { - MetaAnnotation oldAnnotation = metaAnnotationMap.get(annotation.annotationType()); - MetaAnnotation newAnnotation = new MetaAnnotation(source, annotation, index, metaAnnotationMap.size()); + SynthesizedAnnotation oldAnnotation = metaAnnotationMap.get(annotation.annotationType()); + SynthesizedAnnotation newAnnotation = new MetaAnnotation(source, annotation, index, metaAnnotationMap.size()); if (ObjectUtil.isNull(oldAnnotation)) { metaAnnotationMap.put(annotation.annotationType(), newAnnotation); } else { @@ -261,7 +261,7 @@ public class SyntheticMetaAnnotation implements SyntheticA * * @author huangchengxing */ - public static class MetaAnnotation implements Annotation, SynthesizedAnnotation { + public static class MetaAnnotation implements Annotation, SynthesizedAnnotation { private final Annotation root; private final Annotation annotation; diff --git a/hutool-core/src/test/java/cn/hutool/core/annotation/SyntheticMetaAnnotationTest.java b/hutool-core/src/test/java/cn/hutool/core/annotation/SyntheticMetaAnnotationTest.java index e85862be9..009f1e205 100644 --- a/hutool-core/src/test/java/cn/hutool/core/annotation/SyntheticMetaAnnotationTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/annotation/SyntheticMetaAnnotationTest.java @@ -3,8 +3,10 @@ package cn.hutool.core.annotation; import org.junit.Assert; import org.junit.Test; -import java.lang.annotation.*; -import java.util.Map; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; /** * 合成注解{@link SyntheticMetaAnnotation}的测试用例 @@ -16,7 +18,7 @@ public class SyntheticMetaAnnotationTest { @Test public void testSynthesisAnnotation() { ChildAnnotation rootAnnotation = AnnotatedClass.class.getAnnotation(ChildAnnotation.class); - SyntheticMetaAnnotation syntheticMetaAnnotation = new SyntheticMetaAnnotation<>(rootAnnotation); + SyntheticMetaAnnotation syntheticMetaAnnotation = new SyntheticMetaAnnotation(rootAnnotation); Assert.assertEquals(syntheticMetaAnnotation.getSource(), rootAnnotation); Assert.assertEquals(syntheticMetaAnnotation.annotationType(), SyntheticMetaAnnotation.class); Assert.assertEquals(1, syntheticMetaAnnotation.getDeclaredAnnotations().length); @@ -28,28 +30,27 @@ public class SyntheticMetaAnnotationTest { Assert.assertEquals("Child's Parent!", syntheticMetaAnnotation.getAttribute("parentValue", String.class)); Assert.assertEquals("Child's GrandParent!", syntheticMetaAnnotation.getAttribute("grandParentValue", String.class)); - Map, SyntheticMetaAnnotation.MetaAnnotation> annotationMap = syntheticMetaAnnotation.getMetaAnnotationMap(); ChildAnnotation childAnnotation = syntheticMetaAnnotation.syntheticAnnotation(ChildAnnotation.class); Assert.assertTrue(syntheticMetaAnnotation.isAnnotationPresent(ChildAnnotation.class)); Assert.assertNotNull(childAnnotation); Assert.assertEquals("Child!", childAnnotation.childValue()); Assert.assertEquals("Child!", childAnnotation.childValueAlias()); Assert.assertEquals(childAnnotation.grandParentType(), Integer.class); - Assert.assertThrows(IllegalArgumentException.class, () -> new SyntheticMetaAnnotation<>(childAnnotation)); + Assert.assertThrows(IllegalArgumentException.class, () -> new SyntheticMetaAnnotation(childAnnotation)); ParentAnnotation parentAnnotation = syntheticMetaAnnotation.syntheticAnnotation(ParentAnnotation.class); Assert.assertTrue(syntheticMetaAnnotation.isAnnotationPresent(ParentAnnotation.class)); Assert.assertNotNull(parentAnnotation); Assert.assertEquals("Child's Parent!", parentAnnotation.parentValue()); Assert.assertEquals("java.lang.Void", parentAnnotation.grandParentType()); - Assert.assertThrows(IllegalArgumentException.class, () -> new SyntheticMetaAnnotation<>(parentAnnotation)); + Assert.assertThrows(IllegalArgumentException.class, () -> new SyntheticMetaAnnotation(parentAnnotation)); GrandParentAnnotation grandParentAnnotation = syntheticMetaAnnotation.syntheticAnnotation(GrandParentAnnotation.class); Assert.assertTrue(syntheticMetaAnnotation.isAnnotationPresent(GrandParentAnnotation.class)); Assert.assertNotNull(grandParentAnnotation); Assert.assertEquals("Child's GrandParent!", grandParentAnnotation.grandParentValue()); Assert.assertEquals(grandParentAnnotation.grandParentType(), Integer.class); - Assert.assertThrows(IllegalArgumentException.class, () -> new SyntheticMetaAnnotation<>(grandParentAnnotation)); + Assert.assertThrows(IllegalArgumentException.class, () -> new SyntheticMetaAnnotation(grandParentAnnotation)); } // 注解结构如下: