Convert java.lang.reflect.Type to Class clazz

前端 未结 6 1621
闹比i
闹比i 2021-02-12 11:43

How can I convert java.lang.reflect.Type to Class clazz?

If I have one method as next which has an argument of Class

6条回答
  •  别跟我提以往
    2021-02-12 11:54

    If you are willing to use a library, you could use com.google.guava:guava:12+:

    Class clazz = com.google.common.reflect.TypeToken.of(type).getRawType();
    

    Alternatively you could also use com.fasterxml.jackson.core:jackson-databind:2.8.x:

    Class clazz = com.fasterxml.jackson.databind.type.TypeFactory.rawClass(type);
    

    This handles all cases correctly and you will get the type-erased class of your type.

提交回复
热议问题