Posted By:
Michael_Wax
Posted On:
Friday, June 15, 2001 03:12 PM
Here is something that gets you part of the way there for the numeric types: If the type string is the fully qualified class name (e.g., java.lang.Float), you can do the following:
Class c = Class.forName(type);
Class[] classArray = {String.class};
Constructor constructor = c.getDeclaredConstructor(classArray);
Object params = {yourString};
Object o = constructor.newInstance(params);
Note that you will still need to cast the object you get, possibly using a switch statement. Note also that I have ignored exception handling.
Unfortunately, this code will not work for java.sql.date, which does not have a constructor which takes a String argument.