Use Stream.

This commit is contained in:
Gary Gregory 2022-08-21 11:06:02 -04:00
parent b6df0af297
commit 1297d7c365
1 changed files with 8 additions and 17 deletions

View File

@ -665,7 +665,7 @@ public class MethodUtils {
* @return The accessible method * @return The accessible method
*/ */
public static Method getMatchingAccessibleMethod(final Class<?> cls, public static Method getMatchingAccessibleMethod(final Class<?> cls,
final String methodName, final Class<?>... parameterTypes) { final String methodName, final Class<?>... parameterTypes) {
try { try {
return MemberUtils.setAccessibleWorkaround(cls.getMethod(methodName, parameterTypes)); return MemberUtils.setAccessibleWorkaround(cls.getMethod(methodName, parameterTypes));
} catch (final NoSuchMethodException ignored) { } catch (final NoSuchMethodException ignored) {
@ -673,14 +673,8 @@ public class MethodUtils {
} }
// search through all methods // search through all methods
final Method[] methods = cls.getMethods(); final Method[] methods = cls.getMethods();
final List<Method> matchingMethods = new ArrayList<>(); final List<Method> matchingMethods = Stream.of(methods)
for (final Method method : methods) { .filter(method -> method.getName().equals(methodName) && MemberUtils.isMatchingMethod(method, parameterTypes)).collect(Collectors.toList());
// compare name and parameters
if (method.getName().equals(methodName) &&
MemberUtils.isMatchingMethod(method, parameterTypes)) {
matchingMethods.add(method);
}
}
// Sort methods by signature to force deterministic result // Sort methods by signature to force deterministic result
matchingMethods.sort(METHOD_BY_SIGNATURE); matchingMethods.sort(METHOD_BY_SIGNATURE);
@ -689,10 +683,7 @@ public class MethodUtils {
for (final Method method : matchingMethods) { for (final Method method : matchingMethods) {
// get accessible version of method // get accessible version of method
final Method accessibleMethod = getAccessibleMethod(method); final Method accessibleMethod = getAccessibleMethod(method);
if (accessibleMethod != null && (bestMatch == null || MemberUtils.compareMethodFit( if (accessibleMethod != null && (bestMatch == null || MemberUtils.compareMethodFit(accessibleMethod, bestMatch, parameterTypes) < 0)) {
accessibleMethod,
bestMatch,
parameterTypes) < 0)) {
bestMatch = accessibleMethod; bestMatch = accessibleMethod;
} }
} }
@ -706,11 +697,11 @@ public class MethodUtils {
final String methodParameterComponentTypeName = ClassUtils.primitiveToWrapper(methodParameterComponentType).getName(); final String methodParameterComponentTypeName = ClassUtils.primitiveToWrapper(methodParameterComponentType).getName();
final Class<?> lastParameterType = parameterTypes[parameterTypes.length - 1]; final Class<?> lastParameterType = parameterTypes[parameterTypes.length - 1];
final String parameterTypeName = lastParameterType==null ? null : lastParameterType.getName(); final String parameterTypeName = lastParameterType == null ? null : lastParameterType.getName();
final String parameterTypeSuperClassName = lastParameterType==null ? null : lastParameterType.getSuperclass().getName(); final String parameterTypeSuperClassName = lastParameterType == null ? null : lastParameterType.getSuperclass().getName();
if (parameterTypeName!= null && parameterTypeSuperClassName != null && !methodParameterComponentTypeName.equals(parameterTypeName) if (parameterTypeName != null && parameterTypeSuperClassName != null && !methodParameterComponentTypeName.equals(parameterTypeName)
&& !methodParameterComponentTypeName.equals(parameterTypeSuperClassName)) { && !methodParameterComponentTypeName.equals(parameterTypeSuperClassName)) {
return null; return null;
} }
} }