I tried to employ the code given in this answer. I did this:
public class ListClass<T> {
private class ObjectFactory<T> {
//This should the the class of generic type T which is what I need
public final Class<T> cls;
@SuppressWarnings ("unchecked")
public GameObjectFactory()
{
//This three lines are from the linked answer - I don't really understand them
Type type = getClass().getGenericSuperclass();
ParameterizedType paramType = (ParameterizedType) type; //java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
cls = (Class<T>) paramType.getActualTypeArguments()[0];
}
public T createNew(... some parameters ...) {
return (T)new Expression(cls, "new", new Object[]{... some parameters ...}).getValue();
}
}
public final ObjectFactory<T> factory;
public generateItem() {
//let add() be method that adds item of type T to this list
this.add(factory.createNew(... some parameters ...));
}
}
What am I dong wrong?
Aucun commentaire:
Enregistrer un commentaire