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.
This commit is contained in:
John Blum
2022-04-12 11:15:32 -07:00
parent 656ef7e874
commit d384e40e31
3 changed files with 66 additions and 23 deletions

View File

@@ -70,7 +70,7 @@ public class QueryMethod {
Assert.notNull(factory, "ProjectionFactory must not be null!");
Parameters.TYPES.stream()
.filter(type -> getNumberOfOccurences(method, type) > 1)
.filter(type -> getNumberOfOccurrences(method, type) > 1)
.findFirst().ifPresent(type -> {
throw new IllegalStateException(
String.format("Method must have only one argument of type %s! Offending method: %s",

View File

@@ -24,6 +24,7 @@ import java.util.function.Consumer;
import org.springframework.data.repository.Repository;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.TypeInformation;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
@@ -104,22 +105,28 @@ public abstract class ClassUtils {
}
/**
* Returns the number of occurences of the given type in the given {@link Method}s parameters.
*
* @param method
* @param type
* @return
* @deprecated Use {@link #getNumberOfOccurrences(Method, Class)}.
*/
public static int getNumberOfOccurences(Method method, Class<?> type) {
@Deprecated
public static int getNumberOfOccurences(@NonNull Method method, @NonNull Class<?> type) {
return getNumberOfOccurrences(method, type);
}
int result = 0;
for (Class<?> clazz : method.getParameterTypes()) {
if (type.equals(clazz)) {
result++;
}
}
/**
* Returns the number of occurrences for the given {@link Method#getParameterTypes() parameter type}
* in the given {@link Method}.
*
* @param method {@link Method} to evaluate.
* @param parameterType {@link Class} of the {@link Method} parameter type to count.
* @return the number of occurrences for the given {@link Method#getParameterTypes() parameter type}
* in the given {@link Method}.
* @see java.lang.reflect.Method#getParameterTypes()
*/
public static int getNumberOfOccurrences(@NonNull Method method, @NonNull Class<?> parameterType) {
return result;
return (int) Arrays.stream(method.getParameterTypes())
.filter(parameterType::equals)
.count();
}
/**