Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jdt-core-dev] Weird usage of Java Generics compiles correctly with JDT (and code runs) but does not compile with Oracle's compiler

Hello please look at the following code.

public class MyWeirdClass {

    public void entryPoint() {
        doSomethingWeird();
    }

    @SuppressWarnings( "unchecked" )
    private <T extends A & B> T getMyClass() {
        if ( System.currentTimeMillis() % 2 == 0 ) {
           return (T) new MyClass_1();
        } else {
           return (T) new MyClass_2();
        }
    }

    private <T extends A & B> void doSomethingWeird() {
        T obj = getMyClass();

        obj.methodFromA();
        obj.methodFromB();
    }

    static interface A {
        void methodFromA();
    }

    static interface B {
        void methodFromB();
    }

    static class MyClass_1 implements A, B {
        public void methodFromA() {};
        public void methodFromB() {};
    }

    static class MyClass_2 implements A, B {
        public void methodFromA() {};
        public void methodFromB() {};
    }
}


This code compiles correctly using the JDT compiler, however generates errors using the Oracle's JDK (Java 6) compiler.
The problem is that the method entryPoint() calls method doSomethingWeird() which will try to infer a type that cannot be uniquely identified resp. in this case does not even physically exist.
(I know it may be possible to define an interface AB extends A,B but this is not the question ;) )
 
My question is: Is this case defined in the JSL, resp. will you continue allowing it in the future?
Thanks in advance for any comment.

Kind Regards
Danilo Tommasina

Back to the top