Java 不带参数的getConstructor

Java 不带参数的getConstructor,java,getconstructor,Java,Getconstructor,对于没有参数的构造函数,我似乎无法使用getConstructor 我一直得到以下例外情况: java.lang.NoSuchMethodException: classname.<init>() java.lang.NoSuchMethodException:类名。() 代码如下: interface InfoInterface { String getClassName(); String getMethodName(); String getArgu

对于没有参数的构造函数,我似乎无法使用
getConstructor

我一直得到以下例外情况:

java.lang.NoSuchMethodException: classname.<init>()
java.lang.NoSuchMethodException:类名。()
代码如下:

interface InfoInterface {
    String getClassName();
    String getMethodName();
    String getArgument();
}

class asa implements InfoInterface {
    @Override
    public String getClassName() {
        return ("jeden");
    }
    @Override
    public String getMethodName() {
        return ("metoda");
    }
    @Override
    public String getArgument() {
        return ("krzyk");
    }
}

class Jeden {
    Jeden() {
        System.out.println("konstruktor");
    }

    public void Metoda(String s) {
        System.out.println(s);
    }
}

class Start {
    public static void main(String[] argv) {
        if (argv.length == 0) {
            System.err.println("Uzycie programu: java Start nazwa_klasy nazwa_klasy2...");
            return;
        }

        try {
            for (int x = 0; x < argv.length; x++) {
                Class<?> c = Class.forName(argv[x]);
                InfoInterface d = (InfoInterface) c.newInstance();
                String klasa = d.getClassName();
                String metoda = d.getMethodName();
                String argument = d.getArgument();

                Class<?> o = Class.forName(klasa);
                // o.newInstance();

                Constructor<?> oCon = o.getConstructor();
                System.out.println("ASD");
                Class<?> p = (Class<?>) oCon.newInstance();
            }
        } catch (Exception e) {
            System.out.println(e);
        }

    }
}
接口信息接口{
字符串getClassName();
字符串getMethodName();
字符串getArgument();
}
类asa实现了InfoInterface{
@凌驾
公共字符串getClassName(){
返回(“杰登”);
}
@凌驾
公共字符串getMethodName(){
回报(“梅托达”);
}
@凌驾
公共字符串getArgument(){
返回(“krzyk”);
}
}
杰登班{
杰登(){
System.out.println(“konstruktor”);
}
公共空间梅托达(字符串s){
系统输出打印项次;
}
}
开课{
公共静态void main(字符串[]argv){
如果(argv.length==0){
System.err.println(“Uzycie程序:java启动nazwa_klasy nazwa_klasy2…”);
返回;
}
试一试{
对于(int x=0;x

o.newInstance()
毫无问题地打印“konstruktor”

似乎您的类提供了一个不是默认构造函数的构造函数。不带参数的getConstructor()调用要求类具有默认构造函数。下面的测试说明了这一点

import org.junit.Test;

public class ConstructorTest {
    public static class ClassWithParameterizedConstructor {
        public ClassWithParameterizedConstructor(final String param) {
            // A parameterized constructor, no default constructor exists
        }
    }

    @Test
    public void testFoo() throws NoSuchMethodException {
        // Parameterized constructor lookup works fine
        ClassWithParameterizedConstructor.class.getConstructor(String.class);

        // This doesn't work since there is no default constructor
        ClassWithParameterizedConstructor.class.getConstructor();
    }
}

因此,一种可能的解决方案是更改对getConstructor()的调用以包含正确的类型,或者在对象本身上提供默认构造函数(但为什么要这样做?)。

阅读以下内容时,问题就显而易见了:

返回一个构造函数对象,该对象反映由该类对象表示的类的指定的public构造函数

我的

在您的代码中,构造函数不是公共的

例如:

// Note: class is NOT public -- its default constructor won't be either
final class Test
{
    public static void main(final String... args)
        throws NoSuchMethodException
    {
        // throws NoSuchMethodException
        Test.class.getConstructor();
    }
}
强制性,这也为JLS提供了参考。特别要注意的是,默认构造函数与类具有相同的访问修饰符。

阅读以下内容:

看起来类和构造函数都有方法newInstance。区别在于,在类中,您只能在没有参数的情况下调用newInstance,因此被调用的构造函数必须有一个无参数(当您有多个构造函数时,这也会带来问题)。 构造函数类中的MethodoE newInstance还允许您使用参数调用构造函数,请注意,您还可以使用方法getConstructors而不是getConstructor,它返回所有类构造函数,并允许您调用所需的构造函数方法


在本例中,由于您只有一个构造函数,并且没有参数,Class.newInstance可以正常工作。要使用getConstructor获得相同的结果,需要在end-oCon.newInstance()中添加

您可以使用
getDeclaredConstructors()
,它返回一个构造函数对象数组,反映由该类对象表示的类声明的所有构造函数

class-SomeClass{
{
System.out.println(“我在这里!”);
}
}
公共班机{
公共静态void main(字符串[]args)引发异常{
System.out.println(Arrays.toString(SomeClass.class.getDeclaredConstructors());
//返回公共、受保护、默认(包)访问和私有构造函数
//System.out.println(SomeClass.class.getConstructor());
//在这种情况下,您得到:
//NoSuchMethodException:reflection.SomeClass。()
//因为某些类没有公共构造函数
for(构造函数:SomeClass.class.getDeclaredConstructors()){
构造函数。newInstance();
}
}
}
如果你有这样的私有构造函数:

class-SomeClass{
私有类(字符串val){
系统输出打印项次(val);
}
}
必须为构造函数设置可访问性:

constructor.setAccessible(true);
然后得到这样的结果:

class-SomeClass{
私有类(字符串val){
系统输出打印项次(val);
}
}
公共班机{
公共静态void main(字符串[]args)引发异常{
for(构造函数:SomeClass.class.getDeclaredConstructors()){
//constructor.newInstance(“某些参数”);//java.lang.IllegalAccessException
constructor.setAccessible(true);
constructor.newInstance(“某些参数”);
}
}
}
注意:如果您的类声明为
private
,则其默认构造函数也必须是
private

对于接收外部类实例的非静态内部类要小心。在这种(有些复杂的)场景中,实际上可以通过替换以下内容来获得(非公共)构造函数:

Constructor<?> oCon = o.getConstructor();
Constructor-oCon=o.getConstructor();

构造函数oCon=o.getDeclaredConstructor();

Jeden
类(及其构造函数)的“默认”可见性使
Start
类可以访问它,因为它是在同一个包中定义的。

类o中有任何构造函数吗?默认情况下,有一个无参数构造函数,但只要您声明一个具有参数的构造函数,那么您就需要显式声明一个无参数构造函数,它只有一个无参数构造函数。当我换衣服的时候它就工作了
Constructor<?> oCon = o.getDeclaredConstructor();