Browse Source

Support nested annotations in ASM-based processing again

Spring Framework 5.0 introduced a regression in ASM-based annotation
processing. Specifically, nested annotations were no longer supported,
and component scanning resulted in an exception if a candidate
component was annotated with an annotation that contained nested
annotations.

This commit fixes this regression by introducing special handling in
AnnotationTypeMapping that supports extracting values from objects of
type TypeMappedAnnotation when necessary.

Closes gh-24375
pull/24512/head
Sam Brannen 6 years ago
parent
commit
974cacac31
  1. 4
      spring-context/src/test/java/example/gh24375/AnnotatedComponent.java
  2. 11
      spring-context/src/test/java/example/gh24375/EnclosingAnnotation.java
  3. 3
      spring-context/src/test/java/example/gh24375/NestedAnnotation.java
  4. 10
      spring-context/src/test/java/org/springframework/context/annotation/ClassPathScanningCandidateComponentProviderTests.java
  5. 14
      spring-core/src/main/java/org/springframework/core/annotation/AnnotationTypeMapping.java
  6. 2
      spring-core/src/main/java/org/springframework/core/annotation/TypeMappedAnnotation.java
  7. 21
      spring-core/src/test/java/example/type/AnnotatedComponent.java
  8. 33
      spring-core/src/test/java/example/type/EnclosingAnnotation.java
  9. 29
      spring-core/src/test/java/example/type/NestedAnnotation.java
  10. 20
      spring-core/src/test/java/org/springframework/core/annotation/AnnotationTypeMappingsTests.java
  11. 11
      spring-core/src/test/java/org/springframework/core/type/AbstractMethodMetadataTests.java

4
spring-context/src/test/java/example/gh24375/MyComponent.java → spring-context/src/test/java/example/gh24375/AnnotatedComponent.java

