views:

62

answers:

2

how to get class from this expression java.util.List

+3  A: 

If your List is defined with a concrete type param, like for example:

private class Test {
   private List<String> list;
}

then you can get it via reflection:

Type type = ((ParameterizedType) Test.class.getDeclaredField("list")
     .getGenericType()).getActualTypeArguments()[0];

However, if the type is not known at compile time, it is lost due to type erasure

Bozho
+2  A: 

I assume you want to know the template class of the List at run time, and the short answer is: you can't. Java generics are used only at compile time: the template arguments are erased before byte code is generated. This is called "type erasure".

DJClayworth