简体   繁体   English

如何使用反射调用内部类的构造函数

[英]How to invoke constructor of inner class using reflection

I want to create an object of inner class using reflection. 我想使用反射创建内部类的对象。 This is my class: 这是我的课:

//outer class
public final class EMSToCompMessages {
private EMSToCompMessages() {}
//inner class
public static final class CompTypeConfig extends
{
    private CompTypeConfig(com.google.protobuf.GeneratedMessage.Builder<?> builder) {
        super(builder);
        this.unknownFields = builder.getUnknownFields();
    }
    private CompTypeConfig(boolean noInit) { 
        this.unknownFields = com.google.protobuf.UnknownFieldSet.getDefaultInstance(); 
    }

    private static final CompTypeConfig defaultInstance;
    public static CompTypeConfig getDefaultInstance() {
        return defaultInstance;
    }
}

I have to invoke inner class CompTypeConfig using reflection. 我必须使用反射调用内部类CompTypeConfig I am getting following exception: 我收到以下异常:

java.lang.IllegalArgumentException: wrong number of arguments java.lang.IllegalArgumentException:参数数量错误

What am I doing wrong? 我究竟做错了什么?
I am using following code: 我正在使用以下代码:

Class<?> loadedMyClass = Class.forName("EMSToCompMessages", true, loader);
        Constructor constructor = loadedMyClass.getDeclaredConstructor();
        constructor.setAccessible(true);
        Object obj = constructor.newInstance();
        Class[] innerClass = loadedMyClass.getDeclaredClasses();
        for (Class<?> getClass : innerClass) {
             Constructor ctor = getClass.getDeclaredConstructors()[0];
                System.out.println(ctor.getName());
                ctor.setAccessible(true);
                Object innerObj = ctor.newInstance(obj);// Exception is coming here
        }             

You try to create a object of CompTypeConfig type which has no a constructor without parameters. 您尝试创建一个CompTypeConfig类型的对象,该对象没有没有参数的构造函数。 CompTypeConfig has two constructors with com.google.protobuf.GeneratedMessage.Builder<?> builder and boolean noInit params. CompTypeConfig具有两个构造函数,其中包含com.google.protobuf.GeneratedMessage.Builder<?> builderboolean noInit参数。 You have to pass some param for them, when create object. 创建对象时,您必须为它们传递一些参数。 Also as I understood, you pass incorrect parameter type for constructor. 另外,据我了解,您为构造函数传递了错误的参数类型。

You're apparently trying to call 您显然正在尝试致电

Object innerObj = constructor.newInstance(obj); //you wrote constru, which isn't defined

constructor.newInstance(obj) will try to create an instance of EMSToCompMessages class, not an inner class. constructor.newInstance(obj)将尝试创建EMSToCompMessages类的实例,而不是内部类。

Anyway, the Javadoc says (about the getConstructor method) 无论如何,Javadoc说(关于getConstructor方法)

Returns a Constructor object that reflects the specified public constructor of the class represented by this Class object. 返回一个构造函数对象,该对象反映此Class对象表示的类的指定公共构造函数。 The parameterTypes parameter is an array of Class objects that identify the constructor's formal parameter types, in declared order. parameterTypes参数是Class对象的数组,这些Class对象按声明的顺序标识构造函数的形式参数类型。 If this Class object represents an inner class declared in a non-static context, the formal parameter types include the explicit enclosing instance as the first parameter. 如果此Class对象表示在非静态上下文中声明的内部类,则形式参数类型包括显式的封闭实例作为第一个参数。

So, you will first have to make your inner class constructors public. 因此,您首先必须将您的内部类构造函数公开。 Calling setAccessible(true) is not enough in this case. 在这种情况下,仅调用setAccessible(true)是不够的。

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

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