Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么是杰克逊,格森。。。不使用泛型,而是将类对象传递给反序列化对象_Java_Generics - Fatal编程技术网

Java 为什么是杰克逊,格森。。。不使用泛型,而是将类对象传递给反序列化对象

Java 为什么是杰克逊,格森。。。不使用泛型,而是将类对象传递给反序列化对象,java,generics,Java,Generics,例如: 为什么不: Foo result=mapper.readValuejsonStr 而不是 Foo result=mapper.readValuejsonStr,Foo.class Java泛型有什么限制阻止他们使用它?简短回答 这是Java泛型的限制 限制是无法从类型变量中选择。因此,不能调用t.class来获取下一个方法所需的t的class对象 长话短说 请看一下实现 public <T> T readValue(String content, Class<T>

例如: 为什么不:

Foo result=mapper.readValuejsonStr

而不是

Foo result=mapper.readValuejsonStr,Foo.class

Java泛型有什么限制阻止他们使用它?

简短回答 这是Java泛型的限制 限制是无法从类型变量中选择。因此,不能调用t.class来获取下一个方法所需的t的class对象

长话短说 请看一下实现

public <T> T readValue(String content, Class<T> valueType)
    throws IOException, JsonParseException, JsonMappingException
{
    return (T) _readMapAndClose(_jsonFactory.createParser(content), _typeFactory.constructType(valueType));
}

public JavaType constructType(Type type) {
    return _fromAny(null, type, EMPTY_BINDINGS);
}
这在泛型中是不可能的

实例 我试过以下方法

private <T> T readValue(String content)
{
    return constructType(T.class);
}

private JavaType constructType(Type type)
{
    //construct type somehow
}
这将成功编译

也看到
你说什么限制?有限制@RomanC限制是无法从类型变量中选择。因此,您不能调用t.class来获取下一个方法所需的类。
private <T> T readValue(String content)
{
    return constructType(T.class);
}

private JavaType constructType(Type type)
{
    //construct type somehow
}
private <T> T readValue(String content, Class<T> clazz)
{
    return constructType(clazz);
}

private JavaType constructType(Type type)
{
    //construct type somehow
}