I want to instantiate an object based on the type of the list(that is Truck) and add to the list, I try something below, but it warns me with "sun.reflect.generics.reflectiveObjects.TypeVariableImpl incompatible with java.lang.Class"
public <T extends Car> void addRow(List<T> list) {
Class<T> clazz = (Class<T>) ((ParameterizedType) list.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
T newInstance = clazz.newInstance();
list.add(newInstance);
}
method call:
List<Truck> abc = new ArrayList<Truck>;
addRow(abc);
any help? Tks all in advance!