简体   繁体   中英

Weird compile-time error only when class is generic

The following code compiles fine:

public class Test {

    public static interface A<Y> {
        public B<Y> foo();
    }
    public static interface B<Y> extends A<Y> {
        public A<Y> bar();
    }

    private static class Impl
        implements A, B
    {
        public B foo() {
            return this;
        }
        public A bar() {
            return null;
        }
    }

}

But when a generic parameter is introduced to top-level class Test the code no longer compiles:

public class Test<X> {

    public static interface A<Y> {
        public B<Y> foo();
    }
    public static interface B<Y> extends A<Y> {
        public A<Y> bar();
    }

    private static class Impl
        implements A, B
    {
        public B foo() {
            return this;
        }
        public A bar() {
            return null;
        }
    }

}

The error I get is:

The interface A cannot be implemented more than once with different arguments: Test.A and Test.A

What causes this weird compilation error? What can be done to fix it?

By the way, I use eclipse and Java 1.8.0_144.

Thanks for your help.

As has been suggested by Andy Turner in the comments this is a compiler bug. When compiling with the command line and javac both versions compile just fine. Only within eclipse (using the build in eclipse compiler) does the error occur.

The eclipse version where the bug occured is indeed quite outdated:

Version: Oxygen Release (4.7.0)

Build id: 20170620-1800

The bug appears to have been fixed in later versions of eclipse.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM