Java generics - get a class?

I have a list programmed as follows: public class MyList<T> . Is there a way to use the T variable to get the class name (so I can, from inside MyList , know that T is String, Socket, etc.)?

EDIT: Nevermind, found the answer here .

+63
java generics
Jan 29 '11 at 1:47 april
source share
5 answers

The short answer . You can not.

Long answer :

Due to the way generic tools are implemented in Java, the generic type T is not preserved at runtime. However, you can use a private data element:

 public class Foo<T> { private Class<T> type; public Foo(Class<T> type) { this.type = type; } } 
+88
Jan 29 '11 at 13:50
source share

I like the solution from

http://www.nautsch.net/2008/10/28/class-von-type-parameter-java-generics/

 public class Dada<T> { private Class<T> typeOfT; @SuppressWarnings("unchecked") public Dada() { this.typeOfT = (Class<T>) ((ParameterizedType)getClass() .getGenericSuperclass()) .getActualTypeArguments()[0]; } ... 
+38
Jul 20 '13 at 21:53 on
source share

You see the result Type Erasure . From this page ...

When a typical type is generated, the compiler translates these types of methods, called erasing styles - a process where the compiler deletes all information related to type parameters and enter arguments inside the class or method. The erasure type allows Java applications that use binary compatibility support with Java libraries and applications that were created prior to generics.

For example, a Box <String> is translated into a Box type, which is called a raw type — the raw type is a generic class or interface name without type arguments. This means that you cannot find out what type of Object is used by the generic class at runtime.

It also looks like a question , which has a good answer .

+24
Jan 29 2018-11-11T00:
source share

I am not 100% sure if this works in all cases (at least Java 1.5 is required):

 import java.lang.reflect.Field; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.HashMap; import java.util.Map; public class Main { public class A { } public class B extends A { } public Map<A, B> map = new HashMap<Main.A, Main.B>(); public static void main(String[] args) { try { Field field = Main.class.getField("map"); System.out.println("Field " + field.getName() + " is of type " + field.getType().getSimpleName()); Type genericType = field.getGenericType(); if(genericType instanceof ParameterizedType) { ParameterizedType type = (ParameterizedType) genericType; Type[] typeArguments = type.getActualTypeArguments(); for(Type typeArgument : typeArguments) { Class<?> classType = ((Class<?>)typeArgument); System.out.println("Field " + field.getName() + " has a parameterized type of " + classType.getSimpleName()); } } } catch(Exception e) { e.printStackTrace(); } } } 

This will output:

Field Map Type Map
The field map has a parameterized type A The field map has a parameterized type B

+3
Jan 29 2018-11-21T00:
source share

I can get a universal type class as follows:

 class MyList<T> { Class<T> clazz = (Class<T>) DAOUtil.getTypeArguments(MyList.class, this.getClass()).get(0); } 

You need two functions from this file: http://code.google.com/p/hibernate-generic-dao/source/browse/trunk/dao/src/main/java/com/googlecode/genericdao/dao/DAOUtil. java

More details: http://www.artima.com/weblogs/viewpost.jsp?thread=208860

+2
Jan 17 '13 at 21:19
source share



All Articles