java interface 获取 泛型的真实类型

 default Class<T> thisTClass() {
        Class<?> class1 = getClass();
        Class<?> interfaces = class1.getInterfaces()[0];
        Type[] genericInterfaces = interfaces.getGenericInterfaces();
        Type type = genericInterfaces[0];
        if( type instanceof ParameterizedType ){
            ParameterizedType pType = (ParameterizedType)type;
            Type clazz = pType.getActualTypeArguments()[0];
            if( clazz instanceof Class<?> ){
                return (Class<T>) clazz;
            }
        }
        return null;
    }

 

posted @ 2021-07-10 10:35  Binz  阅读(642)  评论(0编辑  收藏  举报