Because of Type Erasure
, you will not be able to get the type(in case of Empty List).As JLS says on Type Erasure :
4.6. Type Erasure
Type erasure is a mapping from types (possibly including parameterized types and type variables) to types (that are never parameterized types or type variables). We write |T| for the erasure of type T. The erasure mapping is defined as follows:
The erasure of a parameterized type (§4.5) G<T1,...,Tn> is |G|. The erasure of a nested type T.C is |T|.C. The erasure of an array type T[] is |T|[]. The erasure of a type variable (§4.4) is the erasure of its leftmost bound. The erasure of every other type is the type itself.
Type erasure also maps the signature (§8.4.2) of a constructor or method to a signature that has no parameterized types or type variables. The erasure of a constructor or method signature s is a signature consisting of the same name as s and the erasures of all the formal parameter types given in s.
The type parameters of a constructor or method (§8.4.4), and the return type (§8.4.5) of a method, also undergo erasure if the constructor or method's signature is erased.
The erasure of the signature of a generic method has no type parameters.
In case of Non Empty List:
......
public static void main(String[] args) throws ClassNotFoundException {
List<MyObject> list= new ArrayList<MyObject>();
list.add(new MyObject());
System.out.println("the type of the list is: "+classTypeOfList(list));
}
public static <T> String classTypeOfList(List<T> list) throws ClassNotFoundException {
return list.get(0).getClass().getCanonicalName();
}
OUTPUT
the type of the list is: MyObject