Weird usage of Java Generics compiles correctly with JDT (and code runs) but does not compile with Oracle's Java6 compiler

Danilo Tommasina danilo.tommasina at avanon.com
Tue Feb 21 04:43:39 PST 2012


|Hello,

please look at the following code:

public  class  MyWeirdClass  {

     public  void  entryPoint()  {
         doSomethingWeird();
     }

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

     private  <Textends  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 eclipse 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 it be supported in future releases of OpenJDK's / Oracle's JDK compilers?
Thanks in advance for any comment.

Kind Regards
Danilo Tommasina

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20120221/27d56e15/attachment.html 


More information about the compiler-dev mailing list