Organizational Research By

Surprising Reserch Topic

java how do i get a class literal from a generic type

java how do i get a class literal from a generic type  using -'java,generics,class,literals'

Typically, I've seen people use the class literal like this:

Class<Foo> cls = Foo.class;

But what if the type is generic, e.g. List? This works fine, but has a warning since List should be parameterized:

Class<List> cls = List.class

So why not add a <?>? Well, this causes a type mismatch error:

Class<List<?>> cls = List.class

I figured something like this would work, but this is just a plain ol' a syntax error:

Class<List<Foo>> cls = List<Foo>.class

How can I get a Class<List<Foo>> statically, e.g. using the class literal?

I could use @SuppressWarnings("unchecked") to get rid of the warnings caused by the non-parameterized use of List in the first example, Class<List> cls = List.class, but I'd rather not.

Any suggestions?

asked Sep 15, 2015 by BetQks
0 votes

Related Hot Questions

6 Answers

0 votes

You can't due to type erasure.

Java generics are little more than syntactic sugar for Object casts. To demonstrate:

List list1 = new ArrayList();
List list2 = (List)list1;
list2.add("foo"); // perfectly legal

The only instance where generic type information is retained at runtime is with Field.getGenericType() if interrogating a class's members via reflection.

All of this is why Object.getClass() has this signature:

public final native Class getClass();

The important part being Class.

To put it another way, from the Java Generics FAQ:

Why is there no class literal for concrete parameterized types?

Because parameterized type has no exact runtime type representation.

A class literal denotes a Class object that represents a given type. For instance, the class literal String.class denotes the Class object that represents the type String and is identical to the Class object that is returned when method getClass is invoked on a String object. A class literal can be used for runtime type checks and for reflection.

Parameterized types lose their type arguments when they are translated to byte code during compilation in a process called type erasure . As a side effect of type erasure, all instantiations of a generic type share the same runtime representation, namely that of the corresponding raw type . In other words, parameterized types do not have type representation of their own. Consequently, there is no point in forming class literals such as List.class , List.class and List.class , since no such Class objects exist. Only the raw type List has a Class object that represents its runtime type. It is referred to as List.class.

answered Sep 15, 2015 by SherleneGrai
0 votes

There are no Class literals for parameterized types, however there are Type objects that correctly define these types.

See java.lang.reflect.ParameterizedType -

Google's Gson library defines a TypeToken class that allows to simply generate parameterized types and uses it to spec json objects with complex parameterized types in a generic friendly way. In your example you would use:

Type typeOfListOfFoo = new TypeToken>(){}.getType()

I intended to post links to the TypeToken and Gson classes javadoc but Stack Overflow won't let me post more than one link since I'm a new user, you can easily find them using Google search

answered Sep 15, 2015 by AntOHaran
0 votes

To expound on cletus' answer, at runtime all record of the generic types is removed. Generics are processed only in the compiler and are used to provide additional type safety. They are really just shorthand that allows the compiler to insert typecasts at the appropriate places. For example, previously you'd have to do the following:

List x = new ArrayList();
x.add(new SomeClass());
Iterator i = x.iterator();
SomeClass z = (SomeClass);


List x = new ArrayList();
x.add(new SomeClass());
Iterator i = x.iterator();
SomeClass z =;

This allows the compiler to check your code at compile-time, but at runtime it still looks like the first example.

answered Sep 15, 2015 by RalphFallsuf
0 votes

You can manage it with a double cast :

@SuppressWarnings("unchecked") Class> cls = (Class>)(Object)List.class

answered Sep 15, 2015 by ptlikzsdf
0 votes

Due to the exposed fact that Class literals doesn't have generic type information, I think you should assume that it will be impossible to get rid of all the warnings. In a way, using Class is the same as using a collection without specifying the generic type. The best I could come out with was:

