Skip to content

Commit 3a36ed5

Browse files
committed
DATACMNS-854 - Polishing.
Simplified conditional expressions in DefaultRepositoryInformation. Reordered test method to reflect natural order of adding. Original pull request: #162.
1 parent e0fc7de commit 3a36ed5

File tree

2 files changed

+34
-31
lines changed

2 files changed

+34
-31
lines changed

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2015 the original author or authors.
2+
* Copyright 2011-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -342,26 +342,28 @@ public Set<Class<?>> getAlternativeDomainTypes() {
342342
*/
343343
private boolean parametersMatch(Method method, Method baseClassMethod) {
344344

345+
Class<?>[] methodParameterTypes = method.getParameterTypes();
345346
Type[] genericTypes = baseClassMethod.getGenericParameterTypes();
346347
Class<?>[] types = baseClassMethod.getParameterTypes();
347348

348349
for (int i = 0; i < genericTypes.length; i++) {
349350

350-
Type type = genericTypes[i];
351+
Type genericType = genericTypes[i];
352+
Class<?> type = types[i];
351353
MethodParameter parameter = new MethodParameter(method, i);
352354
Class<?> parameterType = resolveParameterType(parameter, metadata.getRepositoryInterface());
353355

354-
if (type instanceof TypeVariable<?>) {
355-
if (!matchesGenericType((TypeVariable<?>) type, parameterType)) {
356-
return false;
357-
}
358-
} else {
359-
// We must either have an exact match, or,
360-
if (!types[i].equals(parameterType) &&
361-
// the tpes must be assignable _and_ signature definition types must be identical.
362-
!(types[i].isAssignableFrom(parameterType) && types[i].equals(method.getParameterTypes()[i]))) {
356+
if (genericType instanceof TypeVariable<?>) {
357+
358+
if (!matchesGenericType((TypeVariable<?>) genericType, parameterType)) {
363359
return false;
364360
}
361+
362+
continue;
363+
}
364+
365+
if (!type.isAssignableFrom(parameterType) || !type.equals(methodParameterTypes[i])) {
366+
return false;
365367
}
366368
}
367369

@@ -392,7 +394,7 @@ private boolean matchesGenericType(TypeVariable<?> variable, Class<?> parameterT
392394
boolean isDomainTypeVariableReference = DOMAIN_TYPE_NAME.equals(referenceName);
393395
boolean parameterMatchesEntityType = parameterType.isAssignableFrom(entityType);
394396

395-
// We need this check to besure not to match save(Iterable) for entities implementing Iterable
397+
// We need this check to be sure not to match save(Iterable) for entities implementing Iterable
396398
boolean isNotIterable = !parameterType.equals(Iterable.class);
397399

398400
if (isDomainTypeVariableReference && parameterMatchesEntityType && isNotIterable) {

src/test/java/org/springframework/data/repository/core/support/DefaultRepositoryInformationUnitTests.java

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2014 the original author or authors.
2+
* Copyright 2013-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -94,22 +94,6 @@ public void discoversCustomlyImplementedCrudMethod() throws SecurityException, N
9494
assertThat(information.getTargetClassMethod(source), is(expected));
9595
}
9696

97-
/**
98-
* @see DATACMNS-854
99-
*/
100-
@Test
101-
public void discoversCustomlyImplementedCrudMethodWithGenerics() throws SecurityException, NoSuchMethodException {
102-
103-
RepositoryMetadata metadata = new DefaultRepositoryMetadata(FooRepository.class);
104-
RepositoryInformation information = new DefaultRepositoryInformation(metadata, CrudRepository.class,
105-
customImplementation.getClass());
106-
107-
Method source = FooRepositoryCustom.class.getMethod("exists", Object.class);
108-
Method expected = customImplementation.getClass().getMethod("exists", Object.class);
109-
110-
assertThat(information.getTargetClassMethod(source), is(expected));
111-
}
112-
11397
@Test
11498
public void considersIntermediateMethodsAsFinderMethods() {
11599

@@ -239,20 +223,37 @@ public void getQueryShouldNotReturnAnyBridgeMethods() {
239223
}
240224
}
241225

242-
private Method getMethodFrom(Class<?> type, String name) {
226+
/**
227+
* @see DATACMNS-854
228+
*/
229+
@Test
230+
public void discoversCustomlyImplementedCrudMethodWithGenerics() throws SecurityException, NoSuchMethodException {
231+
232+
RepositoryMetadata metadata = new DefaultRepositoryMetadata(FooRepository.class);
233+
RepositoryInformation information = new DefaultRepositoryInformation(metadata, CrudRepository.class,
234+
customImplementation.getClass());
235+
236+
Method source = FooRepositoryCustom.class.getMethod("exists", Object.class);
237+
Method expected = customImplementation.getClass().getMethod("exists", Object.class);
238+
239+
assertThat(information.getTargetClassMethod(source), is(expected));
240+
}
241+
242+
private static Method getMethodFrom(Class<?> type, String name) {
243+
243244
for (Method method : type.getMethods()) {
244245
if (method.getName().equals(name)) {
245246
return method;
246247
}
247248
}
249+
248250
return null;
249251
}
250252

251253
@Target(ElementType.METHOD)
252254
@Retention(RetentionPolicy.RUNTIME)
253255
@QueryAnnotation
254256
@interface MyQuery {
255-
256257
}
257258

258259
interface FooRepository extends CrudRepository<User, Integer>, FooRepositoryCustom {

0 commit comments

Comments
 (0)