Bug 330733 - [1.5][compiler] Casting to an inner class with generics compiles in Eclipse, but fails in javac
Summary: [1.5][compiler] Casting to an inner class with generics compiles in Eclipse, ...
Status: VERIFIED NOT_ECLIPSE
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.6   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: 3.7 M5   Edit
Assignee: Srikanth Sankaran CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-11-20 06:10 EST by fahdshariff CLA
Modified: 2011-01-25 09:30 EST (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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