Get type of a generic parameter in Java with reflection

后端 未结 18 2172
死守一世寂寞
死守一世寂寞 2020-11-22 05:56

Is it possible to get the type of a generic parameter?

An example:

public final class Voodoo {
    public static void chill(List aListWithTy         


        
18条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 06:13

    I noticed that many people lean towards the getGenericSuperclass() solution:

    class RootGeneric {
      public Class persistentClass = (Class)
        ((ParameterizedType)getClass().getGenericSuperclass())
          .getActualTypeArguments()[0];
    }
    

    However, this solution is error prone. It will not work properly if there are generics in the descendants. Consider this:

    class Foo extends RootGeneric {}
    
    class Bar extends Foo {}
    

    Which type will Bar.persistentClass have? Class? Nope, it will be Class. This will happen due to getClass() always returns the top most class, which is Bar in this case, and its generic super class is Foo. Hence, the argument type will be Double.

    If you need a reliable solution which doesn't fail I can suggest two.

    1. Use Guava. It has a class that was made exactly for this purpose: com.google.common.reflect.TypeToken. It handles all the corner cases just fine and offers some more nice functionality. The downside is an extra dependency. Given you've used this class, your code would look simple and clear, like this:
    class RootGeneric {
      @SuppressWarnings("unchecked")
      public final Class persistentClass = (Class) (new TypeToken(getClass()) {}.getType());
    }
    
    1. Use the custom method below. It implements a significantly simplified logic similar to the Guava class, mentioned above. However, I'd not guarantee it's error prone. It does solve the problem with the generic descendants though.
    abstract class RootGeneric {
      @SuppressWarnings("unchecked")
      private Class getTypeOfT() {
        Class type = null;
        Class iter = getClass();
        while (iter.getSuperclass() != null) {
          Class next = iter.getSuperclass();
          if (next != null && next.isAssignableFrom(RootGeneric.class)) {
            type =
                (Class)
                    ((ParameterizedType) iter.getGenericSuperclass()).getActualTypeArguments()[0];
            break;
          }
          iter = next;
        }
        if (type == null) {
          throw new ClassCastException("Cannot determine type of T");
        }
        return type;
      }
    }
    

提交回复
热议问题