优化代码,调整类名

This commit is contained in:
huangchengxing 2022-07-13 22:42:16 +08:00
parent cf99ec14ca
commit 18f67274b8
25 changed files with 158 additions and 176 deletions

View File

@ -21,7 +21,7 @@ public abstract class AbstractLinkAnnotationPostProcessor implements Synthesized
/** /**
* 若一个注解属性上存在{@link Link}注解注解的{@link Link#type()}返回值在{@link #processTypes()}中存在 * 若一个注解属性上存在{@link Link}注解注解的{@link Link#type()}返回值在{@link #processTypes()}中存在
* 且此{@link Link}指定的注解对象在当前的{@link SynthesizedAnnotationAggregator}中存在 * 且此{@link Link}指定的注解对象在当前的{@link SynthesizedAggregateAnnotation}中存在
* 则从聚合器中获取类型对应的合成注解对象与该对象中的指定属性然后将全部关联数据交给 * 则从聚合器中获取类型对应的合成注解对象与该对象中的指定属性然后将全部关联数据交给
* {@link #processLinkedAttribute}处理 * {@link #processLinkedAttribute}处理
* *
@ -29,7 +29,7 @@ public abstract class AbstractLinkAnnotationPostProcessor implements Synthesized
* @param aggregator 合成注解聚合器 * @param aggregator 合成注解聚合器
*/ */
@Override @Override
public void process(SynthesizedAnnotation synthesizedAnnotation, SynthesizedAnnotationAggregator aggregator) { public void process(SynthesizedAnnotation synthesizedAnnotation, SynthesizedAggregateAnnotation aggregator) {
final Map<String, AnnotationAttribute> attributeMap = new HashMap<>(synthesizedAnnotation.getAttributes()); final Map<String, AnnotationAttribute> attributeMap = new HashMap<>(synthesizedAnnotation.getAttributes());
attributeMap.forEach((originalAttributeName, originalAttribute) -> { attributeMap.forEach((originalAttributeName, originalAttribute) -> {
// 获取注解 // 获取注解
@ -72,7 +72,7 @@ public abstract class AbstractLinkAnnotationPostProcessor implements Synthesized
* @param linkedAttribute {@link Link}指向的{@code originalAnnotation}中的关联属性该参数可能为空 * @param linkedAttribute {@link Link}指向的{@code originalAnnotation}中的关联属性该参数可能为空
*/ */
protected abstract void processLinkedAttribute( protected abstract void processLinkedAttribute(
SynthesizedAnnotationAggregator aggregator, Link annotation, SynthesizedAggregateAnnotation aggregator, Link annotation,
SynthesizedAnnotation originalAnnotation, AnnotationAttribute originalAttribute, SynthesizedAnnotation originalAnnotation, AnnotationAttribute originalAttribute,
SynthesizedAnnotation linkedAnnotation, AnnotationAttribute linkedAttribute SynthesizedAnnotation linkedAnnotation, AnnotationAttribute linkedAttribute
); );
@ -100,7 +100,7 @@ public abstract class AbstractLinkAnnotationPostProcessor implements Synthesized
* @param synthesizedAnnotationAggregator 合成注解 * @param synthesizedAnnotationAggregator 合成注解
*/ */
protected SynthesizedAnnotation getLinkedAnnotation( protected SynthesizedAnnotation getLinkedAnnotation(
Link annotation, SynthesizedAnnotationAggregator synthesizedAnnotationAggregator, Class<? extends Annotation> defaultType) { Link annotation, SynthesizedAggregateAnnotation synthesizedAnnotationAggregator, Class<? extends Annotation> defaultType) {
final Class<?> targetAnnotationType = getLinkedAnnotationType(annotation, defaultType); final Class<?> targetAnnotationType = getLinkedAnnotationType(annotation, defaultType);
return synthesizedAnnotationAggregator.getSynthesizedAnnotation(targetAnnotationType); return synthesizedAnnotationAggregator.getSynthesizedAnnotation(targetAnnotationType);
} }

View File

@ -20,7 +20,7 @@ import java.util.stream.Stream;
*/ */
public abstract class AbstractSynthesizedAnnotation<R> implements Annotation, SynthesizedAnnotation { public abstract class AbstractSynthesizedAnnotation<R> implements Annotation, SynthesizedAnnotation {
private final SynthesizedAnnotationAggregator owner; private final SynthesizedAggregateAnnotation owner;
private final R root; private final R root;
private final Annotation annotation; private final Annotation annotation;
private final Map<String, AnnotationAttribute> attributeMethodCaches; private final Map<String, AnnotationAttribute> attributeMethodCaches;
@ -37,7 +37,7 @@ public abstract class AbstractSynthesizedAnnotation<R> implements Annotation, Sy
* @param horizontalDistance 距离根对象的垂直距离 * @param horizontalDistance 距离根对象的垂直距离
*/ */
protected AbstractSynthesizedAnnotation( protected AbstractSynthesizedAnnotation(
SynthesizedAnnotationAggregator owner, R root, Annotation annotation, int verticalDistance, int horizontalDistance) { SynthesizedAggregateAnnotation owner, R root, Annotation annotation, int verticalDistance, int horizontalDistance) {
this.owner = owner; this.owner = owner;
this.root = root; this.root = root;
this.annotation = annotation; this.annotation = annotation;
@ -53,7 +53,7 @@ public abstract class AbstractSynthesizedAnnotation<R> implements Annotation, Sy
* @return 注解属性 * @return 注解属性
*/ */
protected Map<String, AnnotationAttribute> loadAttributeMethods() { protected Map<String, AnnotationAttribute> loadAttributeMethods() {
return Stream.of(annotation.annotationType().getDeclaredMethods()) return Stream.of(ClassUtil.getDeclaredMethods(annotation.annotationType()))
.filter(AnnotationUtil::isAttributeMethod) .filter(AnnotationUtil::isAttributeMethod)
.collect(Collectors.toMap(Method::getName, method -> new CacheableAnnotationAttribute(annotation, method))); .collect(Collectors.toMap(Method::getName, method -> new CacheableAnnotationAttribute(annotation, method)));
} }
@ -136,7 +136,7 @@ public abstract class AbstractSynthesizedAnnotation<R> implements Annotation, Sy
* @return 合成注解 * @return 合成注解
*/ */
@Override @Override
public SynthesizedAnnotationAggregator getOwner() { public SynthesizedAggregateAnnotation getOwner() {
return owner; return owner;
} }

View File

@ -0,0 +1,27 @@
package cn.hutool.core.annotation;
import java.lang.annotation.Annotation;
/**
* 表示一组被聚合在一起的注解对象
*
* @author huangchengxing
*/
public interface AggregateAnnotation extends Annotation {
/**
* 在聚合中是否存在的指定类型注解对象
*
* @param annotationType 注解类型
* @return 是否
*/
boolean isAnnotationPresent(Class<? extends Annotation> annotationType);
/**
* 获取聚合中的全部注解对象
*
* @return 注解对象
*/
Annotation[] getAnnotations();
}

View File

@ -27,7 +27,7 @@ public class AliasAnnotationPostProcessor implements SynthesizedAnnotationPostPr
} }
@Override @Override
public void process(SynthesizedAnnotation synthesizedAnnotation, SynthesizedAnnotationAggregator aggregator) { public void process(SynthesizedAnnotation synthesizedAnnotation, SynthesizedAggregateAnnotation aggregator) {
final Map<String, AnnotationAttribute> attributeMap = synthesizedAnnotation.getAttributes(); final Map<String, AnnotationAttribute> attributeMap = synthesizedAnnotation.getAttributes();
// 记录别名与属性的关系 // 记录别名与属性的关系

View File

@ -52,7 +52,7 @@ public class AliasLinkAnnotationPostProcessor extends AbstractLinkAnnotationPost
*/ */
@Override @Override
protected void processLinkedAttribute( protected void processLinkedAttribute(
SynthesizedAnnotationAggregator aggregator, Link annotation, SynthesizedAggregateAnnotation aggregator, Link annotation,
SynthesizedAnnotation originalAnnotation, AnnotationAttribute originalAttribute, SynthesizedAnnotation originalAnnotation, AnnotationAttribute originalAttribute,
SynthesizedAnnotation linkedAnnotation, AnnotationAttribute linkedAttribute) { SynthesizedAnnotation linkedAnnotation, AnnotationAttribute linkedAttribute) {
// 校验别名关系 // 校验别名关系
@ -70,7 +70,7 @@ public class AliasLinkAnnotationPostProcessor extends AbstractLinkAnnotationPost
* 对指定注解属性进行包装若该属性已被包装过则递归以其为根节点的树结构对树上全部的叶子节点进行包装 * 对指定注解属性进行包装若该属性已被包装过则递归以其为根节点的树结构对树上全部的叶子节点进行包装
*/ */
private void wrappingLinkedAttribute( private void wrappingLinkedAttribute(
SynthesizedAnnotationAggregator synthesizedAnnotationAggregator, AnnotationAttribute originalAttribute, AnnotationAttribute aliasAttribute, BinaryOperator<AnnotationAttribute> wrapping) { SynthesizedAggregateAnnotation synthesizedAnnotationAggregator, AnnotationAttribute originalAttribute, AnnotationAttribute aliasAttribute, BinaryOperator<AnnotationAttribute> wrapping) {
// 不是包装属性 // 不是包装属性
if (!aliasAttribute.isWrapped()) { if (!aliasAttribute.isWrapped()) {
processAttribute(synthesizedAnnotationAggregator, originalAttribute, aliasAttribute, wrapping); processAttribute(synthesizedAnnotationAggregator, originalAttribute, aliasAttribute, wrapping);
@ -87,7 +87,7 @@ public class AliasLinkAnnotationPostProcessor extends AbstractLinkAnnotationPost
* 获取指定注解属性然后将其再进行一层包装 * 获取指定注解属性然后将其再进行一层包装
*/ */
private void processAttribute( private void processAttribute(
SynthesizedAnnotationAggregator synthesizedAnnotationAggregator, AnnotationAttribute originalAttribute, SynthesizedAggregateAnnotation synthesizedAnnotationAggregator, AnnotationAttribute originalAttribute,
AnnotationAttribute target, BinaryOperator<AnnotationAttribute> wrapping) { AnnotationAttribute target, BinaryOperator<AnnotationAttribute> wrapping) {
Opt.ofNullable(target.getAnnotationType()) Opt.ofNullable(target.getAnnotationType())
.map(synthesizedAnnotationAggregator::getSynthesizedAnnotation) .map(synthesizedAnnotationAggregator::getSynthesizedAnnotation)

View File

@ -1,49 +0,0 @@
package cn.hutool.core.annotation;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
/**
* 表示一组被聚合在一起的注解对象
*
* @author huangchengxing
*/
public interface AnnotationAggregator extends AnnotatedElement {
/**
* 获取在聚合中存在的指定注解对象
*
* @param annotationType 注解类型
* @param <T> 注解类型
* @return 注解对象
*/
@Override
<T extends Annotation> T getAnnotation(Class<T> annotationType);
/**
* 在聚合中是否存在的指定类型注解对象
*
* @param annotationType 注解类型
* @return 是否
*/
@Override
boolean isAnnotationPresent(Class<? extends Annotation> annotationType);
/**
* 获取聚合中的全部注解对象
*
* @return 注解对象
*/
@Override
Annotation[] getAnnotations();
/**
* 从聚合中获取指定类型的属性值
*
* @param attributeName 属性名称
* @param attributeType 属性类型
* @return 属性值
*/
Object getAttribute(String attributeName, Class<?> attributeType);
}

View File

@ -7,7 +7,7 @@ import java.lang.reflect.Method;
/** /**
* <p>表示注解的某个属性等同于绑定的调用对象的{@link Method}方法<br> * <p>表示注解的某个属性等同于绑定的调用对象的{@link Method}方法<br>
* {@link SynthesizedAnnotationAggregator}的解析以及取值过程中 * {@link SynthesizedAggregateAnnotation}的解析以及取值过程中
* 可以通过设置{@link SynthesizedAnnotation}的注解属性 * 可以通过设置{@link SynthesizedAnnotation}的注解属性
* 从而使得可以从一个注解对象中属性获取另一个注解对象的属性值 * 从而使得可以从一个注解对象中属性获取另一个注解对象的属性值
* *

View File

@ -351,11 +351,11 @@ public class AnnotationUtil {
* @param annotationType 注解类 * @param annotationType 注解类
* @param <T> 注解类型 * @param <T> 注解类型
* @return 合成注解 * @return 合成注解
* @see SynthesizedAnnotationAggregator * @see SynthesizedAggregateAnnotation
*/ */
public static <T extends Annotation> T getSynthesizedAnnotation(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 SynthesizedAggregateAnnotation.of(annotation).synthesize(annotationType);
} }
/** /**
@ -369,7 +369,7 @@ public class AnnotationUtil {
* @param annotationType 注解类 * @param annotationType 注解类
* @param <T> 注解类型 * @param <T> 注解类型
* @return 合成注解 * @return 合成注解
* @see SynthesizedAnnotationAggregator * @see SynthesizedAggregateAnnotation
*/ */
public static <T extends Annotation> T getSynthesizedAnnotation(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);
@ -397,7 +397,7 @@ public class AnnotationUtil {
* @param annotationType 注解类 * @param annotationType 注解类
* @param <T> 注解类型 * @param <T> 注解类型
* @return 合成注解 * @return 合成注解
* @see SynthesizedAnnotationAggregator * @see SynthesizedAggregateAnnotation
*/ */
public static <T extends Annotation> List<T> getAllSynthesizedAnnotations(AnnotatedElement annotatedEle, Class<T> annotationType) { public static <T extends Annotation> List<T> getAllSynthesizedAnnotations(AnnotatedElement annotatedEle, Class<T> annotationType) {
AnnotationScanner[] scanners = new AnnotationScanner[]{ AnnotationScanner[] scanners = new AnnotationScanner[]{

View File

@ -4,7 +4,7 @@ import java.lang.annotation.*;
/** /**
* <p>用于在同一注解中或具有一定关联的不同注解的属性中表明这些属性之间具有特定的关联关系 * <p>用于在同一注解中或具有一定关联的不同注解的属性中表明这些属性之间具有特定的关联关系
* 在通过{@link SynthesizedAnnotationAggregator}获取合成注解后合成注解获取属性值时会根据该注解进行调整<br /> * 在通过{@link SynthesizedAggregateAnnotation}获取合成注解后合成注解获取属性值时会根据该注解进行调整<br />
* *
* <p>该注解存在三个字注解{@link MirrorFor}{@link ForceAliasFor}{@link AliasFor} * <p>该注解存在三个字注解{@link MirrorFor}{@link ForceAliasFor}{@link AliasFor}
* 使用三个子注解等同于{@link Link}但是需要注意的是 * 使用三个子注解等同于{@link Link}但是需要注意的是
@ -14,7 +14,7 @@ import java.lang.annotation.*;
* <b>注意该注解的优先级低于{@link Alias}</b> * <b>注意该注解的优先级低于{@link Alias}</b>
* *
* @author huangchengxing * @author huangchengxing
* @see SynthesizedAnnotationAggregator * @see SynthesizedAggregateAnnotation
* @see RelationType * @see RelationType
* @see AliasFor * @see AliasFor
* @see MirrorFor * @see MirrorFor

View File

@ -45,7 +45,7 @@ public class MirrorLinkAnnotationPostProcessor extends AbstractLinkAnnotationPos
*/ */
@Override @Override
protected void processLinkedAttribute( protected void processLinkedAttribute(
SynthesizedAnnotationAggregator aggregator, Link annotation, SynthesizedAggregateAnnotation aggregator, Link annotation,
SynthesizedAnnotation originalAnnotation, AnnotationAttribute originalAttribute, SynthesizedAnnotation originalAnnotation, AnnotationAttribute originalAttribute,
SynthesizedAnnotation linkedAnnotation, AnnotationAttribute linkedAttribute) { SynthesizedAnnotation linkedAnnotation, AnnotationAttribute linkedAttribute) {

View File

@ -3,8 +3,8 @@ package cn.hutool.core.annotation;
/** /**
* <p>注解属性的关系类型 <br> * <p>注解属性的关系类型 <br>
* 若将被{@link Link}注解的属性称为原始属性而在{@link Link}注解中指向的注解属性称为关联属性 * 若将被{@link Link}注解的属性称为原始属性而在{@link Link}注解中指向的注解属性称为关联属性
* 则该枚举用于描述原始属性关联属性{@link SynthesizedAnnotationAggregator}处理过程中的作用关系<br> * 则该枚举用于描述原始属性关联属性{@link SynthesizedAggregateAnnotation}处理过程中的作用关系<br>
* 根据在{@link Link#type()}中指定的关系类型的不同通过{@link SynthesizedAnnotationAggregator}合成的注解的属性值也将有所变化 * 根据在{@link Link#type()}中指定的关系类型的不同通过{@link SynthesizedAggregateAnnotation}合成的注解的属性值也将有所变化
* *
* <p>当一个注解中的所有属性同时具备多种关系时将依次按下述顺序处理 * <p>当一个注解中的所有属性同时具备多种关系时将依次按下述顺序处理
* <ol> * <ol>
@ -15,7 +15,7 @@ package cn.hutool.core.annotation;
* </ol> * </ol>
* *
* @author huangchengxing * @author huangchengxing
* @see SynthesizedAnnotationAggregator * @see SynthesizedAggregateAnnotation
* @see Link * @see Link
*/ */
public enum RelationType { public enum RelationType {

View File

@ -1,16 +1,15 @@
package cn.hutool.core.annotation; package cn.hutool.core.annotation;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.util.Collection; import java.util.Collection;
/** /**
* 表示基于特定规则聚合的一组注解对象 * 表示基于特定规则聚合将一组注解聚合而来的注解对象
* *
* <p>合成注解一般被用于处理类层级结果中具有直接或间接关联的注解对象 * <p>合成注解一般被用于处理类层级结果中具有直接或间接关联的注解对象
* 当实例被创建时会获取到这些注解对象并使用{@link SynthesizedAnnotationSelector}对类型相同的注解进行过滤 * 当实例被创建时会获取到这些注解对象并使用{@link SynthesizedAnnotationSelector}对类型相同的注解进行过滤
* 并最终得到类型不重复的有效注解对象这些有效注解将被包装为{@link SynthesizedAnnotation} * 并最终得到类型不重复的有效注解对象这些有效注解将被包装为{@link SynthesizedAnnotation}
* 然后最终用于合成一个{@link SynthesizedAnnotationAggregator}<br> * 然后最终用于合成一个{@link SynthesizedAggregateAnnotation}<br>
* {@link SynthesizedAnnotationSelector}是合成注解生命周期中的第一个钩子 * {@link SynthesizedAnnotationSelector}是合成注解生命周期中的第一个钩子
* 自定义选择器以拦截原始注解被扫描的过程 * 自定义选择器以拦截原始注解被扫描的过程
* *
@ -28,18 +27,23 @@ import java.util.Collection;
* {@link SynthesizedAnnotationAttributeProcessor}是合成注解生命周期中的第三个钩子 * {@link SynthesizedAnnotationAttributeProcessor}是合成注解生命周期中的第三个钩子
* 自定义属性处理器以拦截合成注解的取值过程 * 自定义属性处理器以拦截合成注解的取值过程
* *
* <p>合成注解可以作为一个特殊的{@link Annotation}或者{@link AnnotatedElement}
* 当调用{@link Annotation}的方法时应当返回当前实例本身的有效信息
* 而当调用{@link AnnotatedElement}的方法时应当返回用于合成该对象的相关注解的信息
*
* @author huangchengxing * @author huangchengxing
* @see SynthesizedAnnotation * @see SynthesizedAnnotation
* @see SynthesizedAnnotationSelector * @see SynthesizedAnnotationSelector
* @see SynthesizedAnnotationAttributeProcessor * @see SynthesizedAnnotationAttributeProcessor
* @see SynthesizedAnnotationPostProcessor * @see SynthesizedAnnotationPostProcessor
* @see SynthesizedMetaAnnotationAggregator * @see SynthesizedMetaAggregateAnnotation
*/ */
public interface SynthesizedAnnotationAggregator extends Annotation, AnnotationAggregator { public interface SynthesizedAggregateAnnotation extends Annotation, AggregateAnnotation {
/**
* 获取在聚合中存在的指定注解对象
*
* @param annotationType 注解类型
* @param <T> 注解类型
* @return 注解对象
*/
<T extends Annotation> T getAnnotation(Class<T> annotationType);
/** /**
* 获取合成注解选择器 * 获取合成注解选择器
@ -87,6 +91,15 @@ public interface SynthesizedAnnotationAggregator extends Annotation, AnnotationA
return this.getClass(); return this.getClass();
} }
/**
* 从聚合中获取指定类型的属性值
*
* @param attributeName 属性名称
* @param attributeType 属性类型
* @return 属性值
*/
Object getAttribute(String attributeName, Class<?> attributeType);
/** /**
* 获取合成注解 * 获取合成注解
* *
@ -103,8 +116,8 @@ public interface SynthesizedAnnotationAggregator extends Annotation, AnnotationA
* @param <T> 注解类型 * @param <T> 注解类型
* @return 合成注解 * @return 合成注解
*/ */
static <T extends Annotation> SynthesizedAnnotationAggregator of(T rootAnnotation) { static <T extends Annotation> SynthesizedAggregateAnnotation of(T rootAnnotation) {
return new SynthesizedMetaAnnotationAggregator(rootAnnotation); return new SynthesizedMetaAggregateAnnotation(rootAnnotation);
} }
} }

View File

@ -8,13 +8,13 @@ import java.util.Map;
import java.util.function.UnaryOperator; import java.util.function.UnaryOperator;
/** /**
* <p>用于在{@link SynthesizedAnnotationAggregator}中表示一个处于合成状态的注解对象<br> * <p>用于在{@link SynthesizedAggregateAnnotation}中表示一个处于合成状态的注解对象<br>
* 当对多个合成注解排序时默认使用{@link #DEFAULT_CHILD_PRIORITY_COMPARATOR}进行排序 * 当对多个合成注解排序时默认使用{@link #DEFAULT_CHILD_PRIORITY_COMPARATOR}进行排序
* 从保证合成注解按{@link #getVerticalDistance()}{@link #getHorizontalDistance()}的返回值保持有序 * 从保证合成注解按{@link #getVerticalDistance()}{@link #getHorizontalDistance()}的返回值保持有序
* 从而使得距离根元素更接近的注解对象在被处理是具有更高的优先级 * 从而使得距离根元素更接近的注解对象在被处理是具有更高的优先级
* *
* @author huangchengxing * @author huangchengxing
* @see SynthesizedAnnotationAggregator * @see SynthesizedAggregateAnnotation
*/ */
public interface SynthesizedAnnotation extends Annotation, Comparable<SynthesizedAnnotation> { public interface SynthesizedAnnotation extends Annotation, Comparable<SynthesizedAnnotation> {
@ -32,7 +32,7 @@ public interface SynthesizedAnnotation extends Annotation, Comparable<Synthesize
* *
* @return 合成注解 * @return 合成注解
*/ */
SynthesizedAnnotationAggregator getOwner(); SynthesizedAggregateAnnotation getOwner();
/** /**
* 获取该合成注解对应的根节点 * 获取该合成注解对应的根节点

View File

@ -3,7 +3,7 @@ package cn.hutool.core.annotation;
import java.util.Collection; import java.util.Collection;
/** /**
* 合成注解属性选择器用于在{@link SynthesizedAnnotationAggregator}中从指定类型的合成注解里获取到对应的属性值 * 合成注解属性选择器用于在{@link SynthesizedAggregateAnnotation}中从指定类型的合成注解里获取到对应的属性值
* *
* @author huangchengxing * @author huangchengxing
*/ */

View File

@ -5,7 +5,7 @@ import cn.hutool.core.comparator.CompareUtil;
import java.util.Comparator; import java.util.Comparator;
/** /**
* <p>被合成注解后置处理器用于在{@link SynthesizedAnnotationAggregator}加载完所有待合成注解后 * <p>被合成注解后置处理器用于在{@link SynthesizedAggregateAnnotation}加载完所有待合成注解后
* 再对加载好的{@link SynthesizedAnnotation}进行后置处理<br> * 再对加载好的{@link SynthesizedAnnotation}进行后置处理<br>
* 当多个{@link SynthesizedAnnotationPostProcessor}需要一起执行时将按照{@link #order()}的返回值进行排序 * 当多个{@link SynthesizedAnnotationPostProcessor}需要一起执行时将按照{@link #order()}的返回值进行排序
* 该值更小的处理器将被优先执行 * 该值更小的处理器将被优先执行
@ -25,6 +25,21 @@ import java.util.Comparator;
*/ */
public interface SynthesizedAnnotationPostProcessor extends Comparable<SynthesizedAnnotationPostProcessor> { public interface SynthesizedAnnotationPostProcessor extends Comparable<SynthesizedAnnotationPostProcessor> {
/**
* 属性上带有{@link Alias}的注解对象的后置处理器
*/
AliasAnnotationPostProcessor ALIAS_ANNOTATION_POST_PROCESSOR = new AliasAnnotationPostProcessor();
/**
* 属性上带有{@link Link}且与其他注解的属性存在镜像关系的注解对象的后置处理器
*/
MirrorLinkAnnotationPostProcessor MIRROR_LINK_ANNOTATION_POST_PROCESSOR = new MirrorLinkAnnotationPostProcessor();
/**
* 属性上带有{@link Link}且与其他注解的属性存在别名关系的注解对象的后置处理器
*/
AliasLinkAnnotationPostProcessor ALIAS_LINK_ANNOTATION_POST_PROCESSOR = new AliasLinkAnnotationPostProcessor();
/** /**
* 在一组后置处理器中被调用的顺序越小越靠前 * 在一组后置处理器中被调用的顺序越小越靠前
* *
@ -51,6 +66,6 @@ public interface SynthesizedAnnotationPostProcessor extends Comparable<Synthesiz
* @param synthesizedAnnotation 合成的注解 * @param synthesizedAnnotation 合成的注解
* @param aggregator 合成注解聚合器 * @param aggregator 合成注解聚合器
*/ */
void process(SynthesizedAnnotation synthesizedAnnotation, SynthesizedAnnotationAggregator aggregator); void process(SynthesizedAnnotation synthesizedAnnotation, SynthesizedAggregateAnnotation aggregator);
} }

View File

@ -2,7 +2,7 @@ package cn.hutool.core.annotation;
/** /**
* 注解选择器指定两个注解选择其中一个返回<br> * 注解选择器指定两个注解选择其中一个返回<br>
* 该接口用于在{@link SynthesizedAnnotationAggregator}中用于从一批相同的注解对象中筛选最终用于合成注解对象 * 该接口用于在{@link SynthesizedAggregateAnnotation}中用于从一批相同的注解对象中筛选最终用于合成注解对象
* *
* @author huangchengxing * @author huangchengxing
*/ */

View File

@ -10,10 +10,10 @@ import java.lang.reflect.AnnotatedElement;
import java.util.*; import java.util.*;
/** /**
* {@link SynthesizedAnnotationAggregator}的基本实现表示一个根注解与根注解上的多层元注解的聚合状态 * {@link SynthesizedAggregateAnnotation}的基本实现表示一个根注解与根注解上的多层元注解的聚合得到的注解
* *
* <p>假设现有注解AA上存在元注解BB上存在元注解C则对注解A进行解析 * <p>假设现有注解AA上存在元注解BB上存在元注解C则对注解A进行解析
* 将得到包含根注解A以及其元注解BC在内的合成元注解聚合{@link SynthesizedMetaAnnotationAggregator} * 将得到包含根注解A以及其元注解BC在内的合成元注解聚合{@link SynthesizedMetaAggregateAnnotation}
* {@link AnnotatedElement}的角度来说得到的合成注解是一个同时承载有ABC三个注解对象的被注解元素 * {@link AnnotatedElement}的角度来说得到的合成注解是一个同时承载有ABC三个注解对象的被注解元素
* 因此通过调用{@link AnnotatedElement}的相关方法将返回对应符合语义的注解对象 * 因此通过调用{@link AnnotatedElement}的相关方法将返回对应符合语义的注解对象
* *
@ -34,7 +34,7 @@ import java.util.*;
* </ul> * </ul>
* 若用户需要自行扩展则需要保证上述三个处理器被正确注入当前实例 * 若用户需要自行扩展则需要保证上述三个处理器被正确注入当前实例
* *
* <p>{@link SynthesizedMetaAnnotationAggregator}支持通过{@link #getAttribute(String, Class)} * <p>{@link SynthesizedMetaAggregateAnnotation}支持通过{@link #getAttribute(String, Class)}
* 或通过{@link #synthesize(Class)}获得注解代理对象后获取指定类型的注解属性值 * 或通过{@link #synthesize(Class)}获得注解代理对象后获取指定类型的注解属性值
* 返回的属性值将根据合成注解中对应原始注解属性上的{@link Alias}{@link Link}注解而有所变化 * 返回的属性值将根据合成注解中对应原始注解属性上的{@link Alias}{@link Link}注解而有所变化
* 通过当前实例获取属性值时将经过{@link SynthesizedAnnotationAttributeProcessor}的处理<br> * 通过当前实例获取属性值时将经过{@link SynthesizedAnnotationAttributeProcessor}的处理<br>
@ -45,7 +45,7 @@ import java.util.*;
* @see AnnotationUtil * @see AnnotationUtil
* @see SynthesizedAnnotationSelector * @see SynthesizedAnnotationSelector
*/ */
public class SynthesizedMetaAnnotationAggregator implements SynthesizedAnnotationAggregator { public class SynthesizedMetaAggregateAnnotation implements SynthesizedAggregateAnnotation {
/** /**
* 根注解即当前查找的注解 * 根注解即当前查找的注解
@ -79,14 +79,14 @@ public class SynthesizedMetaAnnotationAggregator implements SynthesizedAnnotatio
* *
* @param source 源注解 * @param source 源注解
*/ */
public SynthesizedMetaAnnotationAggregator(Annotation source) { public SynthesizedMetaAggregateAnnotation(Annotation source) {
this( this(
source, SynthesizedAnnotationSelector.NEAREST_AND_OLDEST_PRIORITY, source, SynthesizedAnnotationSelector.NEAREST_AND_OLDEST_PRIORITY,
new CacheableSynthesizedAnnotationAttributeProcessor(), new CacheableSynthesizedAnnotationAttributeProcessor(),
Arrays.asList( Arrays.asList(
new AliasAnnotationPostProcessor(), SynthesizedAnnotationPostProcessor.ALIAS_ANNOTATION_POST_PROCESSOR,
new MirrorLinkAnnotationPostProcessor(), SynthesizedAnnotationPostProcessor.MIRROR_LINK_ANNOTATION_POST_PROCESSOR,
new AliasLinkAnnotationPostProcessor() SynthesizedAnnotationPostProcessor.ALIAS_LINK_ANNOTATION_POST_PROCESSOR
) )
); );
} }
@ -98,7 +98,7 @@ public class SynthesizedMetaAnnotationAggregator implements SynthesizedAnnotatio
* @param annotationSelector 合成注解选择器 * @param annotationSelector 合成注解选择器
* @param attributeProcessor 注解属性处理器 * @param attributeProcessor 注解属性处理器
*/ */
public SynthesizedMetaAnnotationAggregator( public SynthesizedMetaAggregateAnnotation(
Annotation annotation, Annotation annotation,
SynthesizedAnnotationSelector annotationSelector, SynthesizedAnnotationSelector annotationSelector,
SynthesizedAnnotationAttributeProcessor attributeProcessor, SynthesizedAnnotationAttributeProcessor attributeProcessor,
@ -240,23 +240,13 @@ public class SynthesizedMetaAnnotationAggregator implements SynthesizedAnnotatio
* *
* @param annotationType 注解类型 * @param annotationType 注解类型
* @return 合成注解对象 * @return 合成注解对象
* @see SyntheticAnnotationProxy#create(Class, SynthesizedAnnotationAggregator) * @see SyntheticAnnotationProxy#create(Class, SynthesizedAggregateAnnotation)
*/ */
@Override @Override
public <T extends Annotation> T synthesize(Class<T> annotationType) { public <T extends Annotation> T synthesize(Class<T> annotationType) {
return SyntheticAnnotationProxy.create(annotationType, this); return SyntheticAnnotationProxy.create(annotationType, this);
} }
/**
* 获取根注解直接声明的注解该方法正常情况下当只返回原注解
*
* @return 直接声明注解
*/
@Override
public Annotation[] getDeclaredAnnotations() {
return new Annotation[]{getSource()};
}
/** /**
* 广度优先遍历并缓存该根注解上的全部元注解 * 广度优先遍历并缓存该根注解上的全部元注解
*/ */
@ -294,7 +284,7 @@ public class SynthesizedMetaAnnotationAggregator implements SynthesizedAnnotatio
* @param verticalDistance 距离根对象的水平距离 * @param verticalDistance 距离根对象的水平距离
* @param horizontalDistance 距离根对象的垂直距离 * @param horizontalDistance 距离根对象的垂直距离
*/ */
protected MetaAnnotation(SynthesizedAnnotationAggregator owner, Annotation root, Annotation annotation, int verticalDistance, int horizontalDistance) { protected MetaAnnotation(SynthesizedAggregateAnnotation owner, Annotation root, Annotation annotation, int verticalDistance, int horizontalDistance) {
super(owner, root, annotation, verticalDistance, horizontalDistance); super(owner, root, annotation, verticalDistance, horizontalDistance);
} }

View File

@ -38,7 +38,7 @@ class SyntheticAnnotationProxy implements InvocationHandler {
/** /**
* 创建一个代理注解生成的代理对象将是{@link SyntheticProxyAnnotation}与指定的注解类的子类 * 创建一个代理注解生成的代理对象将是{@link SyntheticProxyAnnotation}与指定的注解类的子类
* <ul> * <ul>
* <li>当作为{@code annotationType}所指定的类型使用时其属性将通过合成它的{@link SynthesizedAnnotationAggregator}获取</li> * <li>当作为{@code annotationType}所指定的类型使用时其属性将通过合成它的{@link SynthesizedAggregateAnnotation}获取</li>
* <li>当作为{@link SyntheticProxyAnnotation}{@link SynthesizedAnnotation}使用时将可以获得原始注解实例的相关信息</li> * <li>当作为{@link SyntheticProxyAnnotation}{@link SynthesizedAnnotation}使用时将可以获得原始注解实例的相关信息</li>
* </ul> * </ul>
* *
@ -48,7 +48,7 @@ class SyntheticAnnotationProxy implements InvocationHandler {
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
static <T extends Annotation> T create( static <T extends Annotation> T create(
Class<T> annotationType, SynthesizedAnnotationAggregator synthesizedAnnotationAggregator) { Class<T> annotationType, SynthesizedAggregateAnnotation synthesizedAnnotationAggregator) {
final SynthesizedAnnotation annotation = synthesizedAnnotationAggregator.getSynthesizedAnnotation(annotationType); final SynthesizedAnnotation annotation = synthesizedAnnotationAggregator.getSynthesizedAnnotation(annotationType);
if (ObjectUtil.isNull(annotation)) { if (ObjectUtil.isNull(annotation)) {
return null; return null;
@ -93,13 +93,13 @@ class SyntheticAnnotationProxy implements InvocationHandler {
methods.put("getAttributeValue", (method, args) -> annotation.getAttributeValue((String)args[0])); methods.put("getAttributeValue", (method, args) -> annotation.getAttributeValue((String)args[0]));
methods.put("getOwner", (method, args) -> annotation.getOwner()); methods.put("getOwner", (method, args) -> annotation.getOwner());
methods.put("annotationType", (method, args) -> annotation.annotationType()); methods.put("annotationType", (method, args) -> annotation.annotationType());
for (final Method declaredMethod : annotation.getAnnotation().annotationType().getDeclaredMethods()) { for (final Method declaredMethod : ClassUtil.getDeclaredMethods(annotation.getAnnotation().annotationType())) {
methods.put(declaredMethod.getName(), (method, args) -> proxyAttributeValue(method)); methods.put(declaredMethod.getName(), (method, args) -> proxyAttributeValue(method));
} }
} }
private String proxyToString() { private String proxyToString() {
final String attributes = Stream.of(annotation.annotationType().getDeclaredMethods()) final String attributes = Stream.of(ClassUtil.getDeclaredMethods(annotation.getAnnotation().annotationType()))
.filter(AnnotationUtil::isAttributeMethod) .filter(AnnotationUtil::isAttributeMethod)
.map(method -> StrUtil.format("{}={}", method.getName(), annotation.getOwner().getAttribute(method.getName(), method.getReturnType()))) .map(method -> StrUtil.format("{}={}", method.getName(), annotation.getOwner().getAttribute(method.getName(), method.getReturnType())))
.collect(Collectors.joining(", ")); .collect(Collectors.joining(", "));
@ -134,7 +134,7 @@ class SyntheticAnnotationProxy implements InvocationHandler {
* *
* @return 合成注解 * @return 合成注解
*/ */
SynthesizedAnnotationAggregator getSynthesizedAnnotationAggregator(); SynthesizedAggregateAnnotation getSynthesizedAnnotationAggregator();
/** /**
* 获取该代理注解对应的已合成注解 * 获取该代理注解对应的已合成注解

View File

@ -80,7 +80,7 @@ public class MethodAnnotationScanner extends AbstractTypeAnnotationScanner<Metho
@Override @Override
protected Annotation[] getAnnotationsFromTargetClass(AnnotatedElement source, int index, Class<?> targetClass) { protected Annotation[] getAnnotationsFromTargetClass(AnnotatedElement source, int index, Class<?> targetClass) {
final Method sourceMethod = (Method) source; final Method sourceMethod = (Method) source;
return Stream.of(targetClass.getDeclaredMethods()) return Stream.of(ClassUtil.getDeclaredMethods(targetClass))
.filter(superMethod -> !superMethod.isBridge()) .filter(superMethod -> !superMethod.isBridge())
.filter(superMethod -> hasSameSignature(sourceMethod, superMethod)) .filter(superMethod -> hasSameSignature(sourceMethod, superMethod))
.map(AnnotatedElement::getAnnotations) .map(AnnotatedElement::getAnnotations)

View File

@ -19,7 +19,7 @@ public class AliasAnnotationPostProcessorTest {
AliasAnnotationPostProcessor processor = new AliasAnnotationPostProcessor(); AliasAnnotationPostProcessor processor = new AliasAnnotationPostProcessor();
Map<Class<?>, SynthesizedAnnotation> annotationMap = new HashMap<>(); Map<Class<?>, SynthesizedAnnotation> annotationMap = new HashMap<>();
SynthesizedAnnotationAggregator synthesizedAnnotationAggregator = new TestSynthesizedAnnotationAggregator(annotationMap); SynthesizedAggregateAnnotation synthesizedAnnotationAggregator = new TestSynthesizedAggregateAnnotation(annotationMap);
AnnotationForTest annotation = ClassForTest.class.getAnnotation(AnnotationForTest.class); AnnotationForTest annotation = ClassForTest.class.getAnnotation(AnnotationForTest.class);
SynthesizedAnnotation synthesizedAnnotation = new TestSynthesizedAnnotation(synthesizedAnnotationAggregator, annotation); SynthesizedAnnotation synthesizedAnnotation = new TestSynthesizedAnnotation(synthesizedAnnotationAggregator, annotation);
annotationMap.put(annotation.annotationType(), synthesizedAnnotation); annotationMap.put(annotation.annotationType(), synthesizedAnnotation);
@ -49,11 +49,11 @@ public class AliasAnnotationPostProcessorTest {
String name() default ""; String name() default "";
} }
static class TestSynthesizedAnnotationAggregator implements SynthesizedAnnotationAggregator { static class TestSynthesizedAggregateAnnotation implements SynthesizedAggregateAnnotation {
private final Map<Class<?>, SynthesizedAnnotation> annotationMap; private final Map<Class<?>, SynthesizedAnnotation> annotationMap;
public TestSynthesizedAnnotationAggregator(Map<Class<?>, SynthesizedAnnotation> annotationMap) { public TestSynthesizedAggregateAnnotation(Map<Class<?>, SynthesizedAnnotation> annotationMap) {
this.annotationMap = annotationMap; this.annotationMap = annotationMap;
} }
@ -106,20 +106,15 @@ public class AliasAnnotationPostProcessorTest {
public Object getAttribute(String attributeName, Class<?> attributeType) { public Object getAttribute(String attributeName, Class<?> attributeType) {
return null; return null;
} }
@Override
public Annotation[] getDeclaredAnnotations() {
return new Annotation[0];
}
} }
static class TestSynthesizedAnnotation implements SynthesizedAnnotation { static class TestSynthesizedAnnotation implements SynthesizedAnnotation {
private final Annotation annotation; private final Annotation annotation;
private final SynthesizedAnnotationAggregator owner; private final SynthesizedAggregateAnnotation owner;
private final Map<String, AnnotationAttribute> attributeMap; private final Map<String, AnnotationAttribute> attributeMap;
public TestSynthesizedAnnotation(SynthesizedAnnotationAggregator owner, Annotation annotation) { public TestSynthesizedAnnotation(SynthesizedAggregateAnnotation owner, Annotation annotation) {
this.owner = owner; this.owner = owner;
this.attributeMap = new HashMap<>(); this.attributeMap = new HashMap<>();
this.annotation = annotation; this.annotation = annotation;
@ -129,7 +124,7 @@ public class AliasAnnotationPostProcessorTest {
} }
@Override @Override
public SynthesizedAnnotationAggregator getOwner() { public SynthesizedAggregateAnnotation getOwner() {
return owner; return owner;
} }

View File

@ -19,7 +19,7 @@ public class AliasLinkAnnotationPostProcessorTest {
AliasLinkAnnotationPostProcessor processor = new AliasLinkAnnotationPostProcessor(); AliasLinkAnnotationPostProcessor processor = new AliasLinkAnnotationPostProcessor();
Map<Class<?>, SynthesizedAnnotation> annotationMap = new HashMap<>(); Map<Class<?>, SynthesizedAnnotation> annotationMap = new HashMap<>();
SynthesizedAnnotationAggregator synthesizedAnnotationAggregator = new TestSynthesizedAnnotationAggregator(annotationMap); SynthesizedAggregateAnnotation synthesizedAnnotationAggregator = new TestSynthesizedAggregateAnnotation(annotationMap);
AnnotationForTest annotation = ClassForTest.class.getAnnotation(AnnotationForTest.class); AnnotationForTest annotation = ClassForTest.class.getAnnotation(AnnotationForTest.class);
SynthesizedAnnotation synthesizedAnnotation = new TestSynthesizedAnnotation(synthesizedAnnotationAggregator, annotation); SynthesizedAnnotation synthesizedAnnotation = new TestSynthesizedAnnotation(synthesizedAnnotationAggregator, annotation);
annotationMap.put(annotation.annotationType(), synthesizedAnnotation); annotationMap.put(annotation.annotationType(), synthesizedAnnotation);
@ -43,7 +43,7 @@ public class AliasLinkAnnotationPostProcessorTest {
AliasLinkAnnotationPostProcessor processor = new AliasLinkAnnotationPostProcessor(); AliasLinkAnnotationPostProcessor processor = new AliasLinkAnnotationPostProcessor();
Map<Class<?>, SynthesizedAnnotation> annotationMap = new HashMap<>(); Map<Class<?>, SynthesizedAnnotation> annotationMap = new HashMap<>();
SynthesizedAnnotationAggregator synthesizedAnnotationAggregator = new TestSynthesizedAnnotationAggregator(annotationMap); SynthesizedAggregateAnnotation synthesizedAnnotationAggregator = new TestSynthesizedAggregateAnnotation(annotationMap);
AnnotationForTest annotation = ClassForTest.class.getAnnotation(AnnotationForTest.class); AnnotationForTest annotation = ClassForTest.class.getAnnotation(AnnotationForTest.class);
SynthesizedAnnotation synthesizedAnnotation = new TestSynthesizedAnnotation(synthesizedAnnotationAggregator, annotation); SynthesizedAnnotation synthesizedAnnotation = new TestSynthesizedAnnotation(synthesizedAnnotationAggregator, annotation);
annotationMap.put(annotation.annotationType(), synthesizedAnnotation); annotationMap.put(annotation.annotationType(), synthesizedAnnotation);
@ -77,11 +77,11 @@ public class AliasLinkAnnotationPostProcessorTest {
String name2() default ""; String name2() default "";
} }
static class TestSynthesizedAnnotationAggregator implements SynthesizedAnnotationAggregator { static class TestSynthesizedAggregateAnnotation implements SynthesizedAggregateAnnotation {
private final Map<Class<?>, SynthesizedAnnotation> annotationMap; private final Map<Class<?>, SynthesizedAnnotation> annotationMap;
public TestSynthesizedAnnotationAggregator(Map<Class<?>, SynthesizedAnnotation> annotationMap) { public TestSynthesizedAggregateAnnotation(Map<Class<?>, SynthesizedAnnotation> annotationMap) {
this.annotationMap = annotationMap; this.annotationMap = annotationMap;
} }
@ -134,20 +134,15 @@ public class AliasLinkAnnotationPostProcessorTest {
public Object getAttribute(String attributeName, Class<?> attributeType) { public Object getAttribute(String attributeName, Class<?> attributeType) {
return null; return null;
} }
@Override
public Annotation[] getDeclaredAnnotations() {
return new Annotation[0];
}
} }
static class TestSynthesizedAnnotation implements SynthesizedAnnotation { static class TestSynthesizedAnnotation implements SynthesizedAnnotation {
private final Annotation annotation; private final Annotation annotation;
private final SynthesizedAnnotationAggregator owner; private final SynthesizedAggregateAnnotation owner;
private final Map<String, AnnotationAttribute> attributeMap; private final Map<String, AnnotationAttribute> attributeMap;
public TestSynthesizedAnnotation(SynthesizedAnnotationAggregator owner, Annotation annotation) { public TestSynthesizedAnnotation(SynthesizedAggregateAnnotation owner, Annotation annotation) {
this.owner = owner; this.owner = owner;
this.attributeMap = new HashMap<>(); this.attributeMap = new HashMap<>();
this.annotation = annotation; this.annotation = annotation;
@ -157,7 +152,7 @@ public class AliasLinkAnnotationPostProcessorTest {
} }
@Override @Override
public SynthesizedAnnotationAggregator getOwner() { public SynthesizedAggregateAnnotation getOwner() {
return owner; return owner;
} }

View File

@ -39,7 +39,7 @@ public class CacheableSynthesizedAnnotationAttributeProcessorTest {
} }
@Override @Override
public SynthesizedAnnotationAggregator getOwner() { public SynthesizedAggregateAnnotation getOwner() {
return null; return null;
} }

View File

@ -19,7 +19,7 @@ public class MirrorLinkAnnotationPostProcessorTest {
MirrorLinkAnnotationPostProcessor processor = new MirrorLinkAnnotationPostProcessor(); MirrorLinkAnnotationPostProcessor processor = new MirrorLinkAnnotationPostProcessor();
Map<Class<?>, SynthesizedAnnotation> annotationMap = new HashMap<>(); Map<Class<?>, SynthesizedAnnotation> annotationMap = new HashMap<>();
SynthesizedAnnotationAggregator synthesizedAnnotationAggregator = new TestSynthesizedAnnotationAggregator(annotationMap); SynthesizedAggregateAnnotation synthesizedAnnotationAggregator = new TestSynthesizedAggregateAnnotation(annotationMap);
AnnotationForTest annotation = ClassForTest.class.getAnnotation(AnnotationForTest.class); AnnotationForTest annotation = ClassForTest.class.getAnnotation(AnnotationForTest.class);
SynthesizedAnnotation synthesizedAnnotation = new TestSynthesizedAnnotation(synthesizedAnnotationAggregator, annotation); SynthesizedAnnotation synthesizedAnnotation = new TestSynthesizedAnnotation(synthesizedAnnotationAggregator, annotation);
annotationMap.put(annotation.annotationType(), synthesizedAnnotation); annotationMap.put(annotation.annotationType(), synthesizedAnnotation);
@ -51,11 +51,11 @@ public class MirrorLinkAnnotationPostProcessorTest {
String name() default ""; String name() default "";
} }
static class TestSynthesizedAnnotationAggregator implements SynthesizedAnnotationAggregator { static class TestSynthesizedAggregateAnnotation implements SynthesizedAggregateAnnotation {
private final Map<Class<?>, SynthesizedAnnotation> annotationMap; private final Map<Class<?>, SynthesizedAnnotation> annotationMap;
public TestSynthesizedAnnotationAggregator(Map<Class<?>, SynthesizedAnnotation> annotationMap) { public TestSynthesizedAggregateAnnotation(Map<Class<?>, SynthesizedAnnotation> annotationMap) {
this.annotationMap = annotationMap; this.annotationMap = annotationMap;
} }
@ -109,19 +109,15 @@ public class MirrorLinkAnnotationPostProcessorTest {
return null; return null;
} }
@Override
public Annotation[] getDeclaredAnnotations() {
return new Annotation[0];
}
} }
static class TestSynthesizedAnnotation implements SynthesizedAnnotation { static class TestSynthesizedAnnotation implements SynthesizedAnnotation {
private final Annotation annotation; private final Annotation annotation;
private final SynthesizedAnnotationAggregator owner; private final SynthesizedAggregateAnnotation owner;
private final Map<String, AnnotationAttribute> attributeMap; private final Map<String, AnnotationAttribute> attributeMap;
public TestSynthesizedAnnotation(SynthesizedAnnotationAggregator owner, Annotation annotation) { public TestSynthesizedAnnotation(SynthesizedAggregateAnnotation owner, Annotation annotation) {
this.owner = owner; this.owner = owner;
this.attributeMap = new HashMap<>(); this.attributeMap = new HashMap<>();
this.annotation = annotation; this.annotation = annotation;
@ -131,7 +127,7 @@ public class MirrorLinkAnnotationPostProcessorTest {
} }
@Override @Override
public SynthesizedAnnotationAggregator getOwner() { public SynthesizedAggregateAnnotation getOwner() {
return owner; return owner;
} }

View File

@ -88,7 +88,7 @@ public class SynthesizedAnnotationSelectorTest {
} }
@Override @Override
public SynthesizedAnnotationAggregator getOwner() { public SynthesizedAggregateAnnotation getOwner() {
return null; return null;
} }

View File

@ -4,12 +4,15 @@ import cn.hutool.core.util.ReflectUtil;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import java.lang.annotation.*; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Arrays; import java.util.Arrays;
/** /**
* 合成注解{@link SynthesizedMetaAnnotationAggregator}的测试用例 * 合成注解{@link SynthesizedMetaAggregateAnnotation}的测试用例
* *
* @author huangchengxing * @author huangchengxing
*/ */
@ -22,10 +25,10 @@ public class SyntheticMetaAnnotationTest {
final GrandParentAnnotation grandParentAnnotation = ChildAnnotation.class.getAnnotation(GrandParentAnnotation.class); final GrandParentAnnotation grandParentAnnotation = ChildAnnotation.class.getAnnotation(GrandParentAnnotation.class);
final ParentAnnotation parentAnnotation = ChildAnnotation.class.getAnnotation(ParentAnnotation.class); final ParentAnnotation parentAnnotation = ChildAnnotation.class.getAnnotation(ParentAnnotation.class);
final ChildAnnotation childAnnotation = AnnotatedClass.class.getAnnotation(ChildAnnotation.class); final ChildAnnotation childAnnotation = AnnotatedClass.class.getAnnotation(ChildAnnotation.class);
final SynthesizedMetaAnnotationAggregator syntheticMetaAnnotation = new SynthesizedMetaAnnotationAggregator(childAnnotation); final SynthesizedMetaAggregateAnnotation syntheticMetaAnnotation = new SynthesizedMetaAggregateAnnotation(childAnnotation);
// Annotation & AnnotatedElement // Annotation & AnnotatedElement
Assert.assertEquals(SynthesizedMetaAnnotationAggregator.class, syntheticMetaAnnotation.annotationType()); Assert.assertEquals(SynthesizedMetaAggregateAnnotation.class, syntheticMetaAnnotation.annotationType());
Assert.assertTrue(syntheticMetaAnnotation.isAnnotationPresent(GrandParentAnnotation.class)); Assert.assertTrue(syntheticMetaAnnotation.isAnnotationPresent(GrandParentAnnotation.class));
Assert.assertTrue(syntheticMetaAnnotation.isAnnotationPresent(ParentAnnotation.class)); Assert.assertTrue(syntheticMetaAnnotation.isAnnotationPresent(ParentAnnotation.class));
Assert.assertTrue(syntheticMetaAnnotation.isAnnotationPresent(ChildAnnotation.class)); Assert.assertTrue(syntheticMetaAnnotation.isAnnotationPresent(ChildAnnotation.class));
@ -36,7 +39,6 @@ public class SyntheticMetaAnnotationTest {
Arrays.asList(childAnnotation, grandParentAnnotation, parentAnnotation), Arrays.asList(childAnnotation, grandParentAnnotation, parentAnnotation),
Arrays.asList(syntheticMetaAnnotation.getAnnotations()) Arrays.asList(syntheticMetaAnnotation.getAnnotations())
); );
Assert.assertArrayEquals(new Annotation[]{ childAnnotation }, syntheticMetaAnnotation.getDeclaredAnnotations());
// 扩展方法 // 扩展方法
Assert.assertNotNull(syntheticMetaAnnotation.getSynthesizedAnnotation(GrandParentAnnotation.class)); Assert.assertNotNull(syntheticMetaAnnotation.getSynthesizedAnnotation(GrandParentAnnotation.class));
@ -53,11 +55,9 @@ public class SyntheticMetaAnnotationTest {
@Test @Test
public void synthesisAnnotationAttributeTest() { public void synthesisAnnotationAttributeTest() {
final ChildAnnotation rootAnnotation = AnnotatedClass.class.getAnnotation(ChildAnnotation.class); final ChildAnnotation rootAnnotation = AnnotatedClass.class.getAnnotation(ChildAnnotation.class);
SynthesizedMetaAnnotationAggregator syntheticMetaAnnotation = new SynthesizedMetaAnnotationAggregator(rootAnnotation); SynthesizedMetaAggregateAnnotation syntheticMetaAnnotation = new SynthesizedMetaAggregateAnnotation(rootAnnotation);
Assert.assertEquals(syntheticMetaAnnotation.getSource(), rootAnnotation); Assert.assertEquals(syntheticMetaAnnotation.getSource(), rootAnnotation);
Assert.assertEquals(syntheticMetaAnnotation.annotationType(), SynthesizedMetaAnnotationAggregator.class); Assert.assertEquals(syntheticMetaAnnotation.annotationType(), SynthesizedMetaAggregateAnnotation.class);
Assert.assertEquals(1, syntheticMetaAnnotation.getDeclaredAnnotations().length);
Assert.assertEquals(syntheticMetaAnnotation.getDeclaredAnnotations()[0], rootAnnotation);
Assert.assertEquals(3, syntheticMetaAnnotation.getAnnotations().length); Assert.assertEquals(3, syntheticMetaAnnotation.getAnnotations().length);
Assert.assertEquals("Child!", syntheticMetaAnnotation.getAttribute("childValue", String.class)); Assert.assertEquals("Child!", syntheticMetaAnnotation.getAttribute("childValue", String.class));
@ -69,7 +69,7 @@ public class SyntheticMetaAnnotationTest {
@Test @Test
public void syntheticAnnotationTest() { public void syntheticAnnotationTest() {
final ChildAnnotation rootAnnotation = AnnotatedClass.class.getAnnotation(ChildAnnotation.class); final ChildAnnotation rootAnnotation = AnnotatedClass.class.getAnnotation(ChildAnnotation.class);
SynthesizedMetaAnnotationAggregator syntheticMetaAnnotation = new SynthesizedMetaAnnotationAggregator(rootAnnotation); SynthesizedMetaAggregateAnnotation syntheticMetaAnnotation = new SynthesizedMetaAggregateAnnotation(rootAnnotation);
final ChildAnnotation childAnnotation = syntheticMetaAnnotation.synthesize(ChildAnnotation.class); final ChildAnnotation childAnnotation = syntheticMetaAnnotation.synthesize(ChildAnnotation.class);
SynthesizedAnnotation childSyntheticAnnotation = syntheticMetaAnnotation.getSynthesizedAnnotation(ChildAnnotation.class); SynthesizedAnnotation childSyntheticAnnotation = syntheticMetaAnnotation.getSynthesizedAnnotation(ChildAnnotation.class);
@ -82,7 +82,7 @@ public class SyntheticMetaAnnotationTest {
Assert.assertEquals("Child!", childAnnotation.childValue()); Assert.assertEquals("Child!", childAnnotation.childValue());
Assert.assertEquals("Child!", childAnnotation.childValueAlias()); Assert.assertEquals("Child!", childAnnotation.childValueAlias());
Assert.assertEquals(childAnnotation.grandParentType(), Integer.class); Assert.assertEquals(childAnnotation.grandParentType(), Integer.class);
Assert.assertThrows(IllegalArgumentException.class, () -> new SynthesizedMetaAnnotationAggregator(childAnnotation)); Assert.assertThrows(IllegalArgumentException.class, () -> new SynthesizedMetaAggregateAnnotation(childAnnotation));
final ParentAnnotation parentAnnotation = syntheticMetaAnnotation.synthesize(ParentAnnotation.class); final ParentAnnotation parentAnnotation = syntheticMetaAnnotation.synthesize(ParentAnnotation.class);
SynthesizedAnnotation parentSyntheticAnnotation = syntheticMetaAnnotation.getSynthesizedAnnotation(ParentAnnotation.class); SynthesizedAnnotation parentSyntheticAnnotation = syntheticMetaAnnotation.getSynthesizedAnnotation(ParentAnnotation.class);
@ -93,7 +93,7 @@ public class SyntheticMetaAnnotationTest {
Assert.assertNotNull(parentAnnotation); Assert.assertNotNull(parentAnnotation);
Assert.assertEquals("Child's Parent!", parentAnnotation.parentValue()); Assert.assertEquals("Child's Parent!", parentAnnotation.parentValue());
Assert.assertEquals("java.lang.Void", parentAnnotation.grandParentType()); Assert.assertEquals("java.lang.Void", parentAnnotation.grandParentType());
Assert.assertThrows(IllegalArgumentException.class, () -> new SynthesizedMetaAnnotationAggregator(parentAnnotation)); Assert.assertThrows(IllegalArgumentException.class, () -> new SynthesizedMetaAggregateAnnotation(parentAnnotation));
final GrandParentAnnotation grandParentAnnotation = syntheticMetaAnnotation.synthesize(GrandParentAnnotation.class); final GrandParentAnnotation grandParentAnnotation = syntheticMetaAnnotation.synthesize(GrandParentAnnotation.class);
SynthesizedAnnotation grandParentSyntheticAnnotation = syntheticMetaAnnotation.getSynthesizedAnnotation(GrandParentAnnotation.class); SynthesizedAnnotation grandParentSyntheticAnnotation = syntheticMetaAnnotation.getSynthesizedAnnotation(GrandParentAnnotation.class);
@ -105,13 +105,13 @@ public class SyntheticMetaAnnotationTest {
Assert.assertNotNull(grandParentAnnotation); Assert.assertNotNull(grandParentAnnotation);
Assert.assertEquals("Child's GrandParent!", grandParentAnnotation.grandParentValue()); Assert.assertEquals("Child's GrandParent!", grandParentAnnotation.grandParentValue());
Assert.assertEquals(grandParentAnnotation.grandParentType(), Integer.class); Assert.assertEquals(grandParentAnnotation.grandParentType(), Integer.class);
Assert.assertThrows(IllegalArgumentException.class, () -> new SynthesizedMetaAnnotationAggregator(grandParentAnnotation)); Assert.assertThrows(IllegalArgumentException.class, () -> new SynthesizedMetaAggregateAnnotation(grandParentAnnotation));
} }
@Test @Test
public void linkTest() { public void linkTest() {
final Method method = ReflectUtil.getMethod(AnnotationForLinkTest.class, "value"); final Method method = ReflectUtil.getMethod(AnnotationForLinkTest.class, "value");
final SynthesizedAnnotationAggregator synthesizedAnnotationAggregator = new SynthesizedMetaAnnotationAggregator(method.getAnnotation(AliasFor.class)); final SynthesizedAggregateAnnotation synthesizedAnnotationAggregator = new SynthesizedMetaAggregateAnnotation(method.getAnnotation(AliasFor.class));
final Link link = synthesizedAnnotationAggregator.synthesize(Link.class); final Link link = synthesizedAnnotationAggregator.synthesize(Link.class);
Assert.assertEquals(AnnotationForLinkTest.class, link.annotation()); Assert.assertEquals(AnnotationForLinkTest.class, link.annotation());
Assert.assertEquals("name", link.attribute()); Assert.assertEquals("name", link.attribute());
@ -120,19 +120,19 @@ public class SyntheticMetaAnnotationTest {
@Test @Test
public void mirrorAttributeTest() { public void mirrorAttributeTest() {
AnnotationForMirrorTest annotation = ClassForMirrorTest.class.getAnnotation(AnnotationForMirrorTest.class); AnnotationForMirrorTest annotation = ClassForMirrorTest.class.getAnnotation(AnnotationForMirrorTest.class);
SynthesizedAnnotationAggregator synthetic = new SynthesizedMetaAnnotationAggregator(annotation); SynthesizedAggregateAnnotation synthetic = new SynthesizedMetaAggregateAnnotation(annotation);
AnnotationForMirrorTest syntheticAnnotation = synthetic.synthesize(AnnotationForMirrorTest.class); AnnotationForMirrorTest syntheticAnnotation = synthetic.synthesize(AnnotationForMirrorTest.class);
Assert.assertEquals("Foo", syntheticAnnotation.name()); Assert.assertEquals("Foo", syntheticAnnotation.name());
Assert.assertEquals("Foo", syntheticAnnotation.value()); Assert.assertEquals("Foo", syntheticAnnotation.value());
annotation = ClassForMirrorTest2.class.getAnnotation(AnnotationForMirrorTest.class); annotation = ClassForMirrorTest2.class.getAnnotation(AnnotationForMirrorTest.class);
synthetic = new SynthesizedMetaAnnotationAggregator(annotation); synthetic = new SynthesizedMetaAggregateAnnotation(annotation);
syntheticAnnotation = synthetic.synthesize(AnnotationForMirrorTest.class); syntheticAnnotation = synthetic.synthesize(AnnotationForMirrorTest.class);
Assert.assertEquals("Foo", syntheticAnnotation.name()); Assert.assertEquals("Foo", syntheticAnnotation.name());
Assert.assertEquals("Foo", syntheticAnnotation.value()); Assert.assertEquals("Foo", syntheticAnnotation.value());
annotation = ClassForMirrorTest3.class.getAnnotation(AnnotationForMirrorTest.class); annotation = ClassForMirrorTest3.class.getAnnotation(AnnotationForMirrorTest.class);
synthetic = new SynthesizedMetaAnnotationAggregator(annotation); synthetic = new SynthesizedMetaAggregateAnnotation(annotation);
syntheticAnnotation = synthetic.synthesize(AnnotationForMirrorTest.class); syntheticAnnotation = synthetic.synthesize(AnnotationForMirrorTest.class);
AnnotationForMirrorTest finalSyntheticAnnotation = syntheticAnnotation; AnnotationForMirrorTest finalSyntheticAnnotation = syntheticAnnotation;
Assert.assertThrows(IllegalArgumentException.class, finalSyntheticAnnotation::name); Assert.assertThrows(IllegalArgumentException.class, finalSyntheticAnnotation::name);
@ -141,14 +141,14 @@ public class SyntheticMetaAnnotationTest {
@Test @Test
public void aliasForTest() { public void aliasForTest() {
AnnotationForAliasForTest annotation = ClassForAliasForTest.class.getAnnotation(AnnotationForAliasForTest.class); AnnotationForAliasForTest annotation = ClassForAliasForTest.class.getAnnotation(AnnotationForAliasForTest.class);
SynthesizedAnnotationAggregator synthetic = new SynthesizedMetaAnnotationAggregator(annotation); SynthesizedAggregateAnnotation synthetic = new SynthesizedMetaAggregateAnnotation(annotation);
MetaAnnotationForAliasForTest metaAnnotation = synthetic.synthesize(MetaAnnotationForAliasForTest.class); MetaAnnotationForAliasForTest metaAnnotation = synthetic.synthesize(MetaAnnotationForAliasForTest.class);
Assert.assertEquals("Meta", metaAnnotation.name()); Assert.assertEquals("Meta", metaAnnotation.name());
AnnotationForAliasForTest childAnnotation = synthetic.synthesize(AnnotationForAliasForTest.class); AnnotationForAliasForTest childAnnotation = synthetic.synthesize(AnnotationForAliasForTest.class);
Assert.assertEquals("", childAnnotation.value()); Assert.assertEquals("", childAnnotation.value());
annotation = ClassForAliasForTest2.class.getAnnotation(AnnotationForAliasForTest.class); annotation = ClassForAliasForTest2.class.getAnnotation(AnnotationForAliasForTest.class);
synthetic = new SynthesizedMetaAnnotationAggregator(annotation); synthetic = new SynthesizedMetaAggregateAnnotation(annotation);
metaAnnotation = synthetic.synthesize(MetaAnnotationForAliasForTest.class); metaAnnotation = synthetic.synthesize(MetaAnnotationForAliasForTest.class);
Assert.assertEquals("Foo", metaAnnotation.name()); Assert.assertEquals("Foo", metaAnnotation.name());
childAnnotation = synthetic.synthesize(AnnotationForAliasForTest.class); childAnnotation = synthetic.synthesize(AnnotationForAliasForTest.class);
@ -158,14 +158,14 @@ public class SyntheticMetaAnnotationTest {
@Test @Test
public void forceAliasForTest() { public void forceAliasForTest() {
AnnotationForceForAliasForTest annotation = ClassForForceAliasForTest.class.getAnnotation(AnnotationForceForAliasForTest.class); AnnotationForceForAliasForTest annotation = ClassForForceAliasForTest.class.getAnnotation(AnnotationForceForAliasForTest.class);
SynthesizedAnnotationAggregator synthetic = new SynthesizedMetaAnnotationAggregator(annotation); SynthesizedAggregateAnnotation synthetic = new SynthesizedMetaAggregateAnnotation(annotation);
MetaAnnotationForForceAliasForTest metaAnnotation = synthetic.synthesize(MetaAnnotationForForceAliasForTest.class); MetaAnnotationForForceAliasForTest metaAnnotation = synthetic.synthesize(MetaAnnotationForForceAliasForTest.class);
Assert.assertEquals("", metaAnnotation.name()); Assert.assertEquals("", metaAnnotation.name());
AnnotationForceForAliasForTest childAnnotation = synthetic.synthesize(AnnotationForceForAliasForTest.class); AnnotationForceForAliasForTest childAnnotation = synthetic.synthesize(AnnotationForceForAliasForTest.class);
Assert.assertEquals("", childAnnotation.value()); Assert.assertEquals("", childAnnotation.value());
annotation = ClassForForceAliasForTest2.class.getAnnotation(AnnotationForceForAliasForTest.class); annotation = ClassForForceAliasForTest2.class.getAnnotation(AnnotationForceForAliasForTest.class);
synthetic = new SynthesizedMetaAnnotationAggregator(annotation); synthetic = new SynthesizedMetaAggregateAnnotation(annotation);
metaAnnotation = synthetic.synthesize(MetaAnnotationForForceAliasForTest.class); metaAnnotation = synthetic.synthesize(MetaAnnotationForForceAliasForTest.class);
Assert.assertEquals("Foo", metaAnnotation.name()); Assert.assertEquals("Foo", metaAnnotation.name());
childAnnotation = synthetic.synthesize(AnnotationForceForAliasForTest.class); childAnnotation = synthetic.synthesize(AnnotationForceForAliasForTest.class);
@ -175,7 +175,7 @@ public class SyntheticMetaAnnotationTest {
@Test @Test
public void aliasForAndMirrorTest() { public void aliasForAndMirrorTest() {
AnnotationForMirrorThenAliasForTest annotation = ClassForAliasForAndMirrorTest.class.getAnnotation(AnnotationForMirrorThenAliasForTest.class); AnnotationForMirrorThenAliasForTest annotation = ClassForAliasForAndMirrorTest.class.getAnnotation(AnnotationForMirrorThenAliasForTest.class);
SynthesizedAnnotationAggregator synthetic = new SynthesizedMetaAnnotationAggregator(annotation); SynthesizedAggregateAnnotation synthetic = new SynthesizedMetaAggregateAnnotation(annotation);
MetaAnnotationForMirrorThenAliasForTest metaAnnotation = synthetic.synthesize(MetaAnnotationForMirrorThenAliasForTest.class); MetaAnnotationForMirrorThenAliasForTest metaAnnotation = synthetic.synthesize(MetaAnnotationForMirrorThenAliasForTest.class);
Assert.assertEquals("test", metaAnnotation.name()); Assert.assertEquals("test", metaAnnotation.name());
Assert.assertEquals("test", metaAnnotation.value()); Assert.assertEquals("test", metaAnnotation.value());
@ -186,7 +186,7 @@ public class SyntheticMetaAnnotationTest {
@Test @Test
public void multiAliasForTest() { public void multiAliasForTest() {
final AnnotationForMultiAliasForTest annotation = ClassForMultiAliasForTest.class.getAnnotation(AnnotationForMultiAliasForTest.class); final AnnotationForMultiAliasForTest annotation = ClassForMultiAliasForTest.class.getAnnotation(AnnotationForMultiAliasForTest.class);
final SynthesizedAnnotationAggregator synthetic = new SynthesizedMetaAnnotationAggregator(annotation); final SynthesizedAggregateAnnotation synthetic = new SynthesizedMetaAggregateAnnotation(annotation);
final MetaAnnotationForMultiAliasForTest1 metaAnnotation1 = synthetic.synthesize(MetaAnnotationForMultiAliasForTest1.class); final MetaAnnotationForMultiAliasForTest1 metaAnnotation1 = synthetic.synthesize(MetaAnnotationForMultiAliasForTest1.class);
Assert.assertEquals("test", metaAnnotation1.name()); Assert.assertEquals("test", metaAnnotation1.name());
@ -200,7 +200,7 @@ public class SyntheticMetaAnnotationTest {
@Test @Test
public void implicitAliasTest() { public void implicitAliasTest() {
final AnnotationForImplicitAliasTest annotation = ClassForImplicitAliasTest.class.getAnnotation(AnnotationForImplicitAliasTest.class); final AnnotationForImplicitAliasTest annotation = ClassForImplicitAliasTest.class.getAnnotation(AnnotationForImplicitAliasTest.class);
final SynthesizedAnnotationAggregator synthetic = new SynthesizedMetaAnnotationAggregator(annotation); final SynthesizedAggregateAnnotation synthetic = new SynthesizedMetaAggregateAnnotation(annotation);
final MetaAnnotationForImplicitAliasTest metaAnnotation = synthetic.synthesize(MetaAnnotationForImplicitAliasTest.class); final MetaAnnotationForImplicitAliasTest metaAnnotation = synthetic.synthesize(MetaAnnotationForImplicitAliasTest.class);
Assert.assertEquals("Meta", metaAnnotation.name()); Assert.assertEquals("Meta", metaAnnotation.name());