Spring MVC 源码学习札记(五)说说ReflectionUtils

这里主要是为了解决我前面遇到的问题,因为在默认注解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就由自己实现了。

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值