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

@ -365,7 +365,7 @@ class CglibAopProxy implements AopProxy, Serializable {
*/
private static boolean implementsInterface(Method method, Set<Class<?>> ifcs) {
for (Class<?> ifc : ifcs) {
if (ClassUtils.hasMethod(ifc, method.getName(), method.getParameterTypes())) {
if (ClassUtils.hasMethod(ifc, method)) {
return true;
}
}

View File

@ -97,7 +97,7 @@ abstract class AutowireUtils {
// It was declared by CGLIB, but we might still want to autowire it
// if it was actually declared by the superclass.
Class<?> superclass = wm.getDeclaringClass().getSuperclass();
return !ClassUtils.hasMethod(superclass, wm.getName(), wm.getParameterTypes());
return !ClassUtils.hasMethod(superclass, wm);
}
/**
@ -112,8 +112,7 @@ abstract class AutowireUtils {
if (setter != null) {
Class<?> targetClass = setter.getDeclaringClass();
for (Class<?> ifc : interfaces) {
if (ifc.isAssignableFrom(targetClass) &&
ClassUtils.hasMethod(ifc, setter.getName(), setter.getParameterTypes())) {
if (ifc.isAssignableFrom(targetClass) && ClassUtils.hasMethod(ifc, setter)) {
return true;
}
}

View File

@ -207,7 +207,7 @@ public class InterfaceBasedMBeanInfoAssembler extends AbstractConfigurableMBeanI
* configured interfaces and is public, otherwise {@code false}.
*/
private boolean isPublicInInterface(Method method, String beanKey) {
return ((method.getModifiers() & Modifier.PUBLIC) > 0) && isDeclaredInInterface(method, beanKey);
return Modifier.isPublic(method.getModifiers()) && isDeclaredInInterface(method, beanKey);
}
/**

View File

@ -145,7 +145,7 @@ public class MethodValidationInterceptor implements MethodInterceptor {
factoryBeanType = FactoryBean.class;
}
return (factoryBeanType != null && !method.getName().equals("getObject") &&
ClassUtils.hasMethod(factoryBeanType, method.getName(), method.getParameterTypes()));
ClassUtils.hasMethod(factoryBeanType, method));
}
/**

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;
}
}
}