The following simple program compiles with Eclipse compiler, but not Javac:
public class Test {
public static interface Function {
Boolean apply(T1 t1, T2 t2);
}
private static > Function _cmp(final boolean lt) {
return new Function() {
@Override
public Boolean apply(T t1, T t2) {
if (lt) {
return (t1.compareTo(t2) < 0);
} else {
return (t1.compareTo(t2) > 0);
}
}
};
}
public static Function LESS_THAN = _cmp(true); // error line
public static Function GREATER_THAN = _cmp(false); // error line
}
The error message of Javac:
java: P:\java-tool\src\main\java\T3.java:20: incompatible types; inferred type argument(s) java.lang.Object do not conform to bounds of type variable(s) T
found : T3.Function
required: T3.Function
解决方案
The type inference is not working for version 1.6 -
Try manually specifying the type argument. E.g. -
public static Function LESS_THAN = Test._cmp(true); // error line
Note that you are using the raw version of your own generic class there.