Same source code, Eclipse build success but Maven (javac) fails

前端 未结 4 751
鱼传尺愫
鱼传尺愫 2021-02-15 11:07

Keep getting this error when compiling using Maven:

type parameters of X cannot be determined; no unique maximal instance exists for type variable X wit         


        
4条回答
  •  -上瘾入骨i
    2021-02-15 11:15

    This issue can occur when your code is generic and it calls another method that has a generic return type. Sometimes the compiler gets confused trying to figure out how to resolve the method call / return type.

    It can be resolved by adding an explicit cast to your code.

    // Old code:
    public T getValue() {
        return otherMethod();  // otherMethod has the signature:  RT otherMethod() { ... }
    }
    
    // New code:
    @SuppressWarnings("unchecked")
    public T getValue() {
        return (T) otherMethod();   // the cast tells the compiler what to do.
    }
    

提交回复
热议问题