Java: How to use non-primitive types for constructor.newInstance () in Java Reflection?

After a long day of searching, I still cannot figure out how I can create a new object from a self-contained class if the constructor takes non-primitive arguments. Now I'm starting to doubt whether this is possible at all ?!

In the Documentation of Reflection, they only talk about primitive types (e.g. int, float, boolean, etc.) as far as I saw, And all other information / website / seminar / example I found also just look at primitive types to find the constructor and create a new instance.

So what I want to do is the following (broken script):

Suppose I have a class called MyStupidClass. Another class (let its name be MyUsingClass) takes a MyStupidClass object as an argument in the constructor. Now in my main application, I want to be able to load the MyUsingClass class and initialize the object from the constructor.

Here is what I learned about "using Reflection":

With an empty constructor:

//the MyUsingClass:
public class MyUsingClass {

   //the public (empty) constructor
   public MyUsingClass() {
      ...
   }
}

In the main application, I would now do something like:

//get the reflection of the MyUsingClass
Class<?> myUsingClassClass = Class.forName("MyUsingClass");
//get the constructor [I use getConstructor() instead of getConstructors(...) here as I know there is only one]
Constructor<?> myUsingClassConstr = myUsingClassClass.getConstructor();
//instanciate the object through the constructor
Object myInstance = myUsingClassConstr.newInstance(null);

Now, if I were to use some primitive types in MyUsingClass , it would look something like this:

//the MyUsingClass:
public class MyUsingClass {

   //the public (primitive-only) constructor
   public MyUsingClass(String a, int b) {
      ...
   }
}

In the main application, the last line will change to something like:

//instanciate the object through the constructor
Object myInstance = myUsingClassConstr.newInstance(new Object[]{new String("abc"), new Integer(5)});

( , ...) , myInstance, MyUsingClass :

//the MyUsingClass:
public class MyUsingClass {

   //the public (primitive-only) constructor
   public MyUsingClass(String a, int b, MyStupidObject toto) {
      ...
   }
}

? ! , !

sv

+3
3

. .

  • Class.newInstance(), .
  • , getConstructor() newInstance().
  • getConstructor(), int.class, long.class, boolean.class .. . Foo Foo(int p),

    Constructor c = Foo.class.getConstructor(int.class);

  • newInstance():

    c.newInstance(12345);

multiargument - :

c.newInstace(new Object[] {12345, "foo", true})

, autoboxing java 5. 5 :

c.newInstace(new Object[] {new Integer(12345), "foo", new Boolean(true)})

. int.class Integer .

, .

+13

:

Object myInstance = myUsingClassConstr.newInstance(new Object[] {
    new String("abc"),
    new Integer(5),
    new MyStupidObject()
});

Constructor.newInstance :

, Constructor, , , . , , .

.

+1

, ( ) . , , . - , , !

...

[ , ]

0

Source: https://habr.com/ru/post/1777337/


All Articles