0

I'm working on this program which takes input from user in form of "new id class arg0 arg1 …" (e.g. new obj1 String int:5 bool:true ...). After parsing this command, i need to create a new instance of the specified class and call its constructor with the "specified arguments". Now this is the part I've been stuck in, because all the examples i saw are like constructor.newInstance(String.class, bool.class) but in my case i'm getting the arguments in form of strings and i'm confused in how to convert them to that above form and call that specific constructor. Number of arguments are also not clear, so is there any easy solution to my problem? (making instance of the given class and calling constructor with the specified number of arguments) An example command and the action i need to perform is:

new x java.util.ArrayList int:5 --> x refers to “new ArrayList(5)”
3
  • You're going to have to be more specific. Give us an example of your input and what you expect it to do. Start by reading the javadoc of Class and Constructor. Commented Mar 9, 2014 at 17:40
  • Have you successfully parsed the whole thing? Commented Mar 9, 2014 at 17:43
  • yea i have parsed it to the point where i checked if the number of arguments is 0 then create the new object using Class c = Class.forName("class name here"); Object obj = c.newInstance(); Commented Mar 9, 2014 at 17:45

1 Answer 1

2

Once you have successfully parsed your string, you can use either the Class.getConstructor() or Class.getDeclaredConstructor() to fetch the constructor you want. The main difference between those two methods for your case is that Class.getDeclaredConstructor() will also allow you to call private constructors (anything declared in the source code, hence the name). Here is an example of your test case:

int argListLength = 1; // This should really be the number of parsed arguments
Class[] argumentTypes = new Class[argListLength];
Object[] argumentValues = new Object[argListLength];

// In reality you will want to do the following statement in a loop
// based on the parsed types
argumentTypes[0] = Integer.TYPE;

// In reality you will want to do the following statement in a loop
// based on the parsed values
argumentValues[0] = 5;

Constructor<ArrayList> constructor = null;
try {
    consrtuctor = java.util.ArrayList.class.getConstructor(argumentTypes);
} catch(NoSuchMethodException ex) {
    System.err.println("Unable to find selected constructor..."); // Display an error
    // return or continue would be nice here
}

ArrayList x = null;
try {
    x = constructor.newInstance(argumentValues);
} catch(InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
    System.err.println("Unable to call selected constructor..."); // Display an error
    // return or continue would be nice here
}

You may notice that there are a lot of things that can go wrong when calling a constructor. The only special one is InvocationTargetException, which wraps an exception that the successfully invoked constructor threw.

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.