type parameters of
T cannot be determined; no unique maximal instance exists for type variable T with upper bounds T,java.lang.Object
In java 1.6u2 running in maven 2.0.7, we started getting this message but in eclipse 3.3.0 we were not. (and in idea we also were failing)
First, what the hell does this message means? Second, why can eclipse handle the issue and not the regular jdk?
Anyhow the problem was caused by this (our example was a little more complex than this):
public <T> T getFoo() {
T result;
result = getBar();
return result;
}public <T> T getBar() {
return (T) map.get("bar");
}
The workaround is casting the line:
result = getBar();
to:
result = (T)getBar();
Thanks a lot for the post. It saved our day debugging the migration to Maven with a few projects.
IMHO this is just another hint at how badly generics have been implemented in Java.
I had a similar problem where I also used casting as a workaround. There’s another side to this problem though related to autoboxing.
For example, if T is an Integer, the following code will compile in eclipse but fail with javac
int result = getFoo();
The error message from javac is “type parameters of T cannot be determined; no unique maximal instance exists for type variable T with upper bounds int,java.lang.Object”
The only way to get the above code building with both is to give up autoboxing completely:
Integer result = getFoo();
This was using Eclipse 3.3.1.1 and java 5 (1.5.0_14-b03).