Skip to content

Commit

Permalink
Search implemented interfaces on superclass for @Serviceconnection
Browse files Browse the repository at this point in the history
Refine original fix to also search interfaces on the superclass.

Fixes gh-37671
  • Loading branch information
philwebb committed Oct 15, 2023
1 parent 084bd3a commit e01e4f1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ public ContextCustomizer createContextCustomizer(Class<?> testClass,
}

private void findSources(Class<?> clazz, List<ContainerConnectionSource<?>> sources) {
ReflectionUtils.doWithFields(clazz, (field) -> {
if (clazz == Object.class || clazz == null) {
return;
}
ReflectionUtils.doWithLocalFields(clazz, (field) -> {
MergedAnnotations annotations = MergedAnnotations.from(field);
annotations.stream(ServiceConnection.class)
.forEach((annotation) -> sources.add(createSource(field, annotation)));
Expand All @@ -65,6 +68,7 @@ private void findSources(Class<?> clazz, List<ContainerConnectionSource<?>> sour
for (Class<?> implementedInterface : clazz.getInterfaces()) {
findSources(implementedInterface, sources);
}
findSources(clazz.getSuperclass(), sources);
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ void createContextCustomizerWhenImplementedInterfaceHasServiceConnectionsReturns
assertThat(customizer.getSources()).hasSize(2);
}

@Test
void createContextCustomizerWhenInheritedImplementedInterfaceHasServiceConnectionsReturnsCustomizer() {
ServiceConnectionContextCustomizer customizer = (ServiceConnectionContextCustomizer) this.factory
.createContextCustomizer(ServiceConnectionsImplSubclass.class, null);
assertThat(customizer).isNotNull();
assertThat(customizer.getSources()).hasSize(2);
}

@Test
void createContextCustomizerWhenClassHasNonStaticServiceConnectionFailsWithHelpfulException() {
assertThatIllegalStateException()
Expand Down Expand Up @@ -186,6 +194,10 @@ static class ServiceConnectionsImpl implements ServiceConnectionsInterface {

}

static class ServiceConnectionsImplSubclass extends ServiceConnectionsImpl {

}

static class NonStaticServiceConnection {

@ServiceConnection
Expand Down

0 comments on commit e01e4f1

Please sign in to comment.