org.springframework.util.ReflectionUtils.MethodFilter Java Examples

The following examples show how to use org.springframework.util.ReflectionUtils.MethodFilter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BridgeMethodResolver.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Find the original method for the supplied {@link Method bridge Method}.
 * <p>It is safe to call this method passing in a non-bridge {@link Method} instance.
 * In such a case, the supplied {@link Method} instance is returned directly to the caller.
 * Callers are <strong>not</strong> required to check for bridging before calling this method.
 * @param bridgeMethod the method to introspect
 * @return the original method (either the bridged method or the passed-in method
 * if no more specific one could be found)
 */
public static Method findBridgedMethod(Method bridgeMethod) {
	if (!bridgeMethod.isBridge()) {
		return bridgeMethod;
	}
	Method bridgedMethod = cache.get(bridgeMethod);
	if (bridgedMethod == null) {
		// Gather all methods with matching name and parameter size.
		List<Method> candidateMethods = new ArrayList<>();
		MethodFilter filter = candidateMethod ->
				isBridgedCandidateFor(candidateMethod, bridgeMethod);
		ReflectionUtils.doWithMethods(bridgeMethod.getDeclaringClass(), candidateMethods::add, filter);
		if (!candidateMethods.isEmpty()) {
			bridgedMethod = candidateMethods.size() == 1 ?
					candidateMethods.get(0) :
					searchCandidates(candidateMethods, bridgeMethod);
		}
		if (bridgedMethod == null) {
			// A bridge method was passed in but we couldn't find the bridged method.
			// Let's proceed with the passed-in method and hope for the best...
			bridgedMethod = bridgeMethod;
		}
		cache.put(bridgeMethod, bridgedMethod);
	}
	return bridgedMethod;
}
 
Example #2
Source File: MvcUriComponentsBuilder.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private static Method getMethod(Class<?> controllerType, final String methodName, final Object... args) {
	MethodFilter selector = method -> {
		String name = method.getName();
		int argLength = method.getParameterCount();
		return (name.equals(methodName) && argLength == args.length);
	};
	Set<Method> methods = MethodIntrospector.selectMethods(controllerType, selector);
	if (methods.size() == 1) {
		return methods.iterator().next();
	}
	else if (methods.size() > 1) {
		throw new IllegalArgumentException(String.format(
				"Found two methods named '%s' accepting arguments %s in controller %s: [%s]",
				methodName, Arrays.asList(args), controllerType.getName(), methods));
	}
	else {
		throw new IllegalArgumentException("No method named '" + methodName + "' with " + args.length +
				" arguments found in controller " + controllerType.getName());
	}
}
 
Example #3
Source File: MvcUriComponentsBuilder.java    From java-technology-stack with MIT License 6 votes vote down vote up
private static Method getMethod(Class<?> controllerType, final String methodName, final Object... args) {
	MethodFilter selector = method -> {
		String name = method.getName();
		int argLength = method.getParameterCount();
		return (name.equals(methodName) && argLength == args.length);
	};
	Set<Method> methods = MethodIntrospector.selectMethods(controllerType, selector);
	if (methods.size() == 1) {
		return methods.iterator().next();
	}
	else if (methods.size() > 1) {
		throw new IllegalArgumentException(String.format(
				"Found two methods named '%s' accepting arguments %s in controller %s: [%s]",
				methodName, Arrays.asList(args), controllerType.getName(), methods));
	}
	else {
		throw new IllegalArgumentException("No method named '" + methodName + "' with " + args.length +
				" arguments found in controller " + controllerType.getName());
	}
}
 
Example #4
Source File: ReflectionUtil.java    From stategen with GNU Affero General Public License v3.0 6 votes vote down vote up
/***不获取到Object中的method*/
public static void doWithMethods(Class<?> clazz, MethodCallback mc, MethodFilter mf) {
    // Keep backing up the inheritance hierarchy.
    Method[] methods = clazz.getDeclaredMethods();
    for (Method method : methods) {
        if (mf != null && !mf.matches(method)) {
            continue;
        }
        try {
            mc.doWith(method);
        }
        catch (IllegalAccessException ex) {
            throw new IllegalStateException("Not allowed to access method '" + method.getName() + "': " + ex);
        }
    }
    Class<?> superclass = clazz.getSuperclass();
    
    if (superclass != null && superclass!=Object.class) {
        doWithMethods(clazz.getSuperclass(), mc, mf);
    }
    else if (clazz.isInterface()) {
        for (Class<?> superIfc : clazz.getInterfaces()) {
            doWithMethods(superIfc, mc, mf);
        }
    }
}
 
