I had to discover I have Java code in my project, which compiles and runs fine in Eclipse, but throws a compilation error in javac.
A self-contained snippet:
You are right. This problem indeed exists. Eclipse does not use javac
. It uses its own compiler.
Actually javac is "right". Generics are erasures. Type S
is not included into your byte code, so jvm does not have enough information about the return type at runtime. To solve the problem change the method prototype as following:
public static
Set covariantSet(Set returnType)
Now the return type is passed to the method at runtime and compiler should not complain.