TypeLiteral.java 文件源码

java
阅读 41 收藏 0 点赞 0 评论 0

项目:Fluf 作者:
/**
 * Constructs a new type literal. Derives represented class from type
 * parameter.
 * 
 * <p>
 * Clients create an empty anonymous subclass. Doing so embeds the type
 * parameter in the anonymous class's type hierarchy so we can reconstitute
 * it at runtime despite erasure.
 */
public TypeLiteral() {
    ParameterizedType genClass = (ParameterizedType) getClass().getGenericSuperclass();
    Type[] types = genClass.getActualTypeArguments();
    if (types == null || types.length == 0) {
        throw new RuntimeException("TypeLiteral<T> must have a specfied type <T>");
    }
    this.type = types[0];
    if (this.type instanceof GenericArrayType)
        throw new RuntimeException("TypeLiteral does not support GenericArrayTypes, use Injector.get(ComponentType[].class)");
    if (this.type instanceof TypeVariable)
        throw new RuntimeException("TypeLiteral does not support TypeVariables");

    if (type instanceof Class) {
        this.rawType = (Class<T>) this.type;
    } else if (type instanceof ParameterizedType) {
        this.rawType =  (Class<T>) ((ParameterizedType) type).getRawType();
    } else {
        this.rawType = Object.class;
    }
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号