简体   繁体   中英

How to instantiate class in Java where number of parameters in constructor of class under test is unknown through Reflection?

Using Reflection in Java, I want to instantiate a class where number of parameter in constructor is unknown. Can it be done?

 Constructor[] ctors = Test1.class.getDeclaredConstructors();
 for (Constructor constructor : ctors) {
        System.out.println(constructor);
 }

If you test you will see that all constructors are showed, with there parameters and which object you need to pass.

To have the paramets types do:

 Class[] clz = constructor.getParameterTypes();

So I guess the answer is Yes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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