This commit is contained in:
Phillip Webb 2023-10-14 19:51:42 -07:00 committed by Andy Wilkinson
parent fcb75b6a1e
commit 343046d30c
1 changed files with 9 additions and 9 deletions

View File

@ -49,26 +49,26 @@ class ServiceConnectionContextCustomizerFactory implements ContextCustomizerFact
public ContextCustomizer createContextCustomizer(Class<?> testClass, public ContextCustomizer createContextCustomizer(Class<?> testClass,
List<ContextConfigurationAttributes> configAttributes) { List<ContextConfigurationAttributes> configAttributes) {
List<ContainerConnectionSource<?>> sources = new ArrayList<>(); List<ContainerConnectionSource<?>> sources = new ArrayList<>();
findSources(testClass, sources); collectSources(testClass, sources);
return new ServiceConnectionContextCustomizer(sources); return new ServiceConnectionContextCustomizer(sources);
} }
private void findSources(Class<?> clazz, List<ContainerConnectionSource<?>> sources) { private void collectSources(Class<?> candidate, List<ContainerConnectionSource<?>> sources) {
if (clazz == Object.class || clazz == null) { if (candidate == Object.class || candidate == null) {
return; return;
} }
ReflectionUtils.doWithLocalFields(clazz, (field) -> { ReflectionUtils.doWithLocalFields(candidate, (field) -> {
MergedAnnotations annotations = MergedAnnotations.from(field); MergedAnnotations annotations = MergedAnnotations.from(field);
annotations.stream(ServiceConnection.class) annotations.stream(ServiceConnection.class)
.forEach((annotation) -> sources.add(createSource(field, annotation))); .forEach((annotation) -> sources.add(createSource(field, annotation)));
}); });
if (TestContextAnnotationUtils.searchEnclosingClass(clazz)) { if (TestContextAnnotationUtils.searchEnclosingClass(candidate)) {
findSources(clazz.getEnclosingClass(), sources); collectSources(candidate.getEnclosingClass(), sources);
} }
for (Class<?> implementedInterface : clazz.getInterfaces()) { for (Class<?> implementedInterface : candidate.getInterfaces()) {
findSources(implementedInterface, sources); collectSources(implementedInterface, sources);
} }
findSources(clazz.getSuperclass(), sources); collectSources(candidate.getSuperclass(), sources);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")