Skip to content

Commit d384e40

Browse files
committed
Fix mispelling of ClassUtils.getNumberOfOccurrences(..).
* Re-implement getNumberOfOccurrences(..) in terms of a Stream. * Deprecate the mispelled getNumberOfOccurences(..) method and delegate to the new getNumberOfOccurrences(..) method. * Edit Javadoc. Closes #2600.
1 parent 656ef7e commit d384e40

File tree

3 files changed

+66
-23
lines changed

3 files changed

+66
-23
lines changed

src/main/java/org/springframework/data/repository/query/QueryMethod.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public QueryMethod(Method method, RepositoryMetadata metadata, ProjectionFactory
7070
Assert.notNull(factory, "ProjectionFactory must not be null!");
7171

7272
Parameters.TYPES.stream()
73-
.filter(type -> getNumberOfOccurences(method, type) > 1)
73+
.filter(type -> getNumberOfOccurrences(method, type) > 1)
7474
.findFirst().ifPresent(type -> {
7575
throw new IllegalStateException(
7676
String.format("Method must have only one argument of type %s! Offending method: %s",

src/main/java/org/springframework/data/repository/util/ClassUtils.java

+20-13
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.springframework.data.repository.Repository;
2525
import org.springframework.data.util.ClassTypeInformation;
2626
import org.springframework.data.util.TypeInformation;
27+
import org.springframework.lang.NonNull;
2728
import org.springframework.lang.Nullable;
2829
import org.springframework.util.Assert;
2930
import org.springframework.util.ReflectionUtils;
@@ -104,22 +105,28 @@ public static boolean isGenericRepositoryInterface(@Nullable String interfaceNam
104105
}
105106

106107
/**
107-
* Returns the number of occurences of the given type in the given {@link Method}s parameters.
108-
*
109-
* @param method
110-
* @param type
111-
* @return
108+
* @deprecated Use {@link #getNumberOfOccurrences(Method, Class)}.
112109
*/
113-
public static int getNumberOfOccurences(Method method, Class<?> type) {
110+
@Deprecated
111+
public static int getNumberOfOccurences(@NonNull Method method, @NonNull Class<?> type) {
112+
return getNumberOfOccurrences(method, type);
113+
}
114114

115-
int result = 0;
116-
for (Class<?> clazz : method.getParameterTypes()) {
117-
if (type.equals(clazz)) {
118-
result++;
119-
}
120-
}
115+
/**
116+
* Returns the number of occurrences for the given {@link Method#getParameterTypes() parameter type}
117+
* in the given {@link Method}.
118+
*
119+
* @param method {@link Method} to evaluate.
120+
* @param parameterType {@link Class} of the {@link Method} parameter type to count.
121+
* @return the number of occurrences for the given {@link Method#getParameterTypes() parameter type}
122+
* in the given {@link Method}.
123+
* @see java.lang.reflect.Method#getParameterTypes()
124+
*/
125+
public static int getNumberOfOccurrences(@NonNull Method method, @NonNull Class<?> parameterType) {
121126

122-
return result;
127+
return (int) Arrays.stream(method.getParameterTypes())
128+
.filter(parameterType::equals)
129+
.count();
123130
}
124131

125132
/**

src/test/java/org/springframework/data/repository/util/ClassUtilsUnitTests.java

+45-9
Original file line numberDiff line numberDiff line change
@@ -15,44 +15,74 @@
1515
*/
1616
package org.springframework.data.repository.util;
1717

18-
import static org.assertj.core.api.Assertions.*;
19-
import static org.springframework.data.repository.util.ClassUtils.*;
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
2020

2121
import java.io.Serializable;
22+
import java.lang.reflect.Method;
2223
import java.util.List;
2324
import java.util.Map;
2425
import java.util.concurrent.Future;
2526

2627
import org.junit.jupiter.api.Test;
2728
import org.springframework.data.domain.Page;
2829
import org.springframework.data.domain.Pageable;
30+
import org.springframework.data.domain.Sort;
2931
import org.springframework.data.repository.Repository;
3032
import org.springframework.scheduling.annotation.Async;
3133

3234
/**
33-
* Unit test for {@link ClassUtils}.
35+
* Unit tests for {@link ClassUtils}.
3436
*
3537
* @author Oliver Gierke
38+
* @author John Blum
3639
*/
3740
class ClassUtilsUnitTests {
3841

3942
@Test
4043
void rejectsInvalidReturnType() {
41-
assertThatIllegalStateException().isThrownBy(() -> assertReturnTypeAssignable(
44+
assertThatIllegalStateException().isThrownBy(() -> ClassUtils.assertReturnTypeAssignable(
4245
SomeDao.class.getMethod("findByFirstname", Pageable.class, String.class), User.class));
4346
}
4447

4548
@Test
4649
void determinesValidFieldsCorrectly() {
4750

48-
assertThat(hasProperty(User.class, "firstname")).isTrue();
49-
assertThat(hasProperty(User.class, "Firstname")).isTrue();
50-
assertThat(hasProperty(User.class, "address")).isFalse();
51+
assertThat(ClassUtils.hasProperty(User.class, "firstname")).isTrue();
52+
assertThat(ClassUtils.hasProperty(User.class, "Firstname")).isTrue();
53+
assertThat(ClassUtils.hasProperty(User.class, "address")).isFalse();
5154
}
5255

5356
@Test // DATACMNS-769
5457
void unwrapsWrapperTypesBeforeAssignmentCheck() throws Exception {
55-
assertReturnTypeAssignable(UserRepository.class.getMethod("findAsync", Pageable.class), Page.class);
58+
ClassUtils.assertReturnTypeAssignable(UserRepository.class.getMethod("findAsync", Pageable.class),
59+
Page.class);
60+
}
61+
62+
@Test
63+
public void numberOfOccurrencesForMultipleMethodParametersOfType() throws Exception {
64+
65+
Method findByAddress = AnotherDao.class.getMethod("findByAddress", Pageable.class, Pageable.class);
66+
67+
assertThat(ClassUtils.getNumberOfOccurrences(findByAddress, Pageable.class)).isEqualTo(2);
68+
}
69+
70+
@Test
71+
public void numberOfOccurrencesForNoMethodParameterOfType() throws Exception {
72+
73+
Method findByAddress = AnotherDao.class.getMethod("findByAddress", Pageable.class, Pageable.class);
74+
75+
assertThat(ClassUtils.getNumberOfOccurrences(findByAddress, Sort.class)).isZero();
76+
assertThat(ClassUtils.getNumberOfOccurrences(findByAddress, Page.class)).isZero();
77+
}
78+
79+
@Test
80+
public void numberOfOccurrencesForSingleMethodParameterOfType() throws Exception {
81+
82+
Method findByFirstname = SomeDao.class.getMethod("findByFirstname", Pageable.class, String.class);
83+
84+
assertThat(ClassUtils.getNumberOfOccurrences(findByFirstname, Pageable.class)).isOne();
85+
assertThat(ClassUtils.getNumberOfOccurrences(findByFirstname, String.class)).isOne();
5686
}
5787

5888
@SuppressWarnings("unused")
@@ -66,7 +96,7 @@ String getAddress() {
6696
}
6797
}
6898

69-
static interface UserRepository extends Repository<User, Integer> {
99+
interface UserRepository extends Repository<User, Integer> {
70100

71101
@Async
72102
Future<Page<User>> findAsync(Pageable pageable);
@@ -81,6 +111,12 @@ interface SomeDao extends Serializable, UserRepository {
81111
List<Map<String, Object>> anotherMethod();
82112
}
83113

114+
interface AnotherDao extends Repository<User, Integer> {
115+
116+
Page<User> findByAddress(Pageable pageableOne, Pageable pageableTwo);
117+
118+
}
119+
84120
class GenericType<T> {
85121

86122
}

0 commit comments

Comments
 (0)