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
This commit is contained in:
@@ -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 {
|
||||
}
|
||||
@@ -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
Normal file
29
spring-core/src/test/java/example/type/NestedAnnotation.java
Normal file
@@ -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 "";
|
||||
}
|
||||
@@ -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;
|
||||
* Tests for {@link AnnotationTypeMappings} and {@link AnnotationTypeMapping}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
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 {
|
||||
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 {
|
||||
|
||||
@@ -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;
|
||||
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;
|
||||
* Base class for {@link MethodMetadata} tests.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
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());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user