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

发布于 2024-10-19 10:54:38 字数 828 浏览 1 评论 0原文

考虑下面的代码,

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());
    }
}

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

线程“main”中出现异常 java.lang.IllegalArgumentException:参数数量错误 在 sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法) 在 sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) 在 sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) 在 java.lang.reflect.Constructor.newInstance(Constructor.java:513) 在 com.test.StartUp.main(StartUp.java:10)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感性 2024-10-26 10:54:38

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

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

或者

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

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

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

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

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:

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 :(

平定天下 2024-10-26 10:54:38

您可以使用 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());
    }
}

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

$ 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());
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文