com.alibaba.fastjson.util.ParameterizedTypeImpl Java Examples

The following examples show how to use com.alibaba.fastjson.util.ParameterizedTypeImpl. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TypeReference.java    From uavstack with Apache License 2.0 6 votes vote down vote up
private Type handlerParameterizedType(ParameterizedType type, Type[] actualTypeArguments, int actualIndex) {
    Class<?> thisClass = this.getClass();
    Type rawType = type.getRawType();
    Type[] argTypes = type.getActualTypeArguments();

    for(int i = 0; i < argTypes.length; ++i) {
        if (argTypes[i] instanceof TypeVariable && actualIndex < actualTypeArguments.length) {
            argTypes[i] = actualTypeArguments[actualIndex++];
        }

        // fix for openjdk and android env
        if (argTypes[i] instanceof GenericArrayType) {
            argTypes[i] = TypeUtils.checkPrimitiveArray(
                    (GenericArrayType) argTypes[i]);
        }

        // 如果有多层泛型且该泛型已经注明实现的情况下,判断该泛型下一层是否还有泛型
        if(argTypes[i] instanceof ParameterizedType) {
            return handlerParameterizedType((ParameterizedType) argTypes[i], actualTypeArguments, actualIndex);
        }
    }

    Type key = new ParameterizedTypeImpl(argTypes, thisClass, rawType);
    return key;
}
 
Example #2
Source File: TypeReference.java    From uavstack with Apache License 2.0 5 votes vote down vote up
/**
 * @since 1.2.9
 * @param actualTypeArguments
 */
protected TypeReference(Type... actualTypeArguments){
    Class<?> thisClass = this.getClass();
    Type superClass = thisClass.getGenericSuperclass();

    ParameterizedType argType = (ParameterizedType) ((ParameterizedType) superClass).getActualTypeArguments()[0];
    Type rawType = argType.getRawType();
    Type[] argTypes = argType.getActualTypeArguments();

    int actualIndex = 0;
    for (int i = 0; i < argTypes.length; ++i) {
        if (argTypes[i] instanceof TypeVariable &&
                actualIndex < actualTypeArguments.length) {
            argTypes[i] = actualTypeArguments[actualIndex++];
        }
        // fix for openjdk and android env
        if (argTypes[i] instanceof GenericArrayType) {
            argTypes[i] = TypeUtils.checkPrimitiveArray(
                    (GenericArrayType) argTypes[i]);
        }

        // 如果有多层泛型且该泛型已经注明实现的情况下,判断该泛型下一层是否还有泛型
        if(argTypes[i] instanceof ParameterizedType) {
            argTypes[i] = handlerParameterizedType((ParameterizedType) argTypes[i], actualTypeArguments, actualIndex);
        }
    }

    Type key = new ParameterizedTypeImpl(argTypes, thisClass, rawType);
    Type cachedType = classTypeCache.get(key);
    if (cachedType == null) {
        classTypeCache.putIfAbsent(key, key);
        cachedType = classTypeCache.get(key);
    }

    type = cachedType;

}
 
Example #3
Source File: Response.java    From spring-boot-cookbook with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    String jsonText = "{\"data\":{\"id\":1,\"name\":\"张三\"}}";
    Response<User> response = JSON.parseObject(jsonText, new ParameterizedTypeImpl(new Class[]{User.class}, null, Response.class));
    /**
     * {"data":{"id":1,"name":"张三"}}
     */
    System.out.println(response);

    response = JSON.parseObject(jsonText, new TypeReference<Response<User>>() {
    });
    /**
     * {"data":{"id":1,"name":"张三"}}
     */
    System.out.println(response);
}
 
Example #4
Source File: TypeReference.java    From java-tool with Apache License 2.0 4 votes vote down vote up
private static Type create(Type[] actualTypeArguments, Type rawType) {
    return new ParameterizedTypeImpl(actualTypeArguments, null, rawType);
}
 
Example #5
Source File: FastjsonUtils.java    From DevUtils with Apache License 2.0 2 votes vote down vote up
/**
 * 获取 List Type
 * @param type Bean.class
 * @return List<Bean> Type
 */
public static Type getListType(final Type type) {
    return new ParameterizedTypeImpl(new Type[]{type}, null, List.class);
}
 
Example #6
Source File: FastjsonUtils.java    From DevUtils with Apache License 2.0 2 votes vote down vote up
/**
 * 获取 Set Type
 * @param type Bean.class
 * @return Set<Bean> Type
 */
public static Type getSetType(final Type type) {
    return new ParameterizedTypeImpl(new Type[]{type}, null, Set.class);
}
 
Example #7
Source File: FastjsonUtils.java    From DevUtils with Apache License 2.0 2 votes vote down vote up
/**
 * 获取 Map Type
 * @param keyType   Key.class
 * @param valueType Value.class
 * @return Map<Key, Value> Type
 */
public static Type getMapType(final Type keyType, final Type valueType) {
    return new ParameterizedTypeImpl(new Type[]{keyType, valueType}, null, Map.class);
}
 
Example #8
Source File: FastjsonUtils.java    From DevUtils with Apache License 2.0 2 votes vote down vote up
/**
 * 获取 Type
 * @param rawType       raw type
 * @param typeArguments type arguments
 * @return Type
 */
public static Type getType(final Type rawType, final Type... typeArguments) {
    return new ParameterizedTypeImpl(typeArguments, null, rawType);
}