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

前端 未结 4 769
误落风尘
误落风尘 2021-02-15 11:02

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条回答
  •  自闭症患者
    2021-02-15 11:32

    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.
    }
    

提交回复
热议问题