Convert java.lang.reflect.Type to Class clazz

前端 未结 6 1804
闹比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 12:09

    Andy Turner answer is correct, however if you need to retrieve a class from a type parameter this is a complete example:

    private static class MyClass extends ArrayList {
    }
    
    public static void main(String[] args) {
    
        ParameterizedType arrayListWithParamType
                = (ParameterizedType) MyClass.class.getGenericSuperclass();
    
        Type integerType = arrayListWithParamType.getActualTypeArguments()[0];
    
        Class integerClass = (Class) integerType;
    
        System.out.println(integerClass == Integer.class);
    }
    

提交回复
热议问题