这里主要是为了解决我前面遇到的问题,因为在默认注解DefaultAnnotationHandlerMapping的determineUrlsForHandlerMethods方法中遇到了RelectionUtils的doWith方法,在 determineUrlsForHandlerMethods方法中调用的时候,有两个参数,一个是 Class对象,一个是 new ReflectionUtils.MethodCallback() ,这里的MethodClallback是ReflectionUtils的内部回调接口,它用于处理从Class的对象获取的Method对象。
public interface MethodCallback {
/**
* Perform an operation using the given method.
* @param method the method to operate on
*/
void doWith(Method method) throws IllegalArgumentException, IllegalAccessException;
}
public static void doWithMethods(Class<?> clazz, MethodCallback mc) throws IllegalArgumentException {
doWithMethods(clazz, mc, null);
}
然后就主要来看真正的内容doWithMethods,第三个参数这里没用到,它也是RelectionUtils内部接口
public interface MethodFilter {
/**
* Determine whether the given method matches.
* @param method the method to check
*/
boolean matches(Method method);
}
/**
* Perform the given callback operation on all matching methods of the given
* class and superclasses.
* <p>The same named method occurring on subclass and superclass will appear
* twice, unless excluded by the specified {@link MethodFilter}.
* @param clazz class to start looking at
* @param mc the callback to invoke for each method
* @param mf the filter that determines the methods to apply the callback to
*/
public static void doWithMethods(Class<?> clazz, MethodCallback mc, MethodFilter mf)
throws IllegalArgumentException {
// Keep backing up the inheritance hierarchy.
Class<?> targetClass = clazz;
do {
Method[] methods = targetClass.getDeclaredMethods();
for (Method method : methods) {
if (mf != null && !mf.matches(method)) {
continue;
}
try {
mc.doWith(method);
}
catch (IllegalAccessException ex) {
throw new IllegalStateException("Shouldn't be illegal to access method '" + method.getName()
+ "': " + ex);
}
}
targetClass = targetClass.getSuperclass();
}
while (targetClass != null);
}
遍历类的所有Method,对其进行doWith,这里doWith就由自己实现了。