Java – cast to inner classes using generics

Consider the following codes:

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 was successfully compiled in eclipse, but could not be compiled in javac:

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 an error in javac or eclipse?

If I change the transformation to (outer. Inner) O, it compiles, despite a warning:

Solar 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

Javac version: 1.6 0_ twenty-one

Solution

I found that this was an error in the javac compiler and later fixed it JDK 7b100 compiles this fine

see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6665356

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>