I am trying to create an instance of a class using the Constructor.newInstance () method, but I ran into a problem providing parameters correctly for the constructor. The problem is that the constructor options are made available as an array of String [], the elements of which I must use for their respective types. This works for objects, but what if some of the parameters are primitive types?
Here's a simplified example (which seems to work fine until I find a primitive type):
Class fooClass = Class.forName("Foo");
Constructor[] fooCtrs = fooClass.getConstructors();
Class[] types = fooCtrs[0].getParameterTypes();
Object[] params = new Object[types.length];
for(int i = 0; i < types.length; i++) {
params[i] = types[i].cast(args[i]);
}
As soon as I hit the int or something else, I will get a ClassCastException. Am I doing something wrong? Do I need to manually wrap any primitives I come across, or is there a built-in way to do this?
Wilco source
share