JAVA getConstructor 抛出 NoSuchMethodException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29195039/
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 getConstructor throws NoSuchMethodException
提问by LordTitiKaka
I'm new in JAVA and I'm trying to learn reflection. I want to get specific constructor (picking the example form here) from my class :
我是 JAVA 新手,我正在尝试学习反射。我想从我的班级中获取特定的构造函数(在此处选择示例表单):
public class Example1 {
public Example1() {
}
public Example1(int i) {
}
public Example1(String s) {
System.out.println("using param = " + s);
}
public static void main(String[] args) throws Exception {
Class<?>[] paramTypes = String.class.getClasses();
Constructor<Example1> ctor = Example1.class.getConstructor(paramTypes);
ctor.newInstance("test");
}
}
I get NoSuchMethodException
when trying to instantiate ctor
我NoSuchMethodException
在尝试实例化时得到ctor
What am I missing here?
我在这里错过了什么?
回答by tzima
Working example:
工作示例:
import java.lang.reflect.Constructor;
public class Test {
public Test(String str) {
System.out.println(str);
}
public Test(int a, int b) {
System.out.println("Sum is " + (a + b));
}
public static void main(String[] args) throws Exception {
Constructor<Test> constructorStr = Test.class.getConstructor(String.class);
constructorStr.newInstance("Hello, world!");
Constructor<Test> constructorInts = Test.class.getConstructor(int.class, int.class);
constructorInts.newInstance(2, 3);
}
}
Note that method getConstructor
actually doesn't take an array. It's defined like:
请注意,该方法getConstructor
实际上并不采用数组。它的定义如下:
public Constructor<T> getConstructor(Class<?>... parameterTypes) {
... meaning that it accepts variable amount of arguments which should have been passed as I did. Passing an array is possible too, but it's not necessary.
...意味着它接受可变数量的参数,这些参数应该像我一样传递。传递数组也是可能的,但这不是必需的。
What you've done originally was equivalent to:
你最初所做的相当于:
Constructor<Test> constructor = Test.class.getConstructor(String.class.getClasses());
constructor.newInstance("Hello");
But, what does String.class.getClasses()
return? Good question! Lets go debug:
但是,String.class.getClasses()
返回的是什么?好问题!让我们去调试:
Class<?>[] classes = String.class.getClasses();
System.out.println(classes.length); // prints 0
There's a documentation about getClasses()
: https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getClasses. Check it and you'll find out the reason why it's so.
有一个关于getClasses()
:https: //docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getClasses的文档。检查它,你会发现为什么会这样。
For the sake of completeness. The super-original-question (before edits) contained one more constructor - a non-parametric one:
为了完整性。超级原创问题(编辑前)还包含一个构造函数 - 一个非参数构造函数:
import java.lang.reflect.Constructor;
public class Example1 {
public Example1() {
}
public Example1(String s) {
System.out.println("using param = " + s);
}
public static void main(String[] args) throws Exception {
Constructor<Example1> ctor = Example1.class.getConstructor(String.class.getClasses());
ctor.newInstance("test");
}
}
The problem which occurs here is IllegalArgumentException
being thrown. It's because even though String.class.getClasses()
returns an empty array, there actually is constructor which matches the criteria - a non-parametric constructor! It doesn't have any arguments and the array returned by String.class.getClasses()
doesn't contain anything too. This means that constructor is successfully found, but when trying to instantiate it using ctor.newInstance("test")
, it fails because the found constructor doesn'taccept any arguments.
这里发生的问题IllegalArgumentException
正在被抛出。这是因为即使String.class.getClasses()
返回一个空数组,实际上也有符合条件的构造函数 - 一个非参数构造函数!它没有任何参数,并且返回的数组String.class.getClasses()
也不包含任何内容。这意味着成功找到了构造函数,但是当尝试使用 实例化它时ctor.newInstance("test")
,它失败了,因为找到的构造函数不接受任何参数。
回答by flogy
Try it using
尝试使用
Constructor<example1> ctor = examTesting.getConstructor(new Class[]{String.class});
This will return the constructor with one parameter of type String. Your method might return multiple values and therefore a constructor with multiple parameter is searched but not found -> NoSuchMethodException is thrown.
这将返回带有一个 String 类型参数的构造函数。您的方法可能会返回多个值,因此会搜索具有多个参数的构造函数但未找到 -> 抛出 NoSuchMethodException。
More information about getClasses(): http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Class.html#getClasses%28%29
有关 getClasses() 的更多信息:http: //docs.oracle.com/javase/1.5.0/docs/api/java/lang/Class.html#getClasses%28%29