带有子类参数的 Java getMethod [英] Java getMethod with subclass parameter
问题描述
我正在编写一个使用反射来动态查找和调用方法的库.给定一个对象、一个方法名称和一个参数列表,我需要调用给定的方法,就好像方法调用是明确地写在代码中一样.
I'm writing a library that uses reflection to find and call methods dynamically. Given just an object, a method name, and a parameter list, I need to call the given method as though the method call were explicitly written in the code.
我一直在使用以下方法,该方法在大多数情况下都有效:
I've been using the following approach, which works in most cases:
static void callMethod(Object receiver, String methodName, Object[] params) {
Class<?>[] paramTypes = new Class<?>[params.length];
for (int i = 0; i < param.length; i++) {
paramTypes[i] = params[i].getClass();
}
receiver.getClass().getMethod(methodName, paramTypes).invoke(receiver, params);
}
但是,当参数之一是该方法支持的类型之一的子类时,反射 API 会抛出 NoSuchMethodException
.例如,如果接收者的类定义了 testMethod(Foo)
,则以下操作失败:
However, when one of the parameters is a subclass of one of the supported types for the method, the reflection API throws a NoSuchMethodException
. For example, if the receiver's class has testMethod(Foo)
defined, the following fails:
receiver.getClass().getMethod("testMethod", FooSubclass.class).invoke(receiver, new FooSubclass());
即使这有效:
receiver.testMethod(new FooSubclass());
我该如何解决这个问题?如果方法调用是硬编码的,则没有问题 - 编译器只是使用重载算法来选择要使用的最佳适用方法.但是,它不适用于反射,这正是我所需要的.
How do I resolve this? If the method call is hard-coded there's no issue - the compiler just uses the overloading algorithm to pick the best applicable method to use. It doesn't work with reflection, though, which is what I need.
提前致谢!
推荐答案
它比您开始时的要长一点,但是这满足了您的要求......此外还有一点 - 例如,callMethod(receiver,"voidMethod") 其中 voidMethod 不接受任何参数也有效.
It's a bit longer than what you started with, but this does what you asked for... and a little more besides - for example, callMethod(receiver, "voidMethod") where voidMethod takes no arguments also works.
static void callMethod(Object receiver,
String methodName, Object... params) {
if (receiver == null || methodName == null) {
return;
}
Class<?> cls = receiver.getClass();
Method[] methods = cls.getMethods();
Method toInvoke = null;
methodLoop: for (Method method : methods) {
if (!methodName.equals(method.getName())) {
continue;
}
Class<?>[] paramTypes = method.getParameterTypes();
if (params == null && paramTypes == null) {
toInvoke = method;
break;
} else if (params == null || paramTypes == null
|| paramTypes.length != params.length) {
continue;
}
for (int i = 0; i < params.length; ++i) {
if (!paramTypes[i].isAssignableFrom(params[i].getClass())) {
continue methodLoop;
}
}
toInvoke = method;
}
if (toInvoke != null) {
try {
toInvoke.invoke(receiver, params);
} catch (Exception t) {
t.printStackTrace();
}
}
}
这篇关于带有子类参数的 Java getMethod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!