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

2019-10-08  本文已影响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]);
    }
}
上一篇下一篇

猜你喜欢

热点阅读