@ -19,6 +19,6 @@ package example.gh24375; @@ -19,6 +19,6 @@ package example.gh24375;
import org.springframework.stereotype.Component;
@Component
@A(other = @B)
public class MyComponent {
@EnclosingAnnotation(nested2 = @NestedAnnotation)
public class AnnotatedComponent {
}

11
spring-context/src/test/java/example/gh24375/A.java → spring-context/src/test/java/example/gh24375/EnclosingAnnotation.java

@ -25,11 +25,12 @@ import org.springframework.core.annotation.AliasFor; @@ -25,11 +25,12 @@ import org.springframework.core.annotation.AliasFor;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface A {
public @interface EnclosingAnnotation {
@AliasFor("value")
B other() default @B;
@AliasFor("nested2")
NestedAnnotation nested1() default @NestedAnnotation;
@AliasFor("nested1")
NestedAnnotation nested2() default @NestedAnnotation;
@AliasFor("other")
B value() default @B;
}

3
spring-context/src/test/java/example/gh24375/B.java → spring-context/src/test/java/example/gh24375/NestedAnnotation.java

@ -23,7 +23,8 @@ import java.lang.annotation.Target; @@ -23,7 +23,8 @@ import java.lang.annotation.Target;
@Target(ElementType.ANNOTATION_TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface B {
public @interface NestedAnnotation {
String name() default "";
}

10
spring-context/src/test/java/org/springframework/context/annotation/ClassPathScanningCandidateComponentProviderTests.java

@ -21,7 +21,7 @@ import java.lang.annotation.RetentionPolicy; @@ -21,7 +21,7 @@ import java.lang.annotation.RetentionPolicy;
import java.util.Set;
import java.util.regex.Pattern;
import example.gh24375.MyComponent;
import example.gh24375.AnnotatedComponent;
import example.profilescan.DevComponent;
import example.profilescan.ProfileAnnotatedComponent;
import example.profilescan.ProfileMetaAnnotatedComponent;
@ -39,7 +39,6 @@ import example.scannable.ServiceInvocationCounter; @@ -39,7 +39,6 @@ import example.scannable.ServiceInvocationCounter;
import example.scannable.StubFooDao;
import example.scannable.sub.BarComponent;
import org.aspectj.lang.annotation.Aspect;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition;
@ -503,12 +502,11 @@ public class ClassPathScanningCandidateComponentProviderTests { @@ -503,12 +502,11 @@ public class ClassPathScanningCandidateComponentProviderTests {
}
@Test
@Disabled("Disabled until gh-24375 is resolved")
public void gh24375() {
public void componentScanningFindsComponentsAnnotatedWithAnnotationsContainingNestedAnnotations() {
ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true);
Set<BeanDefinition> components = provider.findCandidateComponents(MyComponent.class.getPackage().getName());
Set<BeanDefinition> components = provider.findCandidateComponents(AnnotatedComponent.class.getPackage().getName());
assertThat(components).hasSize(1);
assertThat(components.iterator().next().getBeanClassName()).isEqualTo(MyComponent.class.getName());
assertThat(components.iterator().next().getBeanClassName()).isEqualTo(AnnotatedComponent.class.getName());
}

14
spring-core/src/main/java/org/springframework/core/annotation/AnnotationTypeMapping.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -47,7 +47,6 @@ import org.springframework.util.StringUtils; @@ -47,7 +47,6 @@ import org.springframework.util.StringUtils;
*/
final class AnnotationTypeMapping {
private static final MirrorSet[] EMPTY_MIRROR_SETS = new MirrorSet[0];
@ -534,8 +533,15 @@ final class AnnotationTypeMapping { @@ -534,8 +533,15 @@ final class AnnotationTypeMapping {
AttributeMethods attributes = AttributeMethods.forAnnotationType(annotation.annotationType());
for (int i = 0; i < attributes.size(); i++) {
Method attribute = attributes.get(i);
if (!areEquivalent(ReflectionUtils.invokeMethod(attribute, annotation),
valueExtractor.apply(attribute, extractedValue), valueExtractor)) {
Object value1 = ReflectionUtils.invokeMethod(attribute, annotation);
Object value2;
if (extractedValue instanceof TypeMappedAnnotation) {
value2 = ((TypeMappedAnnotation<?>) extractedValue).getValue(attribute.getName()).orElse(null);
}
else {
value2 = valueExtractor.apply(attribute, extractedValue);
}
if (!areEquivalent(value1, value2, valueExtractor)) {
return false;
}
}

2
spring-core/src/main/java/org/springframework/core/annotation/TypeMappedAnnotation.java

@ -685,7 +685,7 @@ final class TypeMappedAnnotation<A extends Annotation> extends AbstractMergedAnn @@ -685,7 +685,7 @@ final class TypeMappedAnnotation<A extends Annotation> extends AbstractMergedAnn
@SuppressWarnings("unchecked")
@Nullable
private static Object extractFromMap(Method attribute, @Nullable Object map) {
static Object extractFromMap(Method attribute, @Nullable Object map) {
return (map != null ? ((Map<String, ?>) map).get(attribute.getName()) : null);
}

21
spring-core/src/test/java/example/type/AnnotatedComponent.java

@ -0,0 +1,21 @@ @@ -0,0 +1,21 @@
/*
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.type;
@EnclosingAnnotation(nested2 = @NestedAnnotation)
public class AnnotatedComponent {
}

33
spring-core/src/test/java/example/type/EnclosingAnnotation.java

@ -0,0 +1,33 @@ @@ -0,0 +1,33 @@
/*
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.type;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import org.springframework.core.annotation.AliasFor;
@Retention(RetentionPolicy.RUNTIME)
public @interface EnclosingAnnotation {
@AliasFor("nested2")
NestedAnnotation nested1() default @NestedAnnotation;
@AliasFor("nested1")
NestedAnnotation nested2() default @NestedAnnotation;
}

29
spring-core/src/test/java/example/type/NestedAnnotation.java

@ -0,0 +1,29 @@ @@ -0,0 +1,29 @@
/*
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.type;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.ANNOTATION_TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface NestedAnnotation {
String name() default "";
}

20
spring-core/src/test/java/org/springframework/core/annotation/AnnotationTypeMappingsTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -45,6 +45,7 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType; @@ -45,6 +45,7 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
* Tests for {@link AnnotationTypeMappings} and {@link AnnotationTypeMapping}.
*
* @author Phillip Webb
* @author Sam Brannen
*/
class AnnotationTypeMappingsTests {
@ -440,10 +441,18 @@ class AnnotationTypeMappingsTests { @@ -440,10 +441,18 @@ class AnnotationTypeMappingsTests {
}
@Test
void isEquivalentToDefaultValueWhenNestedAnnotationAndExtractedValuesMatchReturnsTrue() {
void isEquivalentToDefaultValueWhenNestedAnnotationAndExtractedValuesMatchReturnsTrueAndValueSuppliedAsMap() {
AnnotationTypeMapping mapping = AnnotationTypeMappings.forAnnotationType(NestedValue.class).get(0);
Map<String, Object> value = Collections.singletonMap("value", "java.io.InputStream");
assertThat(mapping.isEquivalentToDefaultValue(0, value, this::extractFromMap)).isTrue();
assertThat(mapping.isEquivalentToDefaultValue(0, value, TypeMappedAnnotation::extractFromMap)).isTrue();
}
@Test // gh-24375
void isEquivalentToDefaultValueWhenNestedAnnotationAndExtractedValuesMatchReturnsTrueAndValueSuppliedAsTypeMappedAnnotation() {
AnnotationTypeMapping mapping = AnnotationTypeMappings.forAnnotationType(NestedValue.class).get(0);
Map<String, String> attributes = Collections.singletonMap("value", "java.io.InputStream");
MergedAnnotation<ClassValue> value = TypeMappedAnnotation.of(getClass().getClassLoader(), null, ClassValue.class, attributes);
assertThat(mapping.isEquivalentToDefaultValue(0, value, TypeMappedAnnotation::extractFromMap)).isTrue();
}
@Test
@ -504,11 +513,6 @@ class AnnotationTypeMappingsTests { @@ -504,11 +513,6 @@ class AnnotationTypeMappingsTests {
return names;
}
@SuppressWarnings("unchecked")
private Object extractFromMap(Method attribute, Object map) {
return map != null ? ((Map<String, ?>) map).get(attribute.getName()) : null;
}
@Retention(RetentionPolicy.RUNTIME)
@interface SimpleAnnotation {

11
spring-core/src/test/java/org/springframework/core/type/AbstractMethodMetadataTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -19,6 +19,8 @@ package org.springframework.core.type; @@ -19,6 +19,8 @@ package org.springframework.core.type;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import example.type.AnnotatedComponent;
import example.type.EnclosingAnnotation;
import org.junit.jupiter.api.Test;
import org.springframework.core.annotation.MergedAnnotation;
@ -31,6 +33,7 @@ import static org.assertj.core.api.Assertions.entry; @@ -31,6 +33,7 @@ import static org.assertj.core.api.Assertions.entry;
* Base class for {@link MethodMetadata} tests.
*
* @author Phillip Webb
* @author Sam Brannen
*/
public abstract class AbstractMethodMetadataTests {
@ -138,6 +141,12 @@ public abstract class AbstractMethodMetadataTests { @@ -138,6 +141,12 @@ public abstract class AbstractMethodMetadataTests {
assertThat(attributes.get("size")).containsExactlyInAnyOrder(1, 2);
}
@Test // gh-24375
public void metadataLoadsForNestedAnnotations() {
AnnotationMetadata annotationMetadata = get(AnnotatedComponent.class);
assertThat(annotationMetadata.getAnnotationTypes()).containsExactly(EnclosingAnnotation.class.getName());
}
protected MethodMetadata getTagged(Class<?> source) {
return get(source, Tag.class.getName());
}

Loading…
Cancel
Save