Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Feb 7, 2020
1 parent 02e90a8 commit 9bc9bdd
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
import org.springframework.stereotype.Component;

@Component
@A(other = @B)
public class MyComponent {
@EnclosingAnnotation(nested2 = @NestedAnnotation)
public class AnnotatedComponent {
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@

@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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@

@Target(ElementType.ANNOTATION_TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface B {
public @interface NestedAnnotation {

String name() default "";

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
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;
Expand All @@ -39,7 +39,6 @@
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;
Expand Down Expand Up @@ -503,12 +502,11 @@ public void testIntegrationWithAnnotationConfigApplicationContext_metaProfile()
}

@Test
@Disabled("Disabled until gh-24375 is resolved")
public void gh24375() {
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());
}


Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -534,8 +534,15 @@ private static boolean areEquivalent(Annotation annotation, @Nullable Object ext
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;
}
}
Expand Down
21 changes: 21 additions & 0 deletions spring-core/src/test/java/example/type/AnnotatedComponent.java
Original file line number Diff line number Diff line change
@@ -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 changes: 33 additions & 0 deletions spring-core/src/test/java/example/type/EnclosingAnnotation.java
Original file line number Diff line number Diff line change
@@ -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 changes: 29 additions & 0 deletions spring-core/src/test/java/example/type/NestedAnnotation.java
Original file line number Diff line number Diff line change
@@ -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 "";
}
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -19,6 +19,8 @@
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;
Expand All @@ -31,6 +33,7 @@
* Base class for {@link MethodMetadata} tests.
*
* @author Phillip Webb
* @author Sam Brannen
*/
public abstract class AbstractMethodMetadataTests {

Expand Down Expand Up @@ -138,6 +141,12 @@ public void getAllAnnotationAttributesReturnsAllAttributes() {
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());
}
Expand Down

0 comments on commit 9bc9bdd

Please sign in to comment.