I wonder if this is a bug in ECJ or a valid interpretation of the JLS.
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Supplier;
public class GenericsTest {
public void testInference() {
Set<Object> set1 = getCollection(HashSet::new);
Set<Object> set2 = getCollection2(HashSet::new);
}
public static <E, C extends Collection<E>> C getCollection(Supplier<C> collectionSupplier) {
return collectionSupplier.get();
}
public static <E, C extends Collection<E>> C getCollection2(CollectionSupplier<E, C> collectionSupplier) {
return collectionSupplier.get();
}
public interface CollectionSupplier<E, C extends Collection<E>> {
C get();
}
}
Javac (11.0.11) compiles everything (correctly, I would say).
ECJ (4.20.0) fails to compile the getCollection2(HashSet::new)
call with error "Type mismatch: cannot convert from Collection to Set".
The getCollection(HashSet::new)
call is not a problem for any compiler.
If I apply the suggested quickfix and insert a cast to HashSet<Object>
, I get a different error from ECJ: "Problem detected during type inference: Unknown error at invocation of getCollection2(GenericsTest.CollectionSupplier<Object,Collection>)"
There are lots of similar questions here and bugs at bugs.eclipse.org, but most examples seemed to involve ?
.