private > List getList(Class cls) {
    List res = new ArrayList();
    // "snip"... some stuff happening in here, using cls
    return res;

public > List> getList() {
    return getList(A.class);
answered Sep 15, 2015 by Ann91B
0 votes

Well as we all know that it gets erased. But it can be known under some circumstances where the type is explicitly mentioned in the class hierarchy:

import java.lang.reflect.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;

public abstract class CaptureType {
     * {@link java.lang.reflect.Type} object of the corresponding generic type. This method is useful to obtain every kind of information (including annotations) of the generic type.
     * @return Type object. null if type could not be obtained (This happens in case of generic type whose information cant be obtained using Reflection). Please refer documentation of {@link com.types.CaptureType}
    public Type getTypeParam() {
        Class bottom = getClass();
        Map, Type> reifyMap = new LinkedHashMap<>();

        for (; ; ) {
            Type genericSuper = bottom.getGenericSuperclass();
            if (!(genericSuper instanceof Class)) {
                ParameterizedType generic = (ParameterizedType) genericSuper;
                Class actualClaz = (Class) generic.getRawType();
                TypeVariable>[] typeParameters = actualClaz.getTypeParameters();
                Type[] reified = generic.getActualTypeArguments();
                assert (typeParameters.length != 0);
                for (int i = 0; i < typeParameters.length; i++) {
                    reifyMap.put(typeParameters[i], reified[i]);

            if (bottom.getSuperclass().equals(CaptureType.class)) {
                bottom = bottom.getSuperclass();
            bottom = bottom.getSuperclass();

        TypeVariable var = bottom.getTypeParameters()[0];
        while (true) {
            Type type = reifyMap.get(var);
            if (type instanceof TypeVariable) {
                var = (TypeVariable) type;
            } else {
                return type;

     * Returns the raw type of the generic type.

For example in case of {@code CaptureType}, it would return {@code Class}

* For more comprehensive examples, go through javadocs of {@link com.types.CaptureType} * * @return Class object * @throws java.lang.RuntimeException If the type information cant be obtained. Refer documentation of {@link com.types.CaptureType} * @see com.types.CaptureType */ public Class getRawType() { Type typeParam = getTypeParam(); if (typeParam != null) return getClass(typeParam); else throw new RuntimeException("Could not obtain type information"); } /** * Gets the {@link java.lang.Class} object of the argument type. *

If the type is an {@link java.lang.reflect.ParameterizedType}, then it returns its {@link java.lang.reflect.ParameterizedType#getRawType()}

* * @param type The type * @param type of class object expected * @return The Class object of the type * @throws java.lang.RuntimeException If the type is a {@link java.lang.reflect.TypeVariable}. In such cases, it is impossible to obtain the Class object */ public static Class getClass(Type type) { if (type instanceof GenericArrayType) { Type componentType = ((GenericArrayType) type).getGenericComponentType(); Class componentClass = getClass(componentType); if (componentClass != null) { return (Class) Array.newInstance(componentClass, 0).getClass(); } else throw new UnsupportedOperationException("Unknown class: " + type.getClass()); } else if (type instanceof Class) { Class claz = (Class) type; return claz; } else if (type instanceof ParameterizedType) { return getClass(((ParameterizedType) type).getRawType()); } else if (type instanceof TypeVariable) { throw new RuntimeException("The type signature is erased. The type class cant be known by using reflection"); } else throw new UnsupportedOperationException("Unknown class: " + type.getClass()); } /** * This method is the preferred method of usage in case of complex generic types. *

It returns {@link com.types.TypeADT} object which contains nested information of the type parameters

* * @return TypeADT object * @throws java.lang.RuntimeException If the type information cant be obtained. Refer documentation of {@link com.types.CaptureType} */ public TypeADT getParamADT() { return recursiveADT(getTypeParam()); } private TypeADT recursiveADT(Type type) { if (type instanceof Class) { return new TypeADT((Class) type, null); } else if (type instanceof ParameterizedType) { ArrayList generic = new ArrayList<>(); ParameterizedType type1 = (ParameterizedType) type; return new TypeADT((Class) type1.getRawType(), -> recursiveADT(x)).collect(Collectors.toList())); } else throw new UnsupportedOperationException(); } } public class TypeADT { private final Class reify; private final List parametrized; TypeADT(Class reify, List parametrized) { this.reify = reify; this.parametrized = parametrized; } public Class getRawType() { return reify; } public List getParameters() { return parametrized; } }

And now you can do things like:

static void test1() {
        CaptureType t1 = new CaptureType() {
        equals(t1.getRawType(), String.class);

    static void test2() {
        CaptureType> t1 = new CaptureType>() {
        equals(t1.getRawType(), List.class);
        equals(t1.getParamADT().getParameters().get(0).getRawType(), String.class);

    private static void test3() {
            CaptureType>> t1 = new CaptureType>>() {
            equals(t1.getParamADT().getRawType(), List.class);
        equals(t1.getParamADT().getParameters().get(0).getRawType(), List.class);

    static class Test4 extends CaptureType> {

    static void test4() {
        Test4 test4 = new Test4();
        equals(test4.getParamADT().getRawType(), List.class);

    static class PreTest5 extends CaptureType {

    static class Test5 extends PreTest5 {

    static void test5() {
        Test5 test5 = new Test5();
        equals(test5.getTypeParam(), Integer.class);

    static class PreTest6 extends CaptureType {

    static class Test6 extends PreTest6 {

    static void test6() {
        Test6 test6 = new Test6();
        equals(test6.getTypeParam(), Integer.class);

    class X extends CaptureType {

    class Y extends X {

    class Z extends Y>>>> {

    void test7(){
        Z z = new Z<>();
        TypeADT param = z.getParamADT();
        equals(param.getRawType(), Map.class);
        List parameters = param.getParameters();
        equals(parameters.get(0).getRawType(), Integer.class);
        equals(parameters.get(1).getRawType(), List.class);
        equals(parameters.get(1).getParameters().get(0).getRawType(), List.class);
        equals(parameters.get(1).getParameters().get(0).getParameters().get(0).getRawType(), List.class);
        equals(parameters.get(1).getParameters().get(0).getParameters().get(0).getParameters().get(0).getRawType(), Integer.class);

    static void test8() throws IllegalAccessException, InstantiationException {
        CaptureType type = new CaptureType() {
        equals(type.getRawType(), int[].class);

    static void test9(){
        CaptureType type = new CaptureType() {
        equals(type.getRawType(), String[].class);

    static class SomeClass extends CaptureType{}
    static void test10(){
        SomeClass claz = new SomeClass<>();
            throw new RuntimeException("Shouldnt come here");
        }catch (RuntimeException ex){


    static void equals(Object a, Object b) {
        if (!a.equals(b)) {
            throw new RuntimeException("Test failed. " + a + " != " + b);

More info here. But again, it is almost impossible to retrieve for:

class SomeClass extends CaptureType{}
SomeClass claz = new SomeClass<>();

where it gets erased.

answered Sep 15, 2015 by SimaTjalkabo