0

I need to invoke a constructor using reflection to instantiate a class to produce multiple objects.

Here is what I am doing at the moment to create these objects:

for(int i = 0;i < num_objects;i++)
{
    User A = new User(null, null, null);            
    loaded_data.add(A);             
}

However, I wish to assume that I don't know what the constructor for User actually is, and therefore would like to use reflection to create x number of objects using null parameters so that they may be populated with their setters later.

Note loaded_data is an array of objects of type User.

I can invoke the Setters ok so defining the objects later is no problem, I'm just not sure how to create these objects using a constructor if we assume we don't know how many parameters the constructor takes.

George Stocker
  • 57,289
  • 29
  • 176
  • 237
Single Entity
  • 2,925
  • 3
  • 37
  • 66
  • Look at this question - http://stackoverflow.com/questions/3671649/java-newinstance-of-class-that-has-no-default-constructor – Eran Nov 17 '14 at 12:20
  • look at reflection - http://tutorials.jenkov.com/java-reflection/index.html – No Idea For Name Nov 17 '14 at 12:25
  • I am aware of this thank you @Eran, however in this case it assumes you know the number of arguments and their primitive type associated with the constructor. I would like to assume we don't know this. – Single Entity Nov 17 '14 at 12:26
  • @NoIdeaForName your suggestion I am also aware of thank you, but again in this case we also assume the parameters associated with the constructor are known. I could really do with an example of a case where it is assumed the parameters and their types are not known and null values are passed (as in the question). Thanks anyway – Single Entity Nov 17 '14 at 12:33
  • Excuse me, this question has NOT been asked before - yet again let me emphasise that this solution in the link assumes we know the constructor arguments, I don't understand why it has been marked as a possible duplicate, it isn't. – Single Entity Nov 17 '14 at 12:48
  • @SingleEntity I didn't mark it as duplicate, I just suggested what you are looking for is similar. All you need is to locate the constructor you wish to use and find how many parameters it takes. See my answer. – Eran Nov 17 '14 at 13:00
  • @Eran,thank you for your answer. I was pre-occupied with the suggestion it was a duplicate question, something trivial to one person isn't necessarily trivial to another so small differences can be important. Thanks again for your help, I'm just looking at it now. – Single Entity Nov 17 '14 at 13:06

1 Answers1

0

Well, here's a simple example of creating a String with reflection. It throws a NullPointerException, though, since that what happen when you execute new String(null).

 Constructor cons = String.class.getConstructors ()[1]; // I chose the 2nd constructor
                                                        // arbitrarily
 Class[] params = cons.getParameterTypes ();
 System.out.println ("Number of params: " + params.length);
 Object[] cargs = new Object[params.length]; // array of nulls to pass to the 
                                             // constructor
 try {
   Object obj = cons.newInstance (cargs);
 } catch (IllegalArgumentException e1) {
   System.out.println (e1);
   e1.printStackTrace();
 } catch (InstantiationException e1) {
   System.out.println (e1);
   e1.printStackTrace();
 } catch (IllegalAccessException e1) {
   System.out.println (e1);
   e1.printStackTrace();
 } catch (InvocationTargetException e1) {
   System.out.println (e1);
   e1.printStackTrace();
 }

This can only work if all the constructor's parameters are reference types and the constructor can handle nulls as input. That said, if you are going to created an instance with null values, wouldn't it make more sense to use the parameter-less constructor, and create the instance with class.newInstance()?

Eran
  • 387,369
  • 54
  • 702
  • 768
  • You can condense all of those catch-blocks into one: `catch (ReflectiveOperationException e)`. – VGR Nov 18 '14 at 22:24