Add fast path for ClassUtils.hasMethod()
This commit is contained in:
parent
c562c3a0b3
commit
8e5cad2af3
|
@ -365,7 +365,7 @@ class CglibAopProxy implements AopProxy, Serializable {
|
||||||
*/
|
*/
|
||||||
private static boolean implementsInterface(Method method, Set<Class<?>> ifcs) {
|
private static boolean implementsInterface(Method method, Set<Class<?>> ifcs) {
|
||||||
for (Class<?> ifc : ifcs) {
|
for (Class<?> ifc : ifcs) {
|
||||||
if (ClassUtils.hasMethod(ifc, method.getName(), method.getParameterTypes())) {
|
if (ClassUtils.hasMethod(ifc, method)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -97,7 +97,7 @@ abstract class AutowireUtils {
|
||||||
// It was declared by CGLIB, but we might still want to autowire it
|
// It was declared by CGLIB, but we might still want to autowire it
|
||||||
// if it was actually declared by the superclass.
|
// if it was actually declared by the superclass.
|
||||||
Class<?> superclass = wm.getDeclaringClass().getSuperclass();
|
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) {
|
if (setter != null) {
|
||||||
Class<?> targetClass = setter.getDeclaringClass();
|
Class<?> targetClass = setter.getDeclaringClass();
|
||||||
for (Class<?> ifc : interfaces) {
|
for (Class<?> ifc : interfaces) {
|
||||||
if (ifc.isAssignableFrom(targetClass) &&
|
if (ifc.isAssignableFrom(targetClass) && ClassUtils.hasMethod(ifc, setter)) {
|
||||||
ClassUtils.hasMethod(ifc, setter.getName(), setter.getParameterTypes())) {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -207,7 +207,7 @@ public class InterfaceBasedMBeanInfoAssembler extends AbstractConfigurableMBeanI
|
||||||
* configured interfaces and is public, otherwise {@code false}.
|
* configured interfaces and is public, otherwise {@code false}.
|
||||||
*/
|
*/
|
||||||
private boolean isPublicInInterface(Method method, String beanKey) {
|
private boolean isPublicInInterface(Method method, String beanKey) {
|
||||||
return ((method.getModifiers() & Modifier.PUBLIC) > 0) && isDeclaredInInterface(method, beanKey);
|
return Modifier.isPublic(method.getModifiers()) && isDeclaredInInterface(method, beanKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -145,7 +145,7 @@ public class MethodValidationInterceptor implements MethodInterceptor {
|
||||||
factoryBeanType = FactoryBean.class;
|
factoryBeanType = FactoryBean.class;
|
||||||
}
|
}
|
||||||
return (factoryBeanType != null && !method.getName().equals("getObject") &&
|
return (factoryBeanType != null && !method.getName().equals("getObject") &&
|
||||||
ClassUtils.hasMethod(factoryBeanType, method.getName(), method.getParameterTypes()));
|
ClassUtils.hasMethod(factoryBeanType, method));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1101,6 +1101,24 @@ public abstract class ClassUtils {
|
||||||
return (getMethodIfAvailable(clazz, methodName, paramTypes) != null);
|
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,
|
* Determine whether the given class has a public method with the given signature,
|
||||||
* and return it if available (else throws an {@code IllegalStateException}).
|
* 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(clazz, "Class must not be null");
|
||||||
Assert.notNull(methodName, "Method name must not be null");
|
Assert.notNull(methodName, "Method name must not be null");
|
||||||
if (paramTypes != null) {
|
if (paramTypes != null) {
|
||||||
try {
|
return getMethodOrNull(clazz, methodName, paramTypes);
|
||||||
return clazz.getMethod(methodName, paramTypes);
|
|
||||||
}
|
|
||||||
catch (NoSuchMethodException ex) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Set<Method> candidates = findMethodCandidatesByName(clazz, methodName);
|
Set<Method> candidates = findMethodCandidatesByName(clazz, methodName);
|
||||||
|
@ -1370,4 +1383,13 @@ public abstract class ClassUtils {
|
||||||
}
|
}
|
||||||
return candidates;
|
return candidates;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private static Method getMethodOrNull(Class<?> clazz, String methodName, Class<?>[] paramTypes) {
|
||||||
|
try {
|
||||||
|
return clazz.getMethod(methodName, paramTypes);
|
||||||
|
} catch (NoSuchMethodException ex) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue