Fix bug in TYPE_HIERARCHY_AND_ENCLOSING_CLASSES strategy

Prior to this commit, the new `TYPE_HIERARCHY_AND_ENCLOSING_CLASSES`
annotation search strategy failed to find annotations on enclosing
classes if the source class was a nested class that itself had no
annotations present.

This commit fixes this by adding special logic to AnnotationsScanner's
isWithoutHierarchy() method to properly support nested classes.

Closes gh-23378
This commit is contained in:
Sam Brannen
2019-07-31 22:05:46 +02:00
parent b03dd47598
commit de3c115614
2 changed files with 35 additions and 7 deletions

View File

@@ -692,6 +692,19 @@ public class MergedAnnotationsTests {
Transactional.class)).hasSize(1);
}
@Test
public void streamTypeHierarchyAndEnclosingClassesFromNonAnnotatedInnerClassWithAnnotatedEnclosingClass() {
Stream<Class<?>> classes = MergedAnnotations.from(AnnotatedClass.NonAnnotatedInnerClass.class,
SearchStrategy.TYPE_HIERARCHY_AND_ENCLOSING_CLASSES).stream().map(MergedAnnotation::getType);
assertThat(classes).containsExactly(Component.class, Indexed.class);
}
@Test
public void streamTypeHierarchyAndEnclosingClassesFromNonAnnotatedStaticNestedClassWithAnnotatedEnclosingClass() {
Stream<Class<?>> classes = MergedAnnotations.from(AnnotatedClass.NonAnnotatedStaticNestedClass.class,
SearchStrategy.TYPE_HIERARCHY_AND_ENCLOSING_CLASSES).stream().map(MergedAnnotation::getType);
assertThat(classes).containsExactly(Component.class, Indexed.class);
}
@Test
public void getFromMethodWithMethodAnnotationOnLeaf() throws Exception {
Method method = Leaf.class.getMethod("annotatedOnLeaf");
@@ -2112,6 +2125,16 @@ public class MergedAnnotationsTests {
static class NonAnnotatedClass {
}
@Component
static class AnnotatedClass {
class NonAnnotatedInnerClass {
}
static class NonAnnotatedStaticNestedClass {
}
}
static interface NonAnnotatedInterface {
}
@@ -2839,6 +2862,7 @@ public class MergedAnnotationsTests {
public static class ImplementsInterfaceWithGenericAnnotatedMethod
implements InterfaceWithGenericAnnotatedMethod<String> {
@Override
public void foo(String t) {
}
}
@@ -2852,6 +2876,7 @@ public class MergedAnnotationsTests {
public static class ExtendsBaseClassWithGenericAnnotatedMethod
extends BaseClassWithGenericAnnotatedMethod<String> {
@Override
public void foo(String t) {
}
}