Skip to content

Commit 44e8699

Browse files
committed
Filter bridge methods from DefaultCrudMethods lookup.
We now filter bridge methods when looking up the most specific method when looking for an override. Closes #3401
1 parent 2f109c6 commit 44e8699

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/main/java/org/springframework/data/repository/core/support/DefaultCrudMethods.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
*/
1616
package org.springframework.data.repository.core.support;
1717

18-
import static java.util.Arrays.*;
1918
import static org.springframework.data.util.Optionals.*;
2019

2120
import java.lang.reflect.Method;
21+
import java.util.ArrayList;
22+
import java.util.Arrays;
23+
import java.util.List;
2224
import java.util.Optional;
2325
import java.util.function.Supplier;
2426
import java.util.stream.Stream;
@@ -85,7 +87,7 @@ public DefaultCrudMethods(RepositoryMetadata metadata) {
8587
*/
8688
private static Optional<Method> selectMostSuitableSaveMethod(RepositoryMetadata metadata) {
8789

88-
return asList(metadata.getDomainType(), Object.class).stream()//
90+
return Arrays.asList(metadata.getDomainType(), Object.class).stream()//
8991
.flatMap(it -> toStream(findMethod(metadata.getRepositoryInterface(), SAVE, it)))//
9092
.flatMap(it -> toStream(getMostSpecificMethod(it, metadata.getRepositoryInterface())))//
9193
.findFirst();
@@ -160,7 +162,7 @@ private static Optional<Method> selectMostSuitableFindAllMethod(RepositoryMetada
160162
*/
161163
private static Optional<Method> selectMostSuitableFindOneMethod(RepositoryMetadata metadata) {
162164

163-
return asList(metadata.getIdType(), Object.class).stream()//
165+
return Arrays.asList(metadata.getIdType(), Object.class).stream()//
164166
.flatMap(it -> toStream(findMethod(metadata.getRepositoryInterface(), FIND_ONE, it)))//
165167
.flatMap(it -> toStream(getMostSpecificMethod(it, metadata.getRepositoryInterface())))//
166168
.findFirst();
@@ -224,6 +226,19 @@ public Optional<Method> getDeleteMethod() {
224226
}
225227

226228
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+
227238
return Optional.ofNullable(ReflectionUtils.findMethod(type, name, parameterTypes));
228239
}
240+
241+
private static boolean hasSameParams(Method method, Class<?>[] paramTypes) {
242+
return (paramTypes.length == method.getParameterCount() && Arrays.equals(paramTypes, method.getParameterTypes()));
243+
}
229244
}

0 commit comments

Comments
 (0)