restored getMostSpecificMethod's traversal of the inheritance hierarchy

This commit is contained in:
Juergen Hoeller 2009-09-15 22:31:08 +00:00
parent fd81aa205d
commit 2a0d68cb5c
2 changed files with 10 additions and 18 deletions

View File

@ -689,18 +689,12 @@ public abstract class ClassUtils {
* <code>targetClass</code> doesn't implement it or is <code>null</code> * <code>targetClass</code> doesn't implement it or is <code>null</code>
*/ */
public static Method getMostSpecificMethod(Method method, Class targetClass) { public static Method getMostSpecificMethod(Method method, Class targetClass) {
Method result = method; Method specificMethod = null;
if (method != null && !Modifier.isPrivate(method.getModifiers()) && if (method != null && !Modifier.isPrivate(method.getModifiers()) &&
targetClass != null && !targetClass.equals(method.getDeclaringClass())) { targetClass != null && !targetClass.equals(method.getDeclaringClass())) {
try { specificMethod = ReflectionUtils.findMethod(targetClass, method.getName(), method.getParameterTypes());
result = targetClass.getDeclaredMethod(method.getName(), method.getParameterTypes());
} }
catch (NoSuchMethodException ex) { return (specificMethod != null ? specificMethod : method);
// Perhaps the target class doesn't implement this method:
// that's fine, just use the original method.
}
}
return result;
} }
/** /**
@ -716,14 +710,11 @@ public abstract class ClassUtils {
Assert.notNull(methodName, "Method name must not be null"); Assert.notNull(methodName, "Method name must not be null");
try { try {
Method method = clazz.getDeclaredMethod(methodName, args); Method method = clazz.getDeclaredMethod(methodName, args);
if ((method.getModifiers() & Modifier.STATIC) != 0) { return ((method.getModifiers() & Modifier.STATIC) != 0 ? method : null);
return method;
}
} }
catch (NoSuchMethodException ex) { catch (NoSuchMethodException ex) {
return null; return null;
} }
return null;
} }

View File

@ -72,18 +72,19 @@ public class HandlerMethodResolver {
*/ */
public void init(Class<?> handlerType) { public void init(Class<?> handlerType) {
Class<?>[] handlerTypes = Class<?>[] handlerTypes =
Proxy.isProxyClass(handlerType) ? handlerType.getInterfaces() : new Class<?>[]{handlerType}; Proxy.isProxyClass(handlerType) ? handlerType.getInterfaces() : new Class<?>[] {handlerType};
for (final Class<?> currentHandlerType : handlerTypes) { for (final Class<?> currentHandlerType : handlerTypes) {
ReflectionUtils.doWithMethods(currentHandlerType, new ReflectionUtils.MethodCallback() { ReflectionUtils.doWithMethods(currentHandlerType, new ReflectionUtils.MethodCallback() {
public void doWith(Method method) { public void doWith(Method method) {
if (isHandlerMethod(ClassUtils.getMostSpecificMethod(method, currentHandlerType))) { Method specificMethod = ClassUtils.getMostSpecificMethod(method, currentHandlerType);
handlerMethods.add(ClassUtils.getMostSpecificMethod(method, currentHandlerType)); if (isHandlerMethod(specificMethod)) {
handlerMethods.add(specificMethod);
} }
else if (method.isAnnotationPresent(InitBinder.class)) { else if (method.isAnnotationPresent(InitBinder.class)) {
initBinderMethods.add(ClassUtils.getMostSpecificMethod(method, currentHandlerType)); initBinderMethods.add(specificMethod);
} }
else if (method.isAnnotationPresent(ModelAttribute.class)) { else if (method.isAnnotationPresent(ModelAttribute.class)) {
modelAttributeMethods.add(ClassUtils.getMostSpecificMethod(method, currentHandlerType)); modelAttributeMethods.add(specificMethod);
} }
} }
}); });