I have this code which compiles fine when compiled as Java 7 (Eclipse compiler) but fails when I set the project settings to Java 8:
package scratch;
class Param<T extends Comparable<T>> {
public Comparable<?> get() {
return null;
}
}
public class Condition<T extends Comparable<T>> {
public static <T extends Comparable<T>> Condition<T> isInRange(T lower, T upper) {
return null;
}
public void foo() {
Comparable bound = null; // Line 15
Param<?> param = new Param<Double>();
Condition.isInRange(param.get(), bound); // Line 17
}
}
In Java 7, I get these warnings:
- line 15: Comparable is a raw type. References to generic type Comparable should be parameterized
- line 17: Type safety: Unchecked invocation isInRange(Comparable, Comparable) of the generic method isInRange(T, T) of type Condition
When I add <?>
in line 15, the warning is gone, but I then get an error in line 17:
Bound mismatch: The generic method isInRange(T, T) of type Condition is not applicable for the arguments (Comparable, Comparable). The inferred type Comparable is not a valid substitute for the bounded parameter >
Does anyone know what exactly causes this incompability?
PS: I added these ugly casts to make the code compile under both versions of Java:
Condition.isInRange((Comparable)param.get(), (Comparable) bound);