反射

作者: TheDogChou | 来源:发表于2018-07-11 18:43 被阅读0次

    getSuperclass()/getGenericSuperclass()和getInterfaces()/getGenericInterfaces()

    带Generic的表示:如果超类或者父接口是参数化类型,则返回的 Type 对象必须准确反映源代码中所使用的实际类型参数。(也就是说会返回父类泛型的具体类型的Class)如果以前未曾创建表示超类的参数化类型,则创建这个类型。

    public static Class getSuperClassGenricType(Class clazz, int index){

    Type genType = clazz.getGenericSuperclass();

    if(!(genType instanceof ParameterizedType)){

    return Object.class;

    }

    Type [] params = ((ParameterizedType)genType).getActualTypeArguments();

    if(index >= params.length || index < 0){

    return Object.class;

    }

    if(!(params[index] instanceof Class)){

    return Object.class;

    }

    return (Class) params[index];

    }

    相关文章

      网友评论

          本文标题:反射

          本文链接:https://www.haomeiwen.com/subject/ldckpftx.html