IDEA vs Sun Generics compilation/parse issue

In IDEA 9.0.1 the code below is red-lined (manually inserted black underline here instead). However,
1) Compiling with Sun javac 1.6.0_19 reports no error
2) Compiling with Sun javac 1.6.0_01 reports the error

Presumably somewhere between version 1 & 19 this behaviour flips over (somewhere before 13  I think).

My view (based on my limited understanding of java generics) is that this code should not compile and the IDEA behaviour is correct.

If any one a better understanding of this issue I'd be glad to read it.

Many Thx

Richard

---------------------------------------------
public class Bad {
   static interface F1<R, T> {
        R apply(T t);
    }


    static class Foo<T> {
        public static String x(F1<Object, String> f) {
           return null;
        }
    }

    public static final Foo<String> c =  new Foo<String>();

    public static final String xxx = c.x(new F1<String, String>() {
        @Override public String apply(String s) {
            return null;
        }
    });


}

3 comments

Hi Richard,

Current behavior (compile error) is correct because you're trying to use 'F1<String, String>' in a place where 'F1<Object, String>' is expected.

Java generics are not covariant, hence, such a statement is not allowed.

Feel free to check this excellent Java Generics FAQ for more information -  http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html.

Denis

0

So that would be a bug in the Sun compiler then (which reports no error)!!!

0

I believe the issue was reported a while ago and Sun guys already addressed it because java compiler from 1.6_19 reports error for such source file.

0

Please sign in to leave a comment.