Bug 330733

Summary: [1.5][compiler] Casting to an inner class with generics compiles in Eclipse, but fails in javac
Product: [Eclipse Project] JDT Reporter: fahdshariff
Component: CoreAssignee: Srikanth Sankaran <srikanth_sankaran>
Status: VERIFIED NOT_ECLIPSE QA Contact:
Severity: normal    
Priority: P3 CC: fahdshariff, Olivier_Thomann
Version: 3.6   
Target Milestone: 3.7 M5   
Hardware: All   
OS: All   
Whiteboard:

Description fahdshariff CLA 2010-11-20 06:10:23 EST
Build Identifier: M20100211-1343

Consider the following code:

public class Outer<T>  {

    public class Inner{
    }

    public static <T> Outer<T>.Inner get(){
        Object o = new Object();
        return (Outer<T>.Inner)o;
    }

    public static void main(String[] args) throws Exception {
        Outer.<String>get();
    }
}

This code compiles successfully in Eclipse, but fails to compile in javac (1.6.0_21):

Outer.java:10: ')' expected
        return (Outer<T>.Inner)o;
                        ^
Outer.java:10: ';' expected
        return (Outer<T>.Inner)o;
                         ^
Outer.java:10: illegal start of expression
        return (Outer<T>.Inner)o;
                              ^
3 errors

Is this a bug in javac or Eclipse?

If I change the cast to (Outer.Inner)o it compiles, although there is a warning:

Eclipse:

Outer.Inner is a raw type. References to generic type Outer<T>.Inner should be parameterized

javac:

Outer.java:10: warning: [unchecked] unchecked conversion
found   : Outer.Inner
required: Outer<T>.Inner
        return (Outer.Inner)o;
               ^
1 warning


Reproducible: Always

Steps to Reproduce:
1. Create a class called Outer with the following code:

public class Outer<T>  {

    public class Inner{
    }

    public static <T> Outer<T>.Inner get(){
        Object o = new Object();
        return (Outer<T>.Inner)o;
    }

    public static void main(String[] args) throws Exception {
        Outer.<String>get();
    }
}

2. Compile with javac using the command: javac Outer.java

You will see that javac will report problems, whereas Eclipse doesn't.
Comment 1 Srikanth Sankaran CLA 2011-01-10 03:54:42 EST
This is a bug in the javac compiler that has since been
fixed. JDK 7b100 compiles this fine.

See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6665356
Comment 2 Olivier Thomann CLA 2011-01-25 09:30:10 EST
Verified for 3.7M5 using I20110124-1800