Add fast path for ClassUtils.hasMethod()

This commit is contained in:
stsypanov
2019-12-30 17:20:07 +02:00
committed by Juergen Hoeller
parent c562c3a0b3
commit 8e5cad2af3
5 changed files with 33 additions and 12 deletions

View File

@@ -1101,6 +1101,24 @@ public abstract class ClassUtils {
return (getMethodIfAvailable(clazz, methodName, paramTypes) != null);
}
/**
* Determine whether the given class has a public method with the given signature.
* @param clazz the clazz to analyze
* @param method checked method
* @return whether the class has a corresponding method
* @see Method#getDeclaringClass
*/
public static boolean hasMethod(Class<?> clazz, Method method) {
Assert.notNull(clazz, "Class must not be null");
Assert.notNull(method, "Method must not be null");
if (clazz == method.getDeclaringClass()) {
return true;
}
String methodName = method.getName();
Class<?>[] paramTypes = method.getParameterTypes();
return getMethodOrNull(clazz, methodName, paramTypes) != null;
}
/**
* Determine whether the given class has a public method with the given signature,
* and return it if available (else throws an {@code IllegalStateException}).
@@ -1158,12 +1176,7 @@ public abstract class ClassUtils {
Assert.notNull(clazz, "Class must not be null");
Assert.notNull(methodName, "Method name must not be null");
if (paramTypes != null) {
try {
return clazz.getMethod(methodName, paramTypes);
}
catch (NoSuchMethodException ex) {
return null;
}
return getMethodOrNull(clazz, methodName, paramTypes);
}
else {
Set<Method> candidates = findMethodCandidatesByName(clazz, methodName);
@@ -1370,4 +1383,13 @@ public abstract class ClassUtils {
}
return candidates;
}
@Nullable
private static Method getMethodOrNull(Class<?> clazz, String methodName, Class<?>[] paramTypes) {
try {
return clazz.getMethod(methodName, paramTypes);
} catch (NoSuchMethodException ex) {
return null;
}
}
}