sarathonline.com/blog | Trouble viewing this page? Give Feedback .
Try my blog on: Ajax Version
Search

Monday, August 09, 2010

Maven Only: type parameters of X cannot be determined

This is a rare error that compiling with Eclipse does not catch, but maven compile goal will. In the case in question, There is a Generic return type as follows, as discussed in java forums

private <A> A getValue(){
    return getOtherValue(); //<--  Error occurs here
}

It compiles out an error:

type parameters of <X>X cannot be determined; no unique maximal instance exists for type variable X with upper bounds int,java.lang.Object

The intriguing question is, why does this happen only in maven? Why does it not happen in Eclipse? Few in the java forum have suggested that this is a bug in the compiler. However, an impatient programmer looking for a solution around it, Just cant get it. There is an unanswered stack overflow post here. The java forum is also confusing for a novice.

The simple answer here is, the getValue() method (or the getOtherValue, or the method it calls) is, at some place, not really Strongly typed and the compiler is left to guess wildly about what type the return value actually is. To overcome it, the whole call chain need to infer generics or simply perform an unchecked type cast. The solution will be strongly typed after the call in the chain.

private <A> A getValue(){
    return (A) getOtherValue(); //<--  No Error now
}

© SarathOnline.com 2000