|
22 | 22 | import java.lang.reflect.Method;
|
23 | 23 | import java.lang.reflect.Modifier;
|
24 | 24 | import java.util.ArrayList;
|
25 |
| -import java.util.Arrays; |
26 | 25 | import java.util.LinkedHashSet;
|
27 | 26 | import java.util.List;
|
28 | 27 | import java.util.Objects;
|
|
37 | 36 | import org.springframework.test.context.bean.override.OverrideMetadata;
|
38 | 37 | import org.springframework.util.Assert;
|
39 | 38 | import org.springframework.util.ReflectionUtils;
|
| 39 | +import org.springframework.util.ReflectionUtils.MethodFilter; |
40 | 40 | import org.springframework.util.StringUtils;
|
41 | 41 |
|
42 | 42 | /**
|
@@ -84,18 +84,13 @@ static Method findTestBeanFactoryMethod(Class<?> clazz, Class<?> methodReturnTyp
|
84 | 84 | static Method findTestBeanFactoryMethod(Class<?> clazz, Class<?> methodReturnType, List<String> methodNames) {
|
85 | 85 | Assert.notEmpty(methodNames, "At least one candidate method name is required");
|
86 | 86 | Set<String> supportedNames = new LinkedHashSet<>(methodNames);
|
87 |
| - List<Method> methods = Arrays.stream(ReflectionUtils.getAllDeclaredMethods(clazz)) |
88 |
| - .filter(method -> Modifier.isStatic(method.getModifiers()) && |
89 |
| - supportedNames.contains(method.getName()) && |
90 |
| - methodReturnType.isAssignableFrom(method.getReturnType())) |
91 |
| - .toList(); |
| 87 | + MethodFilter methodFilter = method -> (Modifier.isStatic(method.getModifiers()) && |
| 88 | + supportedNames.contains(method.getName()) && |
| 89 | + methodReturnType.isAssignableFrom(method.getReturnType())); |
92 | 90 |
|
| 91 | + List<Method> methods = findMethods(clazz, methodFilter); |
93 | 92 | if (methods.isEmpty() && TestContextAnnotationUtils.searchEnclosingClass(clazz)) {
|
94 |
| - methods = Arrays.stream(ReflectionUtils.getAllDeclaredMethods(clazz.getEnclosingClass())) |
95 |
| - .filter(method -> Modifier.isStatic(method.getModifiers()) && |
96 |
| - supportedNames.contains(method.getName()) && |
97 |
| - methodReturnType.isAssignableFrom(method.getReturnType())) |
98 |
| - .toList(); |
| 93 | + methods = findMethods(clazz.getEnclosingClass(), methodFilter); |
99 | 94 | }
|
100 | 95 |
|
101 | 96 | Assert.state(!methods.isEmpty(), () -> """
|
@@ -143,6 +138,13 @@ public TestBeanOverrideMetadata createMetadata(Annotation overrideAnnotation, Cl
|
143 | 138 | }
|
144 | 139 |
|
145 | 140 |
|
| 141 | + private static List<Method> findMethods(Class<?> clazz, MethodFilter methodFilter) { |
| 142 | + List<Method> methods = new ArrayList<>(); |
| 143 | + ReflectionUtils.doWithMethods(clazz, methods::add, methodFilter); |
| 144 | + return methods; |
| 145 | + } |
| 146 | + |
| 147 | + |
146 | 148 | static final class TestBeanOverrideMetadata extends OverrideMetadata {
|
147 | 149 |
|
148 | 150 | private final Method overrideMethod;
|
|
0 commit comments