IllegalArgumentException:Java Constructor.newInstance() 中的参数数量错误

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

IllegalArgumentException: wrong number of arguments in Java Constructor.newInstance()

javareflectionconstructorillegalargumentexception

提问by xandy

Consider the following code,

考虑以下代码,

public class StartUp {

    public StartUp(String[] test){}

    public static void main(String[] args) throws Exception{
        Constructor cd = StartUp.class.getConstructor(String[].class);
        System.out.println(cd.newInstance(new String[]{}).toString());
    }
}

What's wrong with it? I get the following Exception:

它出什么问题了?我收到以下异常:

Exception in thread "main" java.lang.IllegalArgumentException: wrong number of arguments at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at com.test.StartUp.main(StartUp.java:10)

线程“main”中的异常 java.lang.IllegalArgumentException: sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance0(Native Method) 处的参数数量错误newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at com.test.StartUp.main(StartUp.java:10)

回答by Jon Skeet

Your String[]is being implicitly converted to Object[]and taken as an empty array of arguments, instead of as a single argument which is an empty array. Try this:

String[]正在被隐式转换Object[]并作为一个空的参数数组,而不是作为一个空数组的单个参数。试试这个:

Object arg = new String[0];
System.out.println(cd.newInstance(arg).toString());

or

或者

System.out.println(cd.newInstance(((Object)new String[0]).toString());

or even avoid the compiler having to create the array for you at all:

甚至完全避免编译器必须为您创建数组:

System.out.println(cd.newInstance(new Object[] { new String[0] }).toString());

Basically this is a mixture of varargs handling and array covariance :(

基本上这是可变参数处理和数组协方差的混合:(

回答by simpatico

You could use dp4jverbose option to answer your question, and get the correct reflection code that you need:

您可以使用dp4j详细选项来回答您的问题,并获得您需要的正确反射代码:

$ vim ReflectedAcces.java
 class StartUp {

    private StartUp(String[] test){}

}

public class ReflectedAcces{

        @com.dp4j.InjectReflection
    public static void main(String[] args) throws Exception{
                StartUp su = new StartUp(new String[]{});
                System.out.println(su.toString());
    }

}


$ javac -cp dp4j-1.0-jar-with-dependencies.jar -Averbose=true ReflectedAcces.java 
...
ReflectedAcces.java:10: Note: 
class StartUp {

    private StartUp(String[] test) {
    }
}
public class ReflectedAcces {

    public ReflectedAcces() {
        super();
    }

    @com.dp4j.InjectReflection()
    public static void main(String[] args) java.lang.ClassNotFoundException, java.lang.NoSuchFieldException, java.lang.IllegalAccessException, java.lang.NoSuchMethodException, java.lang.reflect.InvocationTargetException, java.lang.InstantiationException {
        final java.lang.reflect.Constructor startUpConstructor = Class.forName("StartUp").getDeclaredConstructor(.java.lang.String[].class);
        startUpConstructor.setAccessible(true);
        StartUp su = (.StartUp)startUpConstructor.newInstance(new .java.lang.Object[1][]{new String[]{}});
        System.out.println(su.toString());
    }
}