反射时Java参数类型不匹配
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24387242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java argument type mismatch while reflection
提问by Artemkller545
I am trying to run some method I load from another jar, that returns an integer, and then I want to return it to another class and pass it to my logic.
我正在尝试运行从另一个 jar 加载的一些方法,该方法返回一个整数,然后我想将其返回到另一个类并将其传递给我的逻辑。
I made my method loading class pretty simple like this:
我使我的方法加载类非常简单,如下所示:
public class ModuleLoader {
private Class<?> cls;
public void initializeCommandModule(Module m) throws Exception {
URL url = this.getURL(m.getJar());
this.cls = this.loadClass(m.getMainClass(), url);
}
public int execute(Module m, ArrayList<String> args) throws Exception {
Method method = this.cls.getDeclaredMethod("execute", ArrayList.class);
return (int) method.invoke(this.cls.newInstance(), 1);
}
public int respond(ArrayList<String> args) throws Exception {
Method method = this.cls.getDeclaredMethod("response", ArrayList.class);
return (int) method.invoke(this.cls.newInstance(), 1);
}
private Class<?> loadClass(String cls, URL url) throws ClassNotFoundException, IOException {
URLClassLoader loader = new URLClassLoader(new URL[]{url});
Class<?> toReturn = loader.loadClass(cls);
loader.close();
return toReturn;
}
private URL getURL(String jar) throws MalformedURLException {
return new File(jar).toURI().toURL();
}
}
Take a look at the execute(Module m, ArrayList<String> args)method, this line throws the error:
看一下execute(Module m, ArrayList<String> args)方法,这行抛出错误:
return (int) method.invoke(this.cls.newInstance());
The jar library I load looks like this:
我加载的 jar 库如下所示:
public class Test {
public int execute(ArrayList<String> i) {
System.out.println("Hello world!");
return 0;
}
}
Why when I run that method I get the following exception thrown?
为什么当我运行该方法时会抛出以下异常?
Exception in thread "main" java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at ben.console.modules.ModuleLoader.execute(ModuleLoader.java:24)
at ben.console.CommandProcessor.process(CommandProcessor.java:37)
at ben.console.Console.listen(Console.java:25)
at ben.console.Console.main(Console.java:31)
Thanks in advice!
谢谢指教!
采纳答案by Mena
You forgot to pass an argument to your method invocation.
您忘记将参数传递给您的方法调用。
return (int) method.invoke(this.cls.newInstance(), myArrayList);
You can also invoke with a null argument:
您还可以使用空参数进行调用:
return (int) method.invoke(this.cls.newInstance(), (Object[])null);
回答by Elliott Frisch
In your execute()method, you have this
在你的execute()方法中,你有这个
return (int) method.invoke(this.cls.newInstance());
I think you wanted
我想你想要
return (int) method.invoke(this, args);
Likewise with respond(),
同样respond(),
Method method = this.cls.getDeclaredMethod("response", ArrayList.class);
return (int) method.invoke(this.cls.newInstance(), 1);
should be
应该
return (int) method.invoke(this, args);
You may find my blog post herehelpful.
你可能会发现我的博客文章在这里很有帮助。
回答by AlexR
As far as I understand you fetch method that accepts one argument of type ArrayList:
据我了解,您 fetch 方法接受一个类型的参数ArrayList:
Method method = this.cls.getDeclaredMethod("execute", ArrayList.class);
But then try to invoke it with argument of type that I cannot recognize that is obviously not ArrayList
但是然后尝试使用我无法识别的类型参数来调用它,这显然不是 ArrayList
(int) method.invoke(this.cls.newInstance());
What is the type of this.cls.newInstance()? Value of clsis assigned in method initializeCommandModule()as following:
的类型是this.cls.newInstance()什么?的值cls在方法中赋值initializeCommandModule()如下:
this.cls = this.loadClass(m.getMainClass(), url);
this.cls = this.loadClass(m.getMainClass(), url);
where mis Module. I have no idea what Moduleis and therefore do not know what does getMainClass()return.
哪里m是Module。我不知道是什么Module,因此不知道getMainClass()返回什么。
回答by jyllana24
You can also try to convert it into integer. I am having the same issue and I have been pulling off my hair. Using Integer.parseInt it did the trick. I hope this will help.
您也可以尝试将其转换为整数。我有同样的问题,我一直在拔头发。使用 Integer.parseInt 它做到了。我希望这将有所帮助。
Here is a sample code:
这是一个示例代码:
String num = "1";
int result = Integer.parseInt(num);
System.out.println(result);

