1

So, I'm reading data from a file and depending on the text read, the program will use a constructor of the class that matches the parameters on the line. So the parameters given by the line of the text are stored into an ArrayList:

List<Object> parameters = new ArrayList<Object>();

Then I should somehow be able to create an object from those parameters, something like this:

constructor.newInstance(objects);

but I'm not quite sure how I could achieve that?

try {
            Class<?> objectClass = Class.forName("com.editor.object." +line.substring(4, from+4));
            Constructor[] allConstructors = objectClass.getDeclaredConstructors();
            for(Constructor constructor : allConstructors){
                Class<?>[] parameters = constructor.getParameterTypes();
                if(objects.size() == parameters.length){
                    for(int i = 0; i < parameters.length; i++){
                        if(objects.get(i).getClass().equals(parameters[i])){
                            if(i + 1 == parameters.length){
                                return constructor.newInstance(objects); //<-- This doesen't work, I have no idea how should I call the "random" constructor?
                            }
                        }
                    }
                }
            }
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

Edit 1: An example: I have the following line

new Platform(1, 1, 1, 1, 1);

-> Would create a new Platform object with parameters given. The referred class and constructor might be pretty much anything, so I can't rely on stuff like that. Of course I could just run that in code but I would like to learn more and that's why I won't do it the easiest way.

5
  • Activator.createInstance() might be useful. Commented Jul 29, 2015 at 22:01
  • I think that you'd want to use parameters[i].isAssignableFrom(objects.get(i).getClass()) instead of equals, since you don't necessarily need the exact same class. Commented Jul 29, 2015 at 22:09
  • What doesn't work about the above? Commented Jul 29, 2015 at 22:09
  • @AndyTurner Turner Added a comment in code to point the part that I don't know how to deal with. Commented Jul 29, 2015 at 22:15
  • @nautilus_rs - call constructor.newInstance(objects.toArray()). You are currently effectively invoking ctor.newInstance(new Object[] { objects }). Commented Jul 29, 2015 at 22:20

1 Answer 1

1

Constructor.newInstance is a variadic method: its type signature is Constructor.newInstance(Object... args). If you invoke it with the ArrayList as a single argument, that will be interpreted the same as:

Constructor.newInstance(new Object[] { objects })

because ArrayList is not an array type. This will fail unless that constructor happens to accept a single List parameter, and even then it could well fail because the elements in the list are not of the required type for that constructor.

Instead, you can invoke it as:

Constructor.newInstance(objects.toArray())

which "explodes" the list into the separate objects, from the point of view of invoking that constructor.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.