JDK-8148472 : static import may hide other imports
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 8u66
  • Priority: P4
  • Status: Resolved
  • Resolution: Duplicate
  • OS: linux
  • CPU: x86_64
  • Submitted: 2016-01-12
  • Updated: 2016-03-03
  • Resolved: 2016-02-22
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 9
9Resolved
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.8.0_66"
Java(TM) SE Runtime Environment (build 1.8.0_66-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.66-b17, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Linux 3.2.45-0.6.acc.624.45.283.amzn1acc.x86_64 #1 SMP Fri Nov 21 22:39:25 UTC 2015 x86_64 x86_64 x86_64 GNU/linux

A DESCRIPTION OF THE PROBLEM :
See steps to reproduce below. There is a file which fails to compile, but which should succeed in compiling. The issue seems to be that placing a static import causes a symbol to no longer be visible to the compiler.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Attempt to compile the file listed in "Source code for an executable test case" via `javac Foo.java`

This will result in a compile error (see "Error Messages" below for details).

If you comment out the static import on line 3, the file will successfully compile.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
File should compile
ACTUAL -
File fails to compile.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
Foo.java:8: error: cannot find symbol
    public static class Bar extends AbstractList<Object> {
                                    ^
  symbol:   class AbstractList
  location: class Foo
Foo.java:12: error: method does not override or implement a method from a supertype
        @Override
        ^
Foo.java:18: error: method does not override or implement a method from a supertype
        @Override
        ^
3 errors

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
package test;

// import static test.Foo.Bar.batz;
import java.util.AbstractList;

public class Foo
{
    public static class Bar extends AbstractList<Object> {
        public static void batz() {
        }

        @Override
        public Iterable<Object> get(final int index)
        {
            return null;
        }

        @Override
        public int size()
        {
            return 0;
        }
    }
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
You can fully qualify the the AbstractList class:

package test;

import static test.Foo.Bar.batz;

public class Foo
{
    public static class Bar extends java.util.AbstractList<Object> {
        public static void batz() {
        }

        @Override
        public Iterable<Object> get(final int index)
        {
            return null;
        }

        @Override
        public int size()
        {
            return 0;
        }
    }
}


Comments
This should be fixed as part of JDK-8056066 (the code change was done under JDK-7101822). The code compiles OK on: $ javac -fullversion javac full version "9-ea+105-2016-02-10-210515.javare.4433.nc"
22-02-2016

A more lazy evaluation of imports would get this correct.
29-01-2016

I think I know what the problem is, but I'm not sure if it is a bug or not. The static import is from the Foo class itself. Therefore, javac needs to look at the class, in order to find the static batz method. But since the Foo import is made before the AbstractList import, javac seems to look at the class before we have done all the imports, therefore not finding the AbstractList. If one change the order of the imports, the program compiles: import java.util.AbstractList; import static test.Foo.Bar.batz;
28-01-2016

Test result: ######### OS : Windows 7 64 bit JDK 6,7,8 : Fail
28-01-2016