Java generics: What is the compiler's issue here? (“no unique maximal instance”)

前端 未结 4 1381
无人及你
无人及你 2021-02-20 18:00

I have the following methods:

public  T fromJson( Reader jsonData, Class clazz ) {
    return fromJson( jsonData, (Type)clazz );
}

public <         


        
4条回答
  •  我寻月下人不归
    2021-02-20 18:22

    This seems like a failure of inference. The first method clearly intends to call the second method with the type argument being the same type parameter T that it has. But probably the compiler can't figure it out because its inference system is not good enough.

    In any case, you should be able to explicitly specify the type argument and it should get rid of the error:

    public  T fromJson( Reader jsonData, Class clazz ) {
        return this.fromJson( jsonData, (Type)clazz );
    }
    

提交回复
热议问题