提交 06b1f31c 编写于 作者: J Juergen Hoeller

SynthesizedMergedAnnotationInvocationHandler does not pre-load values

Closes gh-24029
上级 4af60393
......@@ -61,9 +61,6 @@ final class SynthesizedMergedAnnotationInvocationHandler<A extends Annotation> i
this.annotation = annotation;
this.type = type;
this.attributes = AttributeMethods.forAnnotationType(type);
for (int i = 0; i < this.attributes.size(); i++) {
getAttributeValue(this.attributes.get(i));
}
}
......
......@@ -28,6 +28,7 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import javax.annotation.Nonnull;
......@@ -46,7 +47,7 @@ import static java.util.Arrays.stream;
import static java.util.stream.Collectors.toList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.springframework.core.annotation.AnnotationUtils.VALUE;
import static org.springframework.core.annotation.AnnotationUtils.findAnnotation;
import static org.springframework.core.annotation.AnnotationUtils.findAnnotationDeclaringClass;
......@@ -685,7 +686,8 @@ class AnnotationUtilsTests {
assertThat(values).isEqualTo(expectedValuesJava);
// Spring
Set<MyRepeatable> set = getDeclaredRepeatableAnnotations(MyRepeatableClass.class, MyRepeatable.class, MyRepeatableContainer.class);
Set<MyRepeatable> set = getDeclaredRepeatableAnnotations(
MyRepeatableClass.class, MyRepeatable.class, MyRepeatableContainer.class);
assertThat(set).isNotNull();
values = set.stream().map(MyRepeatable::value).collect(toList());
assertThat(values).isEqualTo(expectedValuesSpring);
......@@ -720,7 +722,8 @@ class AnnotationUtilsTests {
@Test
void synthesizeAnnotationWithImplicitAliasesWithMissingDefaultValues() throws Exception {
Class<?> clazz = ImplicitAliasesWithMissingDefaultValuesContextConfigClass.class;
Class<ImplicitAliasesWithMissingDefaultValuesContextConfig> annotationType = ImplicitAliasesWithMissingDefaultValuesContextConfig.class;
Class<ImplicitAliasesWithMissingDefaultValuesContextConfig> annotationType =
ImplicitAliasesWithMissingDefaultValuesContextConfig.class;
ImplicitAliasesWithMissingDefaultValuesContextConfig config = clazz.getAnnotation(annotationType);
assertThat(config).isNotNull();
......@@ -735,7 +738,8 @@ class AnnotationUtilsTests {
@Test
void synthesizeAnnotationWithImplicitAliasesWithDifferentDefaultValues() throws Exception {
Class<?> clazz = ImplicitAliasesWithDifferentDefaultValuesContextConfigClass.class;
Class<ImplicitAliasesWithDifferentDefaultValuesContextConfig> annotationType = ImplicitAliasesWithDifferentDefaultValuesContextConfig.class;
Class<ImplicitAliasesWithDifferentDefaultValuesContextConfig> annotationType =
ImplicitAliasesWithDifferentDefaultValuesContextConfig.class;
ImplicitAliasesWithDifferentDefaultValuesContextConfig config = clazz.getAnnotation(annotationType);
assertThat(config).isNotNull();
assertThatExceptionOfType(AnnotationConfigurationException.class).isThrownBy(() ->
......@@ -749,7 +753,8 @@ class AnnotationUtilsTests {
@Test
void synthesizeAnnotationWithImplicitAliasesWithDuplicateValues() throws Exception {
Class<?> clazz = ImplicitAliasesWithDuplicateValuesContextConfigClass.class;
Class<ImplicitAliasesWithDuplicateValuesContextConfig> annotationType = ImplicitAliasesWithDuplicateValuesContextConfig.class;
Class<ImplicitAliasesWithDuplicateValuesContextConfig> annotationType =
ImplicitAliasesWithDuplicateValuesContextConfig.class;
ImplicitAliasesWithDuplicateValuesContextConfig config = clazz.getAnnotation(annotationType);
assertThat(config).isNotNull();
......@@ -780,7 +785,8 @@ class AnnotationUtilsTests {
@Test
@SuppressWarnings("unchecked")
void synthesizeAnnotationFromMapWithNestedMap() throws Exception {
ComponentScanSingleFilter componentScan = ComponentScanSingleFilterClass.class.getAnnotation(ComponentScanSingleFilter.class);
ComponentScanSingleFilter componentScan =
ComponentScanSingleFilterClass.class.getAnnotation(ComponentScanSingleFilter.class);
assertThat(componentScan).isNotNull();
assertThat(componentScan.value().pattern()).as("value from ComponentScan: ").isEqualTo("*Foo");
......@@ -827,7 +833,8 @@ class AnnotationUtilsTests {
filters[1].put("pattern", "newBar");
filters[1].put("enigma", 42);
ComponentScan synthesizedComponentScan = synthesizeAnnotation(attributes, ComponentScan.class, ComponentScanClass.class);
ComponentScan synthesizedComponentScan =
synthesizeAnnotation(attributes, ComponentScan.class, ComponentScanClass.class);
assertThat(synthesizedComponentScan).isNotNull();
assertThat(synthesizedComponentScan).isNotSameAs(componentScan);
......@@ -911,16 +918,16 @@ class AnnotationUtilsTests {
}
private void assertMissingTextAttribute(Map<String, Object> attributes) {
assertThatIllegalArgumentException().isThrownBy(() ->
synthesizeAnnotation(attributes, AnnotationWithoutDefaults.class, null))
assertThatExceptionOfType(NoSuchElementException.class).isThrownBy(() ->
synthesizeAnnotation(attributes, AnnotationWithoutDefaults.class, null).text())
.withMessageContaining("No value found for attribute named 'text' in merged annotation");
}
@Test
void synthesizeAnnotationFromMapWithAttributeOfIncorrectType() throws Exception {
Map<String, Object> map = Collections.singletonMap(VALUE, 42L);
assertThatIllegalArgumentException().isThrownBy(() ->
synthesizeAnnotation(map, Component.class, null))
assertThatIllegalStateException().isThrownBy(() ->
synthesizeAnnotation(map, Component.class, null).value())
.withMessageContaining("Attribute 'value' in annotation org.springframework.stereotype.Component "
+ "should be compatible with java.lang.String but a java.lang.Long value was returned");
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册