Reputation: 6646
I have a method with 1 List, generic class inside:
public static String classTypeOfList(List<T> list) {
return T.getName(); //in my mind...
}
The code is wrong, but you can see, what I want. If i call this method like this:
List<MyObject> list;
System.out.println("the type of the list is: "+classTypeOfList(list));
I would like to get this result:
the type of the list is: MyObject
How could I get the name of a generic type class? Or if I can't get it this way, than could you show me an another option? Thank you!
Upvotes: 2
Views: 15234
Reputation: 12242
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.
......
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();
}
the type of the list is: MyObject
Upvotes: 3
Reputation: 4691
This answer may be little ( or very ) different from question, since it is long enough for the comment, so i am posting it as answer.
I found this question interesting and hence gave it a try. My attempt is following, i got the Type
of the class, so i thought it is worth sharing and getting experts opinion back on my approach
public class A {
public static void main(String[] args) {
List<B> listB = new ArrayList<>();
B b1 = new B();
listB.add(b1);
List<C> listC = new ArrayList<>();
C c1 = new C();
listC.add(c1);
A a = new A();
a.method(listB);
a.method(listC);
}
public <T> void method(List<T> list) {
System.out.println(list.get(0).getClass().getName());
}
}
class B {
}
class C {
}
Output i got was B
and C
Upvotes: 2
Reputation: 509
This is how you can get class:
Class<T> clazz = ((Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]);
You can read more about it on this link.
Upvotes: 0
Reputation: 1960
You need to use getClass()
on the object. After you get the class object you can use getName()
to retrieve the class name.
Upvotes: 1
Reputation: 21831
I'm afraid, this cannot be done. Generics only exist at compile time. At runtime this information is erased and at runtime your List<MyObject>
will simply turn into List
.
Upvotes: 2