java 当构造函数将字符串数组作为参数时,使用反射创建对象实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25610249/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 08:20:12  来源:igfitidea点击:

creating instance of object using reflection , when the constructor takes an array of strings as argument

javareflection

提问by a curious engineer

I am trying to create an instance of a class which has only the following constructor, overwriting the default constructor

我正在尝试创建一个只有以下构造函数的类的实例,覆盖默认构造函数

public HelloWorld(String[] args)

I am doing the following

我正在做以下事情

Class reflect;
HelloWorld obj = null;
//some logic to generate the class name with full path
reflect = Class.forName(class_name);

Then I am trying to create an object for this class

然后我试图为这个类创建一个对象

 obj = (HelloWorld)reflect.getConstructor(String[].class)
                          .newInstance(job1.arg_arr());

arg_arr()is for converting a list to an array of strings

arg_arr()用于将列表转换为字符串数组

public String[] arg_arr(){
    String arg_list[]=new String[args.size()];
    return args.toArray(arg_list);
}

I get the following stack trace when trying to create the instance java.lang.IllegalArgumentException:

尝试创建实例时,我得到以下堆栈跟踪 java.lang.IllegalArgumentException

wrong number of arguments

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)  
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)  
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)  
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)  
at processmigration.Process_manager.eval(Process_manager.java:175)  
at processmigration.Process_manager.run(Process_manager.java:147)  
at java.lang.Thread.run(Thread.java:745)

I wonder what is going wrong since I am passing only one argument to newInstance() just like the constructor of the class I am trying to create.

我想知道出了什么问题,因为我只向 newInstance() 传递一个参数,就像我试图创建的类的构造函数一样。

回答by Peter Lawrey

newInstancetakes an Object...argument so when you give it a String[] it passes it as the Object[].

newInstance接受一个Object...参数,所以当你给它一个 String[] 时,它会将它作为Object[].

What you want is the following which tells it you are passing just one argument, not the contents of the array as arguments.

您想要的是以下内容,它告诉它您只传递一个参数,而不是将数组的内容作为参数传递。

.newInstance((Object) job1.arg_arr())

回答by Dean Leitersdorf

The solution is as follows:

解决方法如下:

When performing getConstructor(...) and newInstance (...), all your arguments must be inside 1 array. Therefore, I created the arrays params and argsToPass and stored your args in them. Otherwise, it would thing your String[] is a list of arguments and not just 1 argument.

执行 getConstructor(...) 和 newInstance (...) 时,所有参数都必须在 1 个数组内。因此,我创建了数组 params 和 argsToPass 并将您的 args 存储在其中。否则,您的 String[] 会是一个参数列表,而不仅仅是 1 个参数。

Class reflect;
    HelloWorld obj = null;
    //some logic to generate the class name with full path
    reflect = HelloWorld.class;
    Class[] params = new Class[1];
    params[0] = String[].class;
    Object[] argsToPass = new Object[1];
    argsToPass[0] = job1.arg_arr();
    obj = (HelloWorld)reflect.getConstructor(params).newInstance(argsToPass);

EDIT: Tested code - works!!

编辑:经过测试的代码 - 有效!!