Example #5
Source File: MvcUriComponentsBuilder.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private static Method getMethod(Class<?> controllerType, final String methodName, final Object... args) {
	MethodFilter selector = new MethodFilter() {
		@Override
		public boolean matches(Method method) {
			String name = method.getName();
			int argLength = method.getParameterTypes().length;
			return (name.equals(methodName) && argLength == args.length);
		}
	};
	Set<Method> methods = MethodIntrospector.selectMethods(controllerType, selector);
	if (methods.size() == 1) {
		return methods.iterator().next();
	}
	else if (methods.size() > 1) {
		throw new IllegalArgumentException(String.format(
				"Found two methods named '%s' accepting arguments %s in controller %s: [%s]",
				methodName, Arrays.asList(args), controllerType.getName(), methods));
	}
	else {
		throw new IllegalArgumentException("No method named '" + methodName + "' with " + args.length +
				" arguments found in controller " + controllerType.getName());
	}
}
 
Example #6
Source File: MvcUriComponentsBuilder.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
private static Method getMethod(Class<?> controllerType, final String methodName, final Object... args) {
	MethodFilter selector = new MethodFilter() {
		@Override
		public boolean matches(Method method) {
			String name = method.getName();
			int argLength = method.getParameterTypes().length;
			return (name.equals(methodName) && argLength == args.length);
		}
	};
	Set<Method> methods = MethodIntrospector.selectMethods(controllerType, selector);
	if (methods.size() == 1) {
		return methods.iterator().next();
	}
	else if (methods.size() > 1) {
		throw new IllegalArgumentException(String.format(
				"Found two methods named '%s' accepting arguments %s in controller %s: [%s]",
				methodName, Arrays.asList(args), controllerType.getName(), methods));
	}
	else {
		throw new IllegalArgumentException("No method named '" + methodName + "' with " + args.length +
				" arguments found in controller " + controllerType.getName());
	}
}
 
Example #7
Source File: ReflectionUtils.java    From anima with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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 targetClass 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 targetClass, MethodCallback mc, MethodFilter mf)
		throws IllegalArgumentException {

	// Keep backing up the inheritance hierarchy.
	do {
		Method[] methods = targetClass.getDeclaredMethods();
		for (int i = 0; i < methods.length; i++) {
			if (mf != null && !mf.matches(methods[i])) {
				continue;
			}
			try {
				mc.doWith(methods[i]);
			}
			catch (IllegalAccessException ex) {
				throw new IllegalStateException(
						"Shouldn't be illegal to access method '" + methods[i].getName() + "': " + ex);
			}
		}
		targetClass = targetClass.getSuperclass();
	}
	while (targetClass != null);
}
 
Example #8
Source File: SpringUtils.java    From onetwo with Apache License 2.0 5 votes vote down vote up
public static Set<Method> selectMethodsByParameterTypes(Class<?> targetClass, String targetMethod, Method sourceMethod) {
	Set<Method> methods = MethodIntrospector.selectMethods(targetClass, (MethodFilter)method -> {
		return method.getName().equals(targetMethod) && 
				method.getParameterCount()==sourceMethod.getParameterCount() &&
				Objects.deepEquals(method.getParameterTypes(), sourceMethod.getParameterTypes());
	});
	return methods;
}
 
Example #9
Source File: HandlerMethodSelector.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Select handler methods for the given handler type.
 * <p>Callers define handler methods of interest through the {@link MethodFilter} parameter.
 * @param handlerType the handler type to search handler methods on
 * @param handlerMethodFilter a {@link MethodFilter} to help recognize handler methods of interest
 * @return the selected methods, or an empty set
 * @see MethodIntrospector#selectMethods
 */
public static Set<Method> selectMethods(Class<?> handlerType, MethodFilter handlerMethodFilter) {
	return MethodIntrospector.selectMethods(handlerType, handlerMethodFilter);
}
 
Example #10
Source File: HandlerMethodSelector.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * Select handler methods for the given handler type.
 * <p>Callers define handler methods of interest through the {@link MethodFilter} parameter.
 * @param handlerType the handler type to search handler methods on
 * @param handlerMethodFilter a {@link MethodFilter} to help recognize handler methods of interest
 * @return the selected methods, or an empty set
 * @see MethodIntrospector#selectMethods(Class, MethodFilter)
 */
public static Set<Method> selectMethods(Class<?> handlerType, MethodFilter handlerMethodFilter) {
	return MethodIntrospector.selectMethods(handlerType, handlerMethodFilter);
}
 
Example #11
Source File: HandlerMethodSelector.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * Select handler methods for the given handler type.
 * <p>Callers define handler methods of interest through the {@link MethodFilter} parameter.
 * @param handlerType the handler type to search handler methods on
 * @param handlerMethodFilter a {@link MethodFilter} to help recognize handler methods of interest
 * @return the selected methods, or an empty set
 * @see MethodIntrospector#selectMethods(Class, MethodFilter)
 */
public static Set<Method> selectMethods(Class<?> handlerType, MethodFilter handlerMethodFilter) {
	return MethodIntrospector.selectMethods(handlerType, handlerMethodFilter);
}