下面列出了怎么用org.springframework.util.ReflectionUtils.MethodFilter的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 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;
}
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());
}
}
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());
}
}
/***不获取到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);
}
}
}
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());
}
}
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());
}
}
/**
* 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);
}
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;
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}