简体   繁体   中英

Interesting compile problems in Oracle JDK 1.7, 1.8 - [Compiling depends on order of imports]

My teammate may have found an interesting bug in jdk compiler. It seems to depend on import sequence when trying to compile a class with 3-level nested class.

FrameLayoutCompat.java

package com.external;

public class FrameLayoutCompat
{

}

First.java

package com.nested;

import com.nested.First.Second.Third;
import com.external.FrameLayoutCompat;

public class First
{
    public static class Second extends FrameLayoutCompat
    {
        public static class Third
        {

        }
    }

    public static class Second2
    {
        public static class Third2
        {
            private Third mThird;
        }
    }
}

Try to compile FrameLayoutCompat.java First.java

javac com/external/FrameLayoutCompat.java
javac com/external/First.java

Here's the error.

com/nested/First.java:8: error: cannot find symbol
    public static class Second extends FrameLayoutCompat
                                       ^
  symbol:   class FrameLayoutCompat
  location: class First
1 error

But if we change the code by switching the import sequence. It works.

package com.nested;

import com.external.FrameLayoutCompat;
import com.nested.First.Second.Third;

public class First
{
    public static class Second extends FrameLayoutCompat
    {
        public static class Third
        {

        }
    }

    public static class Second2
    {
        public static class Third2
        {
            private Third mThird;
        }
    }
}

We tried the ecj, eclipse java compiler, it works also, so it may be a bug in Oracle jdk compiler.It works. Any comment?

java -jar ../ecj-4.4.jar -source 1.8 .

[Edited] I have try this to compile. Because other thinks it's better to compile them together. But the problem is that: the import sequence matters, instead of the 'cannot find symbol' error. Because if you try to change the sequence of import, it works well.

javac -s ../src -d ../build com\external\FrameLayoutCompat.java com\nested\First.java

Here's the zip file I saved in Evernote. It's worth a try. https://www.evernote.com/shard/s70/sh/8ed81644-5b90-4008-aa33-3b9e3aa7904a/5f8ba97f96d2132b

[edit] Find a JPE for this, it's an know issue in JDK for javac command.It can be closed. JEP 216: Process Import Statements Correctly

The reason is "extends FrameLayoutCompat":

When you use,

import com.nested.First.Second.Third;
import com.external.FrameLayoutCompat;

you are trying to import the class called "Third" which exists within class Second which exists within class First.

But the Second class is declared as:

public static class Second extends FrameLayoutCompat.

So it needs FrameLayoutCompat without which it can't compile. And if class Second doesn't compile, then class First won't compile too. And hence the error:

Try commenting

// extends FrameLayoutCompat

and instead use

FrameLayoutCompat layout;

and you'd find the order doesn't matter

Hope this helps.

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