mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
调整方法、变量与类名,完善测试用例
This commit is contained in:
@@ -8,7 +8,7 @@ import org.junit.Test;
|
||||
import java.lang.annotation.*;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class AbstractAnnotationAttributeWrapperTest {
|
||||
public class AbstractWrappedAnnotationAttributeTest {
|
||||
|
||||
@Test
|
||||
public void workTest() {
|
||||
@@ -17,7 +17,9 @@ public class AbstractAnnotationAttributeWrapperTest {
|
||||
CacheableAnnotationAttribute valueAttribute = new CacheableAnnotationAttribute(annotation, valueMethod);
|
||||
Method nameMethod = ReflectUtil.getMethod(AnnotationForTest1.class, "name1");
|
||||
CacheableAnnotationAttribute nameAttribute = new CacheableAnnotationAttribute(annotation, nameMethod);
|
||||
TestAnnotationAttributeWrapper nameWrapper = new TestAnnotationAttributeWrapper(nameAttribute, valueAttribute);
|
||||
AbstractWrappedAnnotationAttribute nameWrapper = new TestWrappedAnnotationAttribute(nameAttribute, valueAttribute);
|
||||
|
||||
Assert.assertEquals(nameWrapper.getAnnotation(), annotation);
|
||||
|
||||
// 注解属性
|
||||
Assert.assertEquals(annotation, nameWrapper.getAnnotation());
|
||||
@@ -40,7 +42,7 @@ public class AbstractAnnotationAttributeWrapperTest {
|
||||
CacheableAnnotationAttribute value1Attribute = new CacheableAnnotationAttribute(annotation1, value1Method);
|
||||
Method name1Method = ReflectUtil.getMethod(AnnotationForTest1.class, "name1");
|
||||
CacheableAnnotationAttribute name1Attribute = new CacheableAnnotationAttribute(annotation1, name1Method);
|
||||
TestAnnotationAttributeWrapper wrapper1 = new TestAnnotationAttributeWrapper(name1Attribute, value1Attribute);
|
||||
AbstractWrappedAnnotationAttribute wrapper1 = new TestWrappedAnnotationAttribute(name1Attribute, value1Attribute);
|
||||
Assert.assertEquals(name1Attribute, wrapper1.getNonWrappedOriginal());
|
||||
Assert.assertEquals(CollUtil.newArrayList(name1Attribute, value1Attribute), wrapper1.getAllLinkedNonWrappedAttributes());
|
||||
|
||||
@@ -48,7 +50,7 @@ public class AbstractAnnotationAttributeWrapperTest {
|
||||
Annotation annotation2 = ClassForTest1.class.getAnnotation(AnnotationForTest2.class);
|
||||
Method value2Method = ReflectUtil.getMethod(AnnotationForTest2.class, "value2");
|
||||
CacheableAnnotationAttribute value2Attribute = new CacheableAnnotationAttribute(annotation2, value2Method);
|
||||
TestAnnotationAttributeWrapper wrapper2 = new TestAnnotationAttributeWrapper(wrapper1, value2Attribute);
|
||||
AbstractWrappedAnnotationAttribute wrapper2 = new TestWrappedAnnotationAttribute(wrapper1, value2Attribute);
|
||||
Assert.assertEquals(name1Attribute, wrapper2.getNonWrappedOriginal());
|
||||
Assert.assertEquals(CollUtil.newArrayList(name1Attribute, value1Attribute, value2Attribute), wrapper2.getAllLinkedNonWrappedAttributes());
|
||||
|
||||
@@ -56,20 +58,25 @@ public class AbstractAnnotationAttributeWrapperTest {
|
||||
Annotation annotation3 = ClassForTest1.class.getAnnotation(AnnotationForTest3.class);
|
||||
Method value3Method = ReflectUtil.getMethod(AnnotationForTest3.class, "value3");
|
||||
CacheableAnnotationAttribute value3Attribute = new CacheableAnnotationAttribute(annotation3, value3Method);
|
||||
TestAnnotationAttributeWrapper wrapper3 = new TestAnnotationAttributeWrapper(value3Attribute, wrapper2);
|
||||
AbstractWrappedAnnotationAttribute wrapper3 = new TestWrappedAnnotationAttribute(value3Attribute, wrapper2);
|
||||
Assert.assertEquals(value3Attribute, wrapper3.getNonWrappedOriginal());
|
||||
Assert.assertEquals(CollUtil.newArrayList(value3Attribute, name1Attribute, value1Attribute, value2Attribute), wrapper3.getAllLinkedNonWrappedAttributes());
|
||||
|
||||
}
|
||||
|
||||
static class TestAnnotationAttributeWrapper extends AbstractAnnotationAttributeWrapper {
|
||||
protected TestAnnotationAttributeWrapper(AnnotationAttribute original, AnnotationAttribute linked) {
|
||||
static class TestWrappedAnnotationAttribute extends AbstractWrappedAnnotationAttribute {
|
||||
protected TestWrappedAnnotationAttribute(AnnotationAttribute original, AnnotationAttribute linked) {
|
||||
super(original, linked);
|
||||
}
|
||||
@Override
|
||||
public Object getValue() {
|
||||
return linked.getValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValueEquivalentToDefaultValue() {
|
||||
return getOriginal().isValueEquivalentToDefaultValue() && getLinked().isValueEquivalentToDefaultValue();
|
||||
}
|
||||
}
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
@@ -0,0 +1,190 @@
|
||||
package cn.hutool.core.annotation;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.UnaryOperator;
|
||||
|
||||
public class AliasAttributePostProcessorTest {
|
||||
|
||||
@Test
|
||||
public void processTest() {
|
||||
AliasAttributePostProcessor processor = new AliasAttributePostProcessor();
|
||||
|
||||
Map<Class<?>, SynthesizedAnnotation> annotationMap = new HashMap<>();
|
||||
SynthesizedAnnotationAggregator synthesizedAnnotationAggregator = new TestSynthesizedAnnotationAggregator(annotationMap);
|
||||
AnnotationForTest annotation = ClassForTest.class.getAnnotation(AnnotationForTest.class);
|
||||
SynthesizedAnnotation synthesizedAnnotation = new TestSynthesizedAnnotation(synthesizedAnnotationAggregator, annotation);
|
||||
annotationMap.put(annotation.annotationType(), synthesizedAnnotation);
|
||||
|
||||
processor.process(synthesizedAnnotation, synthesizedAnnotationAggregator);
|
||||
AnnotationAttribute valueAttribute = synthesizedAnnotation.getAttributes().get("value");
|
||||
Assert.assertEquals(ReflectUtil.getMethod(AnnotationForTest.class, "value"), valueAttribute.getAttribute());
|
||||
Assert.assertTrue(valueAttribute.isWrapped());
|
||||
Assert.assertEquals(ForceAliasedAnnotationAttribute.class, valueAttribute.getClass());
|
||||
|
||||
AnnotationAttribute nameAttribute = synthesizedAnnotation.getAttributes().get("name");
|
||||
Assert.assertEquals(ReflectUtil.getMethod(AnnotationForTest.class, "name"), nameAttribute.getAttribute());
|
||||
Assert.assertFalse(nameAttribute.isWrapped());
|
||||
Assert.assertEquals(CacheableAnnotationAttribute.class, nameAttribute.getClass());
|
||||
|
||||
Assert.assertEquals(nameAttribute, ((WrappedAnnotationAttribute)valueAttribute).getLinked());
|
||||
}
|
||||
|
||||
@AnnotationForTest
|
||||
static class ClassForTest {}
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ ElementType.METHOD, ElementType.TYPE })
|
||||
@interface AnnotationForTest {
|
||||
@Alias("name")
|
||||
String value() default "";
|
||||
String name() default "";
|
||||
}
|
||||
|
||||
static class TestSynthesizedAnnotationAggregator implements SynthesizedAnnotationAggregator {
|
||||
|
||||
private final Map<Class<?>, SynthesizedAnnotation> annotationMap;
|
||||
|
||||
public TestSynthesizedAnnotationAggregator(Map<Class<?>, SynthesizedAnnotation> annotationMap) {
|
||||
this.annotationMap = annotationMap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SynthesizedAnnotationSelector getAnnotationSelector() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SynthesizedAnnotationAttributeProcessor getAnnotationAttributeProcessor() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<SynthesizedAnnotationPostProcessor> getAnnotationAttributePostProcessors() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SynthesizedAnnotation getSynthesizedAnnotation(Class<?> annotationType) {
|
||||
return annotationMap.get(annotationType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<SynthesizedAnnotation> getAllSynthesizedAnnotation() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends Annotation> T getAnnotation(Class<T> annotationType) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Annotation[] getAnnotations() {
|
||||
return new Annotation[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends Annotation> T synthesize(Class<T> annotationType) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getAttribute(String attributeName, Class<?> attributeType) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Annotation[] getDeclaredAnnotations() {
|
||||
return new Annotation[0];
|
||||
}
|
||||
}
|
||||
|
||||
static class TestSynthesizedAnnotation implements SynthesizedAnnotation {
|
||||
|
||||
private final Annotation annotation;
|
||||
private final SynthesizedAnnotationAggregator owner;
|
||||
private final Map<String, AnnotationAttribute> attributeMap;
|
||||
|
||||
public TestSynthesizedAnnotation(SynthesizedAnnotationAggregator owner, Annotation annotation) {
|
||||
this.owner = owner;
|
||||
this.attributeMap = new HashMap<>();
|
||||
this.annotation = annotation;
|
||||
for (Method declaredMethod : annotation.annotationType().getDeclaredMethods()) {
|
||||
attributeMap.put(declaredMethod.getName(), new CacheableAnnotationAttribute(annotation, declaredMethod));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public SynthesizedAnnotationAggregator getOwner() {
|
||||
return owner;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getRoot() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Annotation getAnnotation() {
|
||||
return annotation;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getVerticalDistance() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHorizontalDistance() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasAttribute(String attributeName, Class<?> returnType) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, AnnotationAttribute> getAttributes() {
|
||||
return attributeMap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAttribute(String attributeName, AnnotationAttribute attribute) {
|
||||
attributeMap.put(attributeName, attribute);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void replaceAttribute(String attributeName, UnaryOperator<AnnotationAttribute> operator) {
|
||||
AnnotationAttribute annotationAttribute = attributeMap.get(attributeName);
|
||||
if (ObjectUtil.isNotNull(annotationAttribute)) {
|
||||
attributeMap.put(attributeName, operator.apply(annotationAttribute));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getAttributeValue(String attributeName) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<? extends Annotation> annotationType() {
|
||||
return annotation.annotationType();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,218 @@
|
||||
package cn.hutool.core.annotation;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.UnaryOperator;
|
||||
|
||||
public class AliasForLinkAttributePostProcessorTest {
|
||||
|
||||
@Test
|
||||
public void processForceAliasForTest() {
|
||||
AliasForLinkAttributePostProcessor processor = new AliasForLinkAttributePostProcessor();
|
||||
|
||||
Map<Class<?>, SynthesizedAnnotation> annotationMap = new HashMap<>();
|
||||
SynthesizedAnnotationAggregator synthesizedAnnotationAggregator = new TestSynthesizedAnnotationAggregator(annotationMap);
|
||||
AnnotationForTest annotation = ClassForTest.class.getAnnotation(AnnotationForTest.class);
|
||||
SynthesizedAnnotation synthesizedAnnotation = new TestSynthesizedAnnotation(synthesizedAnnotationAggregator, annotation);
|
||||
annotationMap.put(annotation.annotationType(), synthesizedAnnotation);
|
||||
|
||||
processor.process(synthesizedAnnotation, synthesizedAnnotationAggregator);
|
||||
AnnotationAttribute valueAttribute = synthesizedAnnotation.getAttributes().get("value");
|
||||
Assert.assertEquals(ReflectUtil.getMethod(AnnotationForTest.class, "value"), valueAttribute.getAttribute());
|
||||
Assert.assertFalse(valueAttribute.isWrapped());
|
||||
Assert.assertEquals(CacheableAnnotationAttribute.class, valueAttribute.getClass());
|
||||
|
||||
AnnotationAttribute nameAttribute = synthesizedAnnotation.getAttributes().get("name");
|
||||
Assert.assertEquals(ReflectUtil.getMethod(AnnotationForTest.class, "name"), nameAttribute.getAttribute());
|
||||
Assert.assertTrue(nameAttribute.isWrapped());
|
||||
Assert.assertEquals(ForceAliasedAnnotationAttribute.class, nameAttribute.getClass());
|
||||
|
||||
Assert.assertEquals(valueAttribute, ((WrappedAnnotationAttribute)nameAttribute).getLinked());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void processAliasForTest() {
|
||||
AliasForLinkAttributePostProcessor processor = new AliasForLinkAttributePostProcessor();
|
||||
|
||||
Map<Class<?>, SynthesizedAnnotation> annotationMap = new HashMap<>();
|
||||
SynthesizedAnnotationAggregator synthesizedAnnotationAggregator = new TestSynthesizedAnnotationAggregator(annotationMap);
|
||||
AnnotationForTest annotation = ClassForTest.class.getAnnotation(AnnotationForTest.class);
|
||||
SynthesizedAnnotation synthesizedAnnotation = new TestSynthesizedAnnotation(synthesizedAnnotationAggregator, annotation);
|
||||
annotationMap.put(annotation.annotationType(), synthesizedAnnotation);
|
||||
|
||||
processor.process(synthesizedAnnotation, synthesizedAnnotationAggregator);
|
||||
AnnotationAttribute valueAttribute = synthesizedAnnotation.getAttributes().get("value2");
|
||||
Assert.assertEquals(ReflectUtil.getMethod(AnnotationForTest.class, "value2"), valueAttribute.getAttribute());
|
||||
Assert.assertFalse(valueAttribute.isWrapped());
|
||||
Assert.assertEquals(CacheableAnnotationAttribute.class, valueAttribute.getClass());
|
||||
|
||||
AnnotationAttribute nameAttribute = synthesizedAnnotation.getAttributes().get("name2");
|
||||
Assert.assertEquals(ReflectUtil.getMethod(AnnotationForTest.class, "name2"), nameAttribute.getAttribute());
|
||||
Assert.assertTrue(nameAttribute.isWrapped());
|
||||
Assert.assertEquals(AliasedAnnotationAttribute.class, nameAttribute.getClass());
|
||||
|
||||
Assert.assertEquals(valueAttribute, ((WrappedAnnotationAttribute)nameAttribute).getLinked());
|
||||
}
|
||||
|
||||
@AnnotationForTest
|
||||
static class ClassForTest {}
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ ElementType.METHOD, ElementType.TYPE })
|
||||
@interface AnnotationForTest {
|
||||
@Link(attribute = "name", type = RelationType.FORCE_ALIAS_FOR)
|
||||
String value() default "";
|
||||
String name() default "";
|
||||
|
||||
@Link(attribute = "name2", type = RelationType.ALIAS_FOR)
|
||||
String value2() default "";
|
||||
String name2() default "";
|
||||
}
|
||||
|
||||
static class TestSynthesizedAnnotationAggregator implements SynthesizedAnnotationAggregator {
|
||||
|
||||
private final Map<Class<?>, SynthesizedAnnotation> annotationMap;
|
||||
|
||||
public TestSynthesizedAnnotationAggregator(Map<Class<?>, SynthesizedAnnotation> annotationMap) {
|
||||
this.annotationMap = annotationMap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SynthesizedAnnotationSelector getAnnotationSelector() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SynthesizedAnnotationAttributeProcessor getAnnotationAttributeProcessor() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<SynthesizedAnnotationPostProcessor> getAnnotationAttributePostProcessors() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SynthesizedAnnotation getSynthesizedAnnotation(Class<?> annotationType) {
|
||||
return annotationMap.get(annotationType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<SynthesizedAnnotation> getAllSynthesizedAnnotation() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends Annotation> T getAnnotation(Class<T> annotationType) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Annotation[] getAnnotations() {
|
||||
return new Annotation[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends Annotation> T synthesize(Class<T> annotationType) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getAttribute(String attributeName, Class<?> attributeType) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Annotation[] getDeclaredAnnotations() {
|
||||
return new Annotation[0];
|
||||
}
|
||||
}
|
||||
|
||||
static class TestSynthesizedAnnotation implements SynthesizedAnnotation {
|
||||
|
||||
private final Annotation annotation;
|
||||
private final SynthesizedAnnotationAggregator owner;
|
||||
private final Map<String, AnnotationAttribute> attributeMap;
|
||||
|
||||
public TestSynthesizedAnnotation(SynthesizedAnnotationAggregator owner, Annotation annotation) {
|
||||
this.owner = owner;
|
||||
this.attributeMap = new HashMap<>();
|
||||
this.annotation = annotation;
|
||||
for (Method declaredMethod : annotation.annotationType().getDeclaredMethods()) {
|
||||
attributeMap.put(declaredMethod.getName(), new CacheableAnnotationAttribute(annotation, declaredMethod));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public SynthesizedAnnotationAggregator getOwner() {
|
||||
return owner;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getRoot() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Annotation getAnnotation() {
|
||||
return annotation;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getVerticalDistance() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHorizontalDistance() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasAttribute(String attributeName, Class<?> returnType) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, AnnotationAttribute> getAttributes() {
|
||||
return attributeMap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAttribute(String attributeName, AnnotationAttribute attribute) {
|
||||
attributeMap.put(attributeName, attribute);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void replaceAttribute(String attributeName, UnaryOperator<AnnotationAttribute> operator) {
|
||||
AnnotationAttribute annotationAttribute = attributeMap.get(attributeName);
|
||||
if (ObjectUtil.isNotNull(annotationAttribute)) {
|
||||
attributeMap.put(attributeName, operator.apply(annotationAttribute));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getAttributeValue(String attributeName) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<? extends Annotation> annotationType() {
|
||||
return annotation.annotationType();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -17,15 +17,16 @@ public class AliasedAnnotationAttributeTest {
|
||||
final CacheableAnnotationAttribute valueAttribute = new CacheableAnnotationAttribute(annotation, valueMethod);
|
||||
final Method nameMethod = ReflectUtil.getMethod(AnnotationForTest.class, "name");
|
||||
final CacheableAnnotationAttribute nameAttribute = new CacheableAnnotationAttribute(annotation, nameMethod);
|
||||
final AliasedAnnotationAttribute annotationAttribute = new AliasedAnnotationAttribute(valueAttribute, nameAttribute);
|
||||
final AliasedAnnotationAttribute valueAnnotationAttribute = new AliasedAnnotationAttribute(valueAttribute, nameAttribute);
|
||||
|
||||
// 注解属性
|
||||
Assert.assertEquals(annotation, annotationAttribute.getAnnotation());
|
||||
Assert.assertEquals(annotation.annotationType(), annotationAttribute.getAnnotationType());
|
||||
Assert.assertEquals(annotation, valueAnnotationAttribute.getAnnotation());
|
||||
Assert.assertEquals(annotation.annotationType(), valueAnnotationAttribute.getAnnotationType());
|
||||
|
||||
// 方法属性
|
||||
Assert.assertEquals(valueMethod.getName(), annotationAttribute.getAttributeName());
|
||||
Assert.assertEquals(nameMethod.getReturnType(), annotationAttribute.getAttributeType());
|
||||
Assert.assertEquals(valueMethod.getAnnotation(Alias.class), valueAnnotationAttribute.getAnnotation(Alias.class));
|
||||
Assert.assertEquals(valueMethod.getName(), valueAnnotationAttribute.getAttributeName());
|
||||
Assert.assertEquals(nameMethod.getReturnType(), valueAnnotationAttribute.getAttributeType());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -63,6 +64,7 @@ public class AliasedAnnotationAttributeTest {
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ ElementType.METHOD, ElementType.TYPE })
|
||||
@interface AnnotationForTest {
|
||||
@Alias("value")
|
||||
String value() default "";
|
||||
String name() default "";
|
||||
}
|
||||
|
@@ -0,0 +1,100 @@
|
||||
package cn.hutool.core.annotation;
|
||||
|
||||
import cn.hutool.core.lang.Opt;
|
||||
import cn.hutool.core.map.MapBuilder;
|
||||
import cn.hutool.core.util.ClassUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.function.UnaryOperator;
|
||||
|
||||
public class CacheableSynthesizedAnnotationAttributeProcessorTest {
|
||||
|
||||
@Test
|
||||
public void getAttributeValueTest() {
|
||||
CacheableSynthesizedAnnotationAttributeProcessor processor = new CacheableSynthesizedAnnotationAttributeProcessor();
|
||||
|
||||
Map<String, Object> values1 = MapBuilder.<String, Object> create().put("name", "name1").put("value", 111).build();
|
||||
SynthesizedAnnotation annotation1 = new TestSynthesizedAnnotation(1, 0, values1);
|
||||
Map<String, Object> values2 = MapBuilder.<String, Object> create().put("name", "name2").put("value", "value2").build();
|
||||
SynthesizedAnnotation annotation2 = new TestSynthesizedAnnotation(0, 0, values2);
|
||||
|
||||
Assert.assertEquals("name2", processor.getAttributeValue("name", String.class, Arrays.asList(annotation1, annotation2)));
|
||||
Assert.assertEquals(Integer.valueOf(111), processor.getAttributeValue("value", Integer.class, Arrays.asList(annotation1, annotation2)));
|
||||
}
|
||||
|
||||
static class TestSynthesizedAnnotation implements SynthesizedAnnotation {
|
||||
|
||||
private final int verticalDistance;
|
||||
private final int horizontalDistance;
|
||||
private final Map<String, Object> value;
|
||||
|
||||
public TestSynthesizedAnnotation(int verticalDistance, int horizontalDistance, Map<String, Object> value) {
|
||||
this.verticalDistance = verticalDistance;
|
||||
this.horizontalDistance = horizontalDistance;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SynthesizedAnnotationAggregator getOwner() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getRoot() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Annotation getAnnotation() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getVerticalDistance() {
|
||||
return verticalDistance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHorizontalDistance() {
|
||||
return horizontalDistance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasAttribute(String attributeName, Class<?> returnType) {
|
||||
return Opt.ofNullable(value.get(attributeName))
|
||||
.map(t -> ClassUtil.isAssignable(returnType, t.getClass()))
|
||||
.orElse(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, AnnotationAttribute> getAttributes() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAttribute(String attributeName, AnnotationAttribute attribute) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void replaceAttribute(String attributeName, UnaryOperator<AnnotationAttribute> operator) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getAttributeValue(String attributeName) {
|
||||
return value.get(attributeName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<? extends Annotation> annotationType() {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,192 @@
|
||||
package cn.hutool.core.annotation;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.UnaryOperator;
|
||||
|
||||
public class MirrorLinkAttributePostProcessorTest {
|
||||
|
||||
@Test
|
||||
public void processTest() {
|
||||
MirrorLinkAttributePostProcessor processor = new MirrorLinkAttributePostProcessor();
|
||||
|
||||
Map<Class<?>, SynthesizedAnnotation> annotationMap = new HashMap<>();
|
||||
SynthesizedAnnotationAggregator synthesizedAnnotationAggregator = new TestSynthesizedAnnotationAggregator(annotationMap);
|
||||
AnnotationForTest annotation = ClassForTest.class.getAnnotation(AnnotationForTest.class);
|
||||
SynthesizedAnnotation synthesizedAnnotation = new TestSynthesizedAnnotation(synthesizedAnnotationAggregator, annotation);
|
||||
annotationMap.put(annotation.annotationType(), synthesizedAnnotation);
|
||||
|
||||
processor.process(synthesizedAnnotation, synthesizedAnnotationAggregator);
|
||||
AnnotationAttribute valueAttribute = synthesizedAnnotation.getAttributes().get("value");
|
||||
Assert.assertEquals(ReflectUtil.getMethod(AnnotationForTest.class, "value"), valueAttribute.getAttribute());
|
||||
Assert.assertTrue(valueAttribute.isWrapped());
|
||||
Assert.assertEquals(MirroredAnnotationAttribute.class, valueAttribute.getClass());
|
||||
|
||||
AnnotationAttribute nameAttribute = synthesizedAnnotation.getAttributes().get("name");
|
||||
Assert.assertEquals(ReflectUtil.getMethod(AnnotationForTest.class, "name"), nameAttribute.getAttribute());
|
||||
Assert.assertTrue(nameAttribute.isWrapped());
|
||||
Assert.assertEquals(MirroredAnnotationAttribute.class, nameAttribute.getClass());
|
||||
|
||||
Assert.assertEquals(((WrappedAnnotationAttribute)nameAttribute).getLinked(), ((WrappedAnnotationAttribute)valueAttribute).getOriginal());
|
||||
Assert.assertEquals(((WrappedAnnotationAttribute)nameAttribute).getOriginal(), ((WrappedAnnotationAttribute)valueAttribute).getLinked());
|
||||
}
|
||||
|
||||
@AnnotationForTest
|
||||
static class ClassForTest {}
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ ElementType.METHOD, ElementType.TYPE })
|
||||
@interface AnnotationForTest {
|
||||
@Link(attribute = "name", type = RelationType.MIRROR_FOR)
|
||||
String value() default "";
|
||||
@Link(attribute = "value", type = RelationType.MIRROR_FOR)
|
||||
String name() default "";
|
||||
}
|
||||
|
||||
static class TestSynthesizedAnnotationAggregator implements SynthesizedAnnotationAggregator {
|
||||
|
||||
private final Map<Class<?>, SynthesizedAnnotation> annotationMap;
|
||||
|
||||
public TestSynthesizedAnnotationAggregator(Map<Class<?>, SynthesizedAnnotation> annotationMap) {
|
||||
this.annotationMap = annotationMap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SynthesizedAnnotationSelector getAnnotationSelector() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SynthesizedAnnotationAttributeProcessor getAnnotationAttributeProcessor() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<SynthesizedAnnotationPostProcessor> getAnnotationAttributePostProcessors() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SynthesizedAnnotation getSynthesizedAnnotation(Class<?> annotationType) {
|
||||
return annotationMap.get(annotationType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<SynthesizedAnnotation> getAllSynthesizedAnnotation() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends Annotation> T getAnnotation(Class<T> annotationType) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Annotation[] getAnnotations() {
|
||||
return new Annotation[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends Annotation> T synthesize(Class<T> annotationType) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getAttribute(String attributeName, Class<?> attributeType) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Annotation[] getDeclaredAnnotations() {
|
||||
return new Annotation[0];
|
||||
}
|
||||
}
|
||||
|
||||
static class TestSynthesizedAnnotation implements SynthesizedAnnotation {
|
||||
|
||||
private final Annotation annotation;
|
||||
private final SynthesizedAnnotationAggregator owner;
|
||||
private final Map<String, AnnotationAttribute> attributeMap;
|
||||
|
||||
public TestSynthesizedAnnotation(SynthesizedAnnotationAggregator owner, Annotation annotation) {
|
||||
this.owner = owner;
|
||||
this.attributeMap = new HashMap<>();
|
||||
this.annotation = annotation;
|
||||
for (Method declaredMethod : annotation.annotationType().getDeclaredMethods()) {
|
||||
attributeMap.put(declaredMethod.getName(), new CacheableAnnotationAttribute(annotation, declaredMethod));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public SynthesizedAnnotationAggregator getOwner() {
|
||||
return owner;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getRoot() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Annotation getAnnotation() {
|
||||
return annotation;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getVerticalDistance() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHorizontalDistance() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasAttribute(String attributeName, Class<?> returnType) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, AnnotationAttribute> getAttributes() {
|
||||
return attributeMap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAttribute(String attributeName, AnnotationAttribute attribute) {
|
||||
attributeMap.put(attributeName, attribute);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void replaceAttribute(String attributeName, UnaryOperator<AnnotationAttribute> operator) {
|
||||
AnnotationAttribute annotationAttribute = attributeMap.get(attributeName);
|
||||
if (ObjectUtil.isNotNull(annotationAttribute)) {
|
||||
attributeMap.put(attributeName, operator.apply(annotationAttribute));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getAttributeValue(String attributeName) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<? extends Annotation> annotationType() {
|
||||
return annotation.annotationType();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -10,8 +10,8 @@ import java.util.function.UnaryOperator;
|
||||
public class SynthesizedAnnotationSelectorTest {
|
||||
|
||||
@Test
|
||||
public void nearestAndOldestPriorityTest() {
|
||||
final SynthesizedAnnotationSelector selector = SynthesizedAnnotationSelector.NEAREST_AND_OLDEST_PRIORITY;
|
||||
public void chooseTest() {
|
||||
final SynthesizedAnnotationSelector.NearestAndOldestPrioritySelector selector = (SynthesizedAnnotationSelector.NearestAndOldestPrioritySelector)SynthesizedAnnotationSelector.NEAREST_AND_OLDEST_PRIORITY;
|
||||
|
||||
TestSynthesizedAnnotation annotation1 = new TestSynthesizedAnnotation(0, 0);
|
||||
TestSynthesizedAnnotation annotation2 = new TestSynthesizedAnnotation(0, 0);
|
||||
@@ -28,7 +28,7 @@ public class SynthesizedAnnotationSelectorTest {
|
||||
|
||||
@Test
|
||||
public void nearestAndNewestPriorityTest() {
|
||||
final SynthesizedAnnotationSelector selector = SynthesizedAnnotationSelector.NEAREST_AND_NEWEST_PRIORITY;
|
||||
final SynthesizedAnnotationSelector.NearestAndNewestPrioritySelector selector = (SynthesizedAnnotationSelector.NearestAndNewestPrioritySelector)SynthesizedAnnotationSelector.NEAREST_AND_NEWEST_PRIORITY;
|
||||
|
||||
TestSynthesizedAnnotation annotation1 = new TestSynthesizedAnnotation(0, 0);
|
||||
TestSynthesizedAnnotation annotation2 = new TestSynthesizedAnnotation(0, 0);
|
||||
@@ -45,7 +45,7 @@ public class SynthesizedAnnotationSelectorTest {
|
||||
|
||||
@Test
|
||||
public void farthestAndOldestPriorityTest() {
|
||||
final SynthesizedAnnotationSelector selector = SynthesizedAnnotationSelector.FARTHEST_AND_OLDEST_PRIORITY;
|
||||
final SynthesizedAnnotationSelector.FarthestAndOldestPrioritySelector selector = (SynthesizedAnnotationSelector.FarthestAndOldestPrioritySelector)SynthesizedAnnotationSelector.FARTHEST_AND_OLDEST_PRIORITY;
|
||||
|
||||
TestSynthesizedAnnotation annotation1 = new TestSynthesizedAnnotation(0, 0);
|
||||
TestSynthesizedAnnotation annotation2 = new TestSynthesizedAnnotation(0, 0);
|
||||
@@ -62,7 +62,7 @@ public class SynthesizedAnnotationSelectorTest {
|
||||
|
||||
@Test
|
||||
public void farthestAndNewestPriorityTest() {
|
||||
final SynthesizedAnnotationSelector selector = SynthesizedAnnotationSelector.FARTHEST_AND_NEWEST_PRIORITY;
|
||||
final SynthesizedAnnotationSelector.FarthestAndNewestPrioritySelector selector = (SynthesizedAnnotationSelector.FarthestAndNewestPrioritySelector)SynthesizedAnnotationSelector.FARTHEST_AND_NEWEST_PRIORITY;
|
||||
|
||||
TestSynthesizedAnnotation annotation1 = new TestSynthesizedAnnotation(0, 0);
|
||||
TestSynthesizedAnnotation annotation2 = new TestSynthesizedAnnotation(0, 0);
|
||||
@@ -88,7 +88,7 @@ public class SynthesizedAnnotationSelectorTest {
|
||||
}
|
||||
|
||||
@Override
|
||||
public SyntheticAnnotation getOwner() {
|
||||
public SynthesizedAnnotationAggregator getOwner() {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@@ -9,7 +9,7 @@ import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* 合成注解{@link SyntheticMetaAnnotation}的测试用例
|
||||
* 合成注解{@link SynthesizedMetaAnnotationAggregator}的测试用例
|
||||
*
|
||||
* @author huangchengxing
|
||||
*/
|
||||
@@ -22,10 +22,10 @@ public class SyntheticMetaAnnotationTest {
|
||||
final GrandParentAnnotation grandParentAnnotation = ChildAnnotation.class.getAnnotation(GrandParentAnnotation.class);
|
||||
final ParentAnnotation parentAnnotation = ChildAnnotation.class.getAnnotation(ParentAnnotation.class);
|
||||
final ChildAnnotation childAnnotation = AnnotatedClass.class.getAnnotation(ChildAnnotation.class);
|
||||
final SyntheticMetaAnnotation syntheticMetaAnnotation = new SyntheticMetaAnnotation(childAnnotation);
|
||||
final SynthesizedMetaAnnotationAggregator syntheticMetaAnnotation = new SynthesizedMetaAnnotationAggregator(childAnnotation);
|
||||
|
||||
// Annotation & AnnotatedElement
|
||||
Assert.assertEquals(SyntheticMetaAnnotation.class, syntheticMetaAnnotation.annotationType());
|
||||
Assert.assertEquals(SynthesizedMetaAnnotationAggregator.class, syntheticMetaAnnotation.annotationType());
|
||||
Assert.assertTrue(syntheticMetaAnnotation.isAnnotationPresent(GrandParentAnnotation.class));
|
||||
Assert.assertTrue(syntheticMetaAnnotation.isAnnotationPresent(ParentAnnotation.class));
|
||||
Assert.assertTrue(syntheticMetaAnnotation.isAnnotationPresent(ChildAnnotation.class));
|
||||
@@ -42,15 +42,20 @@ public class SyntheticMetaAnnotationTest {
|
||||
Assert.assertNotNull(syntheticMetaAnnotation.getSynthesizedAnnotation(GrandParentAnnotation.class));
|
||||
Assert.assertNotNull(syntheticMetaAnnotation.getSynthesizedAnnotation(ParentAnnotation.class));
|
||||
Assert.assertNotNull(syntheticMetaAnnotation.getSynthesizedAnnotation(ChildAnnotation.class));
|
||||
Assert.assertEquals(3, syntheticMetaAnnotation.getAllSyntheticAnnotations().size());
|
||||
Assert.assertEquals(3, syntheticMetaAnnotation.getAllSynthesizedAnnotation().size());
|
||||
|
||||
// 属性
|
||||
Assert.assertEquals(SynthesizedAnnotationSelector.NEAREST_AND_OLDEST_PRIORITY, syntheticMetaAnnotation.getAnnotationSelector());
|
||||
Assert.assertEquals(CacheableSynthesizedAnnotationAttributeProcessor.class, syntheticMetaAnnotation.getAnnotationAttributeProcessor().getClass());
|
||||
Assert.assertEquals(3, syntheticMetaAnnotation.getAnnotationAttributePostProcessors().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void synthesisAnnotationAttributeTest() {
|
||||
final ChildAnnotation rootAnnotation = AnnotatedClass.class.getAnnotation(ChildAnnotation.class);
|
||||
SyntheticMetaAnnotation syntheticMetaAnnotation = new SyntheticMetaAnnotation(rootAnnotation);
|
||||
SynthesizedMetaAnnotationAggregator syntheticMetaAnnotation = new SynthesizedMetaAnnotationAggregator(rootAnnotation);
|
||||
Assert.assertEquals(syntheticMetaAnnotation.getSource(), rootAnnotation);
|
||||
Assert.assertEquals(syntheticMetaAnnotation.annotationType(), SyntheticMetaAnnotation.class);
|
||||
Assert.assertEquals(syntheticMetaAnnotation.annotationType(), SynthesizedMetaAnnotationAggregator.class);
|
||||
Assert.assertEquals(1, syntheticMetaAnnotation.getDeclaredAnnotations().length);
|
||||
Assert.assertEquals(syntheticMetaAnnotation.getDeclaredAnnotations()[0], rootAnnotation);
|
||||
Assert.assertEquals(3, syntheticMetaAnnotation.getAnnotations().length);
|
||||
@@ -59,35 +64,55 @@ public class SyntheticMetaAnnotationTest {
|
||||
Assert.assertEquals("Child!", syntheticMetaAnnotation.getAttribute("childValueAlias", String.class));
|
||||
Assert.assertEquals("Child's Parent!", syntheticMetaAnnotation.getAttribute("parentValue", String.class));
|
||||
Assert.assertEquals("Child's GrandParent!", syntheticMetaAnnotation.getAttribute("grandParentValue", String.class));
|
||||
}
|
||||
|
||||
final ChildAnnotation childAnnotation = syntheticMetaAnnotation.syntheticAnnotation(ChildAnnotation.class);
|
||||
@Test
|
||||
public void syntheticAnnotationTest() {
|
||||
final ChildAnnotation rootAnnotation = AnnotatedClass.class.getAnnotation(ChildAnnotation.class);
|
||||
SynthesizedMetaAnnotationAggregator syntheticMetaAnnotation = new SynthesizedMetaAnnotationAggregator(rootAnnotation);
|
||||
|
||||
final ChildAnnotation childAnnotation = syntheticMetaAnnotation.synthesize(ChildAnnotation.class);
|
||||
SynthesizedAnnotation childSyntheticAnnotation = syntheticMetaAnnotation.getSynthesizedAnnotation(ChildAnnotation.class);
|
||||
Assert.assertNotNull(childSyntheticAnnotation);
|
||||
Assert.assertTrue(childSyntheticAnnotation.hasAttribute("childValue", String.class));
|
||||
Assert.assertEquals(AnnotatedClass.class.getAnnotation(ChildAnnotation.class), childSyntheticAnnotation.getRoot());
|
||||
Assert.assertEquals(AnnotatedClass.class.getAnnotation(ChildAnnotation.class), childSyntheticAnnotation.getAnnotation());
|
||||
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 SynthesizedMetaAnnotationAggregator(childAnnotation));
|
||||
|
||||
final ParentAnnotation parentAnnotation = syntheticMetaAnnotation.syntheticAnnotation(ParentAnnotation.class);
|
||||
Assert.assertTrue(syntheticMetaAnnotation.isAnnotationPresent(ParentAnnotation.class));
|
||||
final ParentAnnotation parentAnnotation = syntheticMetaAnnotation.synthesize(ParentAnnotation.class);
|
||||
SynthesizedAnnotation parentSyntheticAnnotation = syntheticMetaAnnotation.getSynthesizedAnnotation(ParentAnnotation.class);
|
||||
Assert.assertNotNull(parentSyntheticAnnotation);
|
||||
Assert.assertTrue(parentSyntheticAnnotation.hasAttribute("parentValue", String.class));
|
||||
Assert.assertEquals(AnnotatedClass.class.getAnnotation(ChildAnnotation.class), parentSyntheticAnnotation.getRoot());
|
||||
Assert.assertEquals(ChildAnnotation.class.getAnnotation(ParentAnnotation.class), parentSyntheticAnnotation.getAnnotation());
|
||||
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 SynthesizedMetaAnnotationAggregator(parentAnnotation));
|
||||
|
||||
final GrandParentAnnotation grandParentAnnotation = syntheticMetaAnnotation.syntheticAnnotation(GrandParentAnnotation.class);
|
||||
final GrandParentAnnotation grandParentAnnotation = syntheticMetaAnnotation.synthesize(GrandParentAnnotation.class);
|
||||
SynthesizedAnnotation grandParentSyntheticAnnotation = syntheticMetaAnnotation.getSynthesizedAnnotation(GrandParentAnnotation.class);
|
||||
Assert.assertNotNull(grandParentSyntheticAnnotation);
|
||||
Assert.assertTrue(grandParentSyntheticAnnotation.hasAttribute("grandParentType", Class.class));
|
||||
Assert.assertEquals(AnnotatedClass.class.getAnnotation(ChildAnnotation.class), grandParentSyntheticAnnotation.getRoot());
|
||||
Assert.assertEquals(ChildAnnotation.class.getAnnotation(GrandParentAnnotation.class), grandParentSyntheticAnnotation.getAnnotation());
|
||||
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 SynthesizedMetaAnnotationAggregator(grandParentAnnotation));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void linkTest() {
|
||||
final Method method = ReflectUtil.getMethod(AnnotationForLinkTest.class, "value");
|
||||
final SyntheticAnnotation syntheticAnnotation = new SyntheticMetaAnnotation(method.getAnnotation(AliasFor.class));
|
||||
final Link link = syntheticAnnotation.syntheticAnnotation(Link.class);
|
||||
final SynthesizedAnnotationAggregator synthesizedAnnotationAggregator = new SynthesizedMetaAnnotationAggregator(method.getAnnotation(AliasFor.class));
|
||||
final Link link = synthesizedAnnotationAggregator.synthesize(Link.class);
|
||||
Assert.assertEquals(AnnotationForLinkTest.class, link.annotation());
|
||||
Assert.assertEquals("name", link.attribute());
|
||||
}
|
||||
@@ -95,20 +120,20 @@ public class SyntheticMetaAnnotationTest {
|
||||
@Test
|
||||
public void mirrorAttributeTest() {
|
||||
AnnotationForMirrorTest annotation = ClassForMirrorTest.class.getAnnotation(AnnotationForMirrorTest.class);
|
||||
SyntheticAnnotation synthetic = new SyntheticMetaAnnotation(annotation);
|
||||
AnnotationForMirrorTest syntheticAnnotation = synthetic.syntheticAnnotation(AnnotationForMirrorTest.class);
|
||||
SynthesizedAnnotationAggregator synthetic = new SynthesizedMetaAnnotationAggregator(annotation);
|
||||
AnnotationForMirrorTest syntheticAnnotation = synthetic.synthesize(AnnotationForMirrorTest.class);
|
||||
Assert.assertEquals("Foo", syntheticAnnotation.name());
|
||||
Assert.assertEquals("Foo", syntheticAnnotation.value());
|
||||
|
||||
annotation = ClassForMirrorTest2.class.getAnnotation(AnnotationForMirrorTest.class);
|
||||
synthetic = new SyntheticMetaAnnotation(annotation);
|
||||
syntheticAnnotation = synthetic.syntheticAnnotation(AnnotationForMirrorTest.class);
|
||||
synthetic = new SynthesizedMetaAnnotationAggregator(annotation);
|
||||
syntheticAnnotation = synthetic.synthesize(AnnotationForMirrorTest.class);
|
||||
Assert.assertEquals("Foo", syntheticAnnotation.name());
|
||||
Assert.assertEquals("Foo", syntheticAnnotation.value());
|
||||
|
||||
annotation = ClassForMirrorTest3.class.getAnnotation(AnnotationForMirrorTest.class);
|
||||
synthetic = new SyntheticMetaAnnotation(annotation);
|
||||
syntheticAnnotation = synthetic.syntheticAnnotation(AnnotationForMirrorTest.class);
|
||||
synthetic = new SynthesizedMetaAnnotationAggregator(annotation);
|
||||
syntheticAnnotation = synthetic.synthesize(AnnotationForMirrorTest.class);
|
||||
AnnotationForMirrorTest finalSyntheticAnnotation = syntheticAnnotation;
|
||||
Assert.assertThrows(IllegalArgumentException.class, finalSyntheticAnnotation::name);
|
||||
}
|
||||
@@ -116,71 +141,71 @@ public class SyntheticMetaAnnotationTest {
|
||||
@Test
|
||||
public void aliasForTest() {
|
||||
AnnotationForAliasForTest annotation = ClassForAliasForTest.class.getAnnotation(AnnotationForAliasForTest.class);
|
||||
SyntheticAnnotation synthetic = new SyntheticMetaAnnotation(annotation);
|
||||
MetaAnnotationForAliasForTest metaAnnotation = synthetic.syntheticAnnotation(MetaAnnotationForAliasForTest.class);
|
||||
SynthesizedAnnotationAggregator synthetic = new SynthesizedMetaAnnotationAggregator(annotation);
|
||||
MetaAnnotationForAliasForTest metaAnnotation = synthetic.synthesize(MetaAnnotationForAliasForTest.class);
|
||||
Assert.assertEquals("Meta", metaAnnotation.name());
|
||||
AnnotationForAliasForTest childAnnotation = synthetic.syntheticAnnotation(AnnotationForAliasForTest.class);
|
||||
AnnotationForAliasForTest childAnnotation = synthetic.synthesize(AnnotationForAliasForTest.class);
|
||||
Assert.assertEquals("", childAnnotation.value());
|
||||
|
||||
annotation = ClassForAliasForTest2.class.getAnnotation(AnnotationForAliasForTest.class);
|
||||
synthetic = new SyntheticMetaAnnotation(annotation);
|
||||
metaAnnotation = synthetic.syntheticAnnotation(MetaAnnotationForAliasForTest.class);
|
||||
synthetic = new SynthesizedMetaAnnotationAggregator(annotation);
|
||||
metaAnnotation = synthetic.synthesize(MetaAnnotationForAliasForTest.class);
|
||||
Assert.assertEquals("Foo", metaAnnotation.name());
|
||||
childAnnotation = synthetic.syntheticAnnotation(AnnotationForAliasForTest.class);
|
||||
childAnnotation = synthetic.synthesize(AnnotationForAliasForTest.class);
|
||||
Assert.assertEquals("Foo", childAnnotation.value());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void forceAliasForTest() {
|
||||
AnnotationForceForAliasForTest annotation = ClassForForceAliasForTest.class.getAnnotation(AnnotationForceForAliasForTest.class);
|
||||
SyntheticAnnotation synthetic = new SyntheticMetaAnnotation(annotation);
|
||||
MetaAnnotationForForceAliasForTest metaAnnotation = synthetic.syntheticAnnotation(MetaAnnotationForForceAliasForTest.class);
|
||||
SynthesizedAnnotationAggregator synthetic = new SynthesizedMetaAnnotationAggregator(annotation);
|
||||
MetaAnnotationForForceAliasForTest metaAnnotation = synthetic.synthesize(MetaAnnotationForForceAliasForTest.class);
|
||||
Assert.assertEquals("", metaAnnotation.name());
|
||||
AnnotationForceForAliasForTest childAnnotation = synthetic.syntheticAnnotation(AnnotationForceForAliasForTest.class);
|
||||
AnnotationForceForAliasForTest childAnnotation = synthetic.synthesize(AnnotationForceForAliasForTest.class);
|
||||
Assert.assertEquals("", childAnnotation.value());
|
||||
|
||||
annotation = ClassForForceAliasForTest2.class.getAnnotation(AnnotationForceForAliasForTest.class);
|
||||
synthetic = new SyntheticMetaAnnotation(annotation);
|
||||
metaAnnotation = synthetic.syntheticAnnotation(MetaAnnotationForForceAliasForTest.class);
|
||||
synthetic = new SynthesizedMetaAnnotationAggregator(annotation);
|
||||
metaAnnotation = synthetic.synthesize(MetaAnnotationForForceAliasForTest.class);
|
||||
Assert.assertEquals("Foo", metaAnnotation.name());
|
||||
childAnnotation = synthetic.syntheticAnnotation(AnnotationForceForAliasForTest.class);
|
||||
childAnnotation = synthetic.synthesize(AnnotationForceForAliasForTest.class);
|
||||
Assert.assertEquals("Foo", childAnnotation.value());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aliasForAndMirrorTest() {
|
||||
AnnotationForMirrorThenAliasForTest annotation = ClassForAliasForAndMirrorTest.class.getAnnotation(AnnotationForMirrorThenAliasForTest.class);
|
||||
SyntheticAnnotation synthetic = new SyntheticMetaAnnotation(annotation);
|
||||
MetaAnnotationForMirrorThenAliasForTest metaAnnotation = synthetic.syntheticAnnotation(MetaAnnotationForMirrorThenAliasForTest.class);
|
||||
SynthesizedAnnotationAggregator synthetic = new SynthesizedMetaAnnotationAggregator(annotation);
|
||||
MetaAnnotationForMirrorThenAliasForTest metaAnnotation = synthetic.synthesize(MetaAnnotationForMirrorThenAliasForTest.class);
|
||||
Assert.assertEquals("test", metaAnnotation.name());
|
||||
Assert.assertEquals("test", metaAnnotation.value());
|
||||
AnnotationForMirrorThenAliasForTest childAnnotation = synthetic.syntheticAnnotation(AnnotationForMirrorThenAliasForTest.class);
|
||||
AnnotationForMirrorThenAliasForTest childAnnotation = synthetic.synthesize(AnnotationForMirrorThenAliasForTest.class);
|
||||
Assert.assertEquals("test", childAnnotation.childValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multiAliasForTest() {
|
||||
final AnnotationForMultiAliasForTest annotation = ClassForMultiAliasForTest.class.getAnnotation(AnnotationForMultiAliasForTest.class);
|
||||
final SyntheticAnnotation synthetic = new SyntheticMetaAnnotation(annotation);
|
||||
final SynthesizedAnnotationAggregator synthetic = new SynthesizedMetaAnnotationAggregator(annotation);
|
||||
|
||||
final MetaAnnotationForMultiAliasForTest1 metaAnnotation1 = synthetic.syntheticAnnotation(MetaAnnotationForMultiAliasForTest1.class);
|
||||
final MetaAnnotationForMultiAliasForTest1 metaAnnotation1 = synthetic.synthesize(MetaAnnotationForMultiAliasForTest1.class);
|
||||
Assert.assertEquals("test", metaAnnotation1.name());
|
||||
Assert.assertEquals("test", metaAnnotation1.value1());
|
||||
final MetaAnnotationForMultiAliasForTest2 metaAnnotation2 = synthetic.syntheticAnnotation(MetaAnnotationForMultiAliasForTest2.class);
|
||||
final MetaAnnotationForMultiAliasForTest2 metaAnnotation2 = synthetic.synthesize(MetaAnnotationForMultiAliasForTest2.class);
|
||||
Assert.assertEquals("test", metaAnnotation2.value2());
|
||||
final AnnotationForMultiAliasForTest childAnnotation = synthetic.syntheticAnnotation(AnnotationForMultiAliasForTest.class);
|
||||
final AnnotationForMultiAliasForTest childAnnotation = synthetic.synthesize(AnnotationForMultiAliasForTest.class);
|
||||
Assert.assertEquals("test", childAnnotation.value3());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void implicitAliasTest() {
|
||||
final AnnotationForImplicitAliasTest annotation = ClassForImplicitAliasTest.class.getAnnotation(AnnotationForImplicitAliasTest.class);
|
||||
final SyntheticAnnotation synthetic = new SyntheticMetaAnnotation(annotation);
|
||||
final SynthesizedAnnotationAggregator synthetic = new SynthesizedMetaAnnotationAggregator(annotation);
|
||||
|
||||
final MetaAnnotationForImplicitAliasTest metaAnnotation = synthetic.syntheticAnnotation(MetaAnnotationForImplicitAliasTest.class);
|
||||
final MetaAnnotationForImplicitAliasTest metaAnnotation = synthetic.synthesize(MetaAnnotationForImplicitAliasTest.class);
|
||||
Assert.assertEquals("Meta", metaAnnotation.name());
|
||||
Assert.assertEquals("Foo", metaAnnotation.value());
|
||||
final AnnotationForImplicitAliasTest childAnnotation = synthetic.syntheticAnnotation(AnnotationForImplicitAliasTest.class);
|
||||
final AnnotationForImplicitAliasTest childAnnotation = synthetic.synthesize(AnnotationForImplicitAliasTest.class);
|
||||
Assert.assertEquals("Foo", childAnnotation.value());
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user