美文网首页
2019-10-08 获取泛型T的Type

2019-10-08 获取泛型T的Type

作者: 兣甅 | 来源:发表于2019-10-08 21:33 被阅读0次
public class BaseSubscriber<T> implements Observer<Object> {
    private final Type mType;

    public BaseSubscriber() {
        this.mType = getSuperclassTypeParameter(getClass());
    }

    private Type getSuperclassTypeParameter(Class<?> subclass) {
        Type superclass = subclass.getGenericSuperclass();
        if (superclass instanceof Class) {
            throw new RuntimeException("Missing type parameter.");
        }
        ParameterizedType parameterized = (ParameterizedType) superclass;
        return $Gson$Types.canonicalize(parameterized.getActualTypeArguments()[0]);
    }
}

相关文章

网友评论

      本文标题:2019-10-08 获取泛型T的Type

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