JAVA getConstructor 抛出 NoSuchMethodException [英] JAVA getConstructor throws NoSuchMethodException

查看:31
本文介绍了JAVA getConstructor 抛出 NoSuchMethodException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 JAVA 新手我正在努力学习 reflection.我想获得特定的构造函数(选择示例表单 here)来自我的班级:

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 :

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

我在尝试实例化 ctor

我在这里错过了什么?

推荐答案

工作示例:

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

请注意,方法 getConstructor 实际上不接受数组.它的定义如下:

Note that method getConstructor actually doesn't take an array. It's defined like:

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.

您最初所做的相当于:

    Constructor<Test> constructor = Test.class.getConstructor(String.class.getClasses());
    constructor.newInstance("Hello");

但是,String.class.getClasses() 返回什么?好问题!让我们开始调试:

But, what does String.class.getClasses() return? Good question! Lets go debug:

    Class<?>[] classes = String.class.getClasses();
    System.out.println(classes.length); // prints 0

有一个关于 getClasses() 的文档:https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getClasses.检查它,你会发现为什么会这样.

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.

为了完整起见.超级原创问题(编辑前)还包含一个构造函数 - 一个非参数构造函数:

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

这里发生的问题是抛出了IllegalArgumentException.这是因为即使 String.class.getClasses() 返回一个空数组,实际上有符合条件的构造函数 - 一个非参数构造函数!它没有任何参数,并且 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't accept any arguments.

这篇关于JAVA getConstructor 抛出 NoSuchMethodException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