How can I instantiate a class instance in java with generics?
I am trying to read data from XML file and then instantiate an object so that I can add the other properties of the object (read from the XML file) into the object. I thought the easiest way (so that I don’t have to read the class method names and find the setters) would be to instantiate the object with all the values in the constructor.
I’m trying to do something like this so I need to have something like: T obj = new Object() but get the objects class
private static final boolean string_field = true;
private static <T> T getObject(Element e, String[] fieldNames, boolean[] fieldTypes) {
Object[] values = new Object[fieldNames.length];
for (int i=0; i<fieldNames.length; i++) {
values[i] = (fieldTypes[i]==string_field)? getStringValue(e, fieldNames[i])
: getIntegerValue(e, fieldNames[i]);
}
return new T(values);
}
Thanks for your advice.
EDIT:
This is my updated code (untested):
public static <T> List<T> populateObjectList(Document xmlDoc, String tagName,
Class clazz, String[] fieldNames, Class[] fieldTypes) {
List<T> objList = new ArrayList<T>();
NodeList nl = xmlDoc.getElementsByTagName(tagName);
if (nl!=null && nl.getLength()>0) {
for (int i=0; i<nl.getLength(); i++) {
Element e = (Element) nl.item(i);
T t;
try {
t = getObject(e, clazz, fieldNames, fieldTypes);
objList.add(t);
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (IllegalArgumentException ex) {
} catch (InvocationTargetException ex) {
} catch (NoSuchMethodException ex) {
}
}
}
return objList;
}
private static <T> T getObject(Element e, Class clazz, String[] fieldNames, Class[] fieldTypes)
throws InstantiationException, IllegalAccessException, IllegalArgumentException,
InvocationTargetException, NoSuchMethodException {
Object[] initargs = new Object[fieldNames.length];
for (int i=0; i<fieldNames.length; i++) {
initargs[i] = (fieldTypes[i].getName().equals("int"))?
getIntegerValue(e, fieldNames[i])
: getStringValue(e, fieldNames[i]);
}
return (T) clazz.getConstructor(fieldTypes).newInstance(initargs);
}
I posted this so that some of you would understand what I’m trying to do.
Thanks again everyone for their advice.
You cannot instantiate a generic type. Java generics have an annoying feature called type erasure, which basically means that the JVM doesn’t know what type was used for a generic class or method at runtime. This is for backwards compatibility with pre-generic versions of generic classes, such as collections.
What you can do is add a
Class<T> clazzto your parameter list, then replace the end of the method with this:This will throw an exception if the class doesn’t have a constructor that takes the correct type and number of arguments in the correct order.
CAVEAT: This only works if the constructor’s parameter types are exactly the same as the object types. For instance, if
valuesconsists of{ "Hello", new Integer(2) }, then this will only find a constructor with signatureSomeClass(String, Integer), not one with signatureSomeClass(Object, Object).