Add TYPE_HIERARCHY_AND_ENCLOSING_CLASSES strategy

Add a `TYPE_HIERARCHY_AND_ENCLOSING_CLASSES` annotation search strategy
that can be used to search the full type hierarchy as well as any
enclosing classes.

Closes gh-23378
This commit is contained in:
Phillip Webb
2019-07-31 14:40:40 +01:00
parent a6021cc968
commit 17518ecbbe
4 changed files with 131 additions and 7 deletions

View File

@@ -0,0 +1,66 @@
/*
* Copyright 2002-2019 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 org.springframework.core.annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* Example class used to test {@link AnnotationsScanner} with enclosing classes.
*
* @author Phillip Webb
* @since 5.2
*/
@AnnotationEnclosingClassSample.EnclosedOne
public class AnnotationEnclosingClassSample {
@EnclosedTwo
public static class EnclosedStatic {
@EnclosedThree
public static class EnclosedStaticStatic {
}
}
@EnclosedTwo
public class EnclosedInner {
@EnclosedThree
public class EnclosedInnerInner {
}
}
@Retention(RetentionPolicy.RUNTIME)
public static @interface EnclosedOne {
}
@Retention(RetentionPolicy.RUNTIME)
public static @interface EnclosedTwo {
}
@Retention(RetentionPolicy.RUNTIME)
public static @interface EnclosedThree {
}
}

View File

@@ -454,6 +454,29 @@ public class AnnotationsScannerTests {
"0:TestAnnotation1");
}
@Test
public void typeHierarchyWithEnclosedStrategyOnEnclosedStaticClassScansAnnotations() {
Class<?> source = AnnotationEnclosingClassSample.EnclosedStatic.EnclosedStaticStatic.class;
assertThat(scan(source, SearchStrategy.TYPE_HIERARCHY_AND_ENCLOSING_CLASSES))
.containsExactly("0:EnclosedThree", "1:EnclosedTwo", "2:EnclosedOne");
}
@Test
public void typeHierarchyWithEnclosedStrategyOnEnclosedInnerClassScansAnnotations() {
Class<?> source = AnnotationEnclosingClassSample.EnclosedInner.EnclosedInnerInner.class;
assertThat(scan(source, SearchStrategy.TYPE_HIERARCHY_AND_ENCLOSING_CLASSES))
.containsExactly("0:EnclosedThree", "1:EnclosedTwo", "2:EnclosedOne");
}
@Test
public void typeHierarchyWithEnclosedStrategyOnMethodHierarchyUsesTypeHierarchyScan() {
Method source = methodFrom(WithHierarchy.class);
assertThat(scan(source, SearchStrategy.TYPE_HIERARCHY_AND_ENCLOSING_CLASSES)).containsExactly(
"0:TestAnnotation1", "1:TestAnnotation5", "1:TestInheritedAnnotation5",
"2:TestAnnotation6", "3:TestAnnotation2", "3:TestInheritedAnnotation2",
"4:TestAnnotation3", "5:TestAnnotation4");
}
@Test
public void scanWhenProcessorReturnsFromDoWithAggregateExitsEarly() {
String result = AnnotationsScanner.scan(this, WithSingleSuperclass.class,