|
15 | 15 | */ |
16 | 16 | package org.springframework.data.repository.core.support; |
17 | 17 |
|
18 | | -import static java.util.Arrays.*; |
19 | 18 | import static org.springframework.data.util.Optionals.*; |
20 | 19 |
|
21 | 20 | import java.lang.reflect.Method; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.List; |
22 | 24 | import java.util.Optional; |
23 | 25 | import java.util.function.Supplier; |
24 | 26 | import java.util.stream.Stream; |
@@ -85,7 +87,7 @@ public DefaultCrudMethods(RepositoryMetadata metadata) { |
85 | 87 | */ |
86 | 88 | private static Optional<Method> selectMostSuitableSaveMethod(RepositoryMetadata metadata) { |
87 | 89 |
|
88 | | - return asList(metadata.getDomainType(), Object.class).stream()// |
| 90 | + return Arrays.asList(metadata.getDomainType(), Object.class).stream()// |
89 | 91 | .flatMap(it -> toStream(findMethod(metadata.getRepositoryInterface(), SAVE, it)))// |
90 | 92 | .flatMap(it -> toStream(getMostSpecificMethod(it, metadata.getRepositoryInterface())))// |
91 | 93 | .findFirst(); |
@@ -160,7 +162,7 @@ private static Optional<Method> selectMostSuitableFindAllMethod(RepositoryMetada |
160 | 162 | */ |
161 | 163 | private static Optional<Method> selectMostSuitableFindOneMethod(RepositoryMetadata metadata) { |
162 | 164 |
|
163 | | - return asList(metadata.getIdType(), Object.class).stream()// |
| 165 | + return Arrays.asList(metadata.getIdType(), Object.class).stream()// |
164 | 166 | .flatMap(it -> toStream(findMethod(metadata.getRepositoryInterface(), FIND_ONE, it)))// |
165 | 167 | .flatMap(it -> toStream(getMostSpecificMethod(it, metadata.getRepositoryInterface())))// |
166 | 168 | .findFirst(); |
@@ -224,6 +226,19 @@ public Optional<Method> getDeleteMethod() { |
224 | 226 | } |
225 | 227 |
|
226 | 228 | private static Optional<Method> findMethod(Class<?> type, String name, Class<?>... parameterTypes) { |
| 229 | + |
| 230 | + List<Method> candidates = new ArrayList<>(); |
| 231 | + ReflectionUtils.doWithMethods(type, candidates::add, |
| 232 | + it -> it.getName().equals(name) && hasSameParams(it, parameterTypes) && !it.isBridge()); |
| 233 | + |
| 234 | + if (!candidates.isEmpty()) { |
| 235 | + return Optional.of(candidates.get(0)); |
| 236 | + } |
| 237 | + |
227 | 238 | return Optional.ofNullable(ReflectionUtils.findMethod(type, name, parameterTypes)); |
228 | 239 | } |
| 240 | + |
| 241 | + private static boolean hasSameParams(Method method, Class<?>[] paramTypes) { |
| 242 | + return (paramTypes.length == method.getParameterCount() && Arrays.equals(paramTypes, method.getParameterTypes())); |
| 243 | + } |
229 | 244 | } |
0 commit comments