Passing the Class<T> in java of a generic list?
You can use Bozho's solution, or avoid the creation of a temporary array list by using:
Class<List<MyClass>> clazz = (Class) List.class;
The only problem with this solution is that you have to suppress the unchecked warning with @SuppressWarnings("unchecked")
.
You can't. You'd have to use unsafe cast:
Class<List<MyClass>> clazz =
(Class<List<MyClass>>) new ArrayList<MyClass>().getClass();
As a follow up to this, I found this in the Gson docs.
Type listType = new TypeToken<List<String>>() {}.getType();
Which solves the problem of getting the type safely but the TypeToken class is specific to Gson.