Community
Participate
Working Groups
Build Identifier: 20110916-0149 I wrote some horrible generics (when using guava) that compiled in the Eclipse IDE. However, from javac at the command line, it fails to compile with the error message: GenericsBug.java:9: <T>ofInstance(T) in mypackage.GenericsBug cannot be applied to <mypackage.GenericsBug.Cache<java.lang.Integer,? extends java.lang.String>>(mypackage.GenericsBug.Cache<java.lang.Object,java.lang.String>) Supplier<Cache<Integer, ? extends String>> imageCache = GenericsBug.<Cache<Integer, ? extends String>> ofInstance( ^ 1 error I would expect Eclipse to give me this same error. Below is a simplified class with stripped down copies of the guava interfaces. ==== package mypackage; import java.util.Map; public class GenericsBug { public static void main(String[] args) { String image = "abc"; Map<Integer, String> imageMap = GenericsBug.of(1, image); Supplier<Cache<Integer, ? extends String>> imageCache = GenericsBug.<Cache<Integer, ? extends String>> ofInstance( CacheBuilder.newBuilder().build(GenericsBug.from(GenericsBug.forMap(imageMap)))); } public static <T> Supplier<T> ofInstance(T instance) { return null; } public interface Supplier<T> { } public abstract class CacheLoader<K, V> { } public static <K, V> CacheLoader<K, V> from(Function<K, V> function) { return null; } public interface Function<F, T> { } public interface Cache<K, V> extends Function<K, V> { } public static <K, V> Function<K, V> forMap(Map<K, V> map) { return null; } public static <K, V> Map<K, V> of(K k1, V v1) { return null; } public static class CacheBuilder<K, V> { public static CacheBuilder<Object, Object> newBuilder() { return null; } public <K1 extends K, V1 extends V> Cache<K1, V1> build(CacheLoader<? super K1, V1> loader) { return null; } } } ==== Version: Indigo Service Release 1 Build id: 20110916-0149 javac 1.6.0_29 Reproducible: Always Steps to Reproduce: 1. Paste this class in Eclipse; it compiles 2. Compile it using javac at the command line; it fails
the magic to get it to compile with javac is to add <Integer,String> for the call to build: public static void main(String[] args) { String image = "abc"; Map<Integer, String> imageMap = GenericsBug.of(1, image); Supplier<Cache<Integer, ? extends String>> imageCache = GenericsBug.<Cache<Integer, ? extends String>> ofInstance( CacheBuilder.newBuilder().<Integer,String>build(GenericsBug.from(GenericsBug.forMap(imageMap)))); }
This a javac bug which has been fixed now in java 7 (javac7b147). The code is legal, however horrible it looks :)
Verified for 3.8M5 using build I20120122-2000