简体   繁体   English

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

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

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)

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.您的String[]被隐式转换为Object[]并作为一个空的参数数组,而不是作为一个空数组的单个参数。 Try this:尝试这个:

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 :(基本上这是可变参数处理和数组协方差的混合:(

You could use dp4j verbose 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());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM