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.
4 Answers 4
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> clazz
to your parameter list, then replace the end of the method with this:
Class<?>[] paramTypes = new Class[values.length];
for (int i = 0; i < paramTypes.length; i++) {
paramTypes[i] = values[i].getClass();
}
return clazz.getConstructor(paramTypes).newInstance(values);
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 values
consists of { "Hello", new Integer(2) }
, then this will only find a constructor with signature SomeClass(String, Integer)
, not one with signature SomeClass(Object, Object)
.
6 Comments
clazz.getConstructors()[0].newInstance(values)
. I'll have to make sure I use the first constructor correctly. I don't understand what your Object[].class does?getConstructor
, you have to specify which constructor you want, and the JVM can only tell constructors apart by their signatures. So you pass an (implicit) array of Class
objects corresponding to the constructor's argument types, and it gets you that constructor. Here, it looks like you want a constructor that takes a single Object[]
as an argument. Object[].class
is a class literal; it represents the Class
object that corresponds to type Object[]
.How would you even know that T
contained a constructor that took a single Object[]
argument? The way to fix this is to pass in an abstract factory.
interface ThingFactory { // Choose an appropriate name.
T create(Object[] values);
}
private static <T> T getObject(
ThingFactory<T> factory, Element e, String[] fieldNames, boolean[] fieldTypes
) {
...
return factory.create(values);
}
Now T
might not even have a rather strange Object[]
constructor. T
might be an abstract type. The factory might return a different type dependent upon the arguments. Perhaps sometimes these products are immutable objects that can be cached.
3 Comments
Uses the constructor represented by this Constructor object to create and initialize a new instance of the constructor's declaring class, with the specified initialization parameters.
You can't do new T()
because at runtime, T
is effectively just Object
. The compiler uses generics for type-checking (e.g. you can't put an Integer
into a List<String>
), but then throws the generics information away. There's one compiled copy of the method for all types of arguments, so there's no way for it to know which type you expect it to instantiate.
Read about type erasure for more information.
Comments
I dont really see your problem, is it not possible to use obj.getClass ?
2 Comments
getObject(..., Class clazz)
then tried to get field names and field types but I got stuck there so I thought it would be easier to pass the field names which I want to set the values of from the XML file
T
is supposed to be? Is it determined somehow based on theElement
? If so, perhaps aMap<String, Class<?>>
mapping element names to classes is what you want; then you can construct the object as per Taymon's answer. Alternatively, perhaps yourgetObject()
method should take aClass<T>
as an argument.