Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6391197

static import seems to hide non-static import in a very special case

    XMLWordPrintable

Details

    • generic, x86
    • generic, linux, windows_xp

    Backports

      Description

        FULL PRODUCT VERSION :
        java version "1.6.0-beta"
        Java(TM) 2 Runtime Environment, Standard Edition (build 1.6.0-beta-b59g)
        Java HotSpot(TM) Client VM (build 1.6.0-beta-b59g, mixed mode, sharing)

        ADDITIONAL OS VERSION INFORMATION :
        Microsoft Windows XP [Version 5.1.2600]

        A DESCRIPTION OF THE PROBLEM :
        The following piece of code produces with JDK 1.5.0_06 and JDK 1.6.0beta a compiler error.
        ####
        package test;

        import static test.Outer.Inner.CONST;
        import java.util.Iterator;

        class Outer {
          interface Inner extends Iterator {
            static String CONST = "CONST";
          }
        }
        ####
        The compilers produce the described error only in this special case. When putting the non-static import im front of the static import, the piece of code compiles fine. Example:
        ####
        import java.util.Iterator;
        import static test.Outer.Inner.CONST;
        ####
        The symbol Iterator cannot be found only for the "extends Iterator" part. When omitting this part and using Iterator inside the inner interface, the piece of code also compiles fine. Example:
        ####
        package test;

        import static test.Outer.Inner.CONST;
        import java.util.Iterator;

        class Outer {
          interface Inner {
            static String CONST = "CONST";

            Iterator iterator();
          }
        }
        ####
        The bug cannot be produced in the outer class. When trying it, the pice of code also compiles fine. Example:
        ####
        package test;

        import static test.Outer.CONST;
        import java.util.Iterator;

        interface Outer extends Iterator {
          static String CONST = "CONST";
        }
        ####
        I.e., in the very special case of a static import of an inner class/interface's field followed by a non-static import of a class/interface, the inner class/interface extends/implements, the compiler produces an error. When changing one of this assumptions, the piece of code compiles fine.

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Try to compile the following piece of code
        ####
        package test;

        import static test.Outer.Inner.CONST;
        import java.util.Iterator;

        class Outer {
          interface Inner extends Iterator {
            static String CONST = "CONST";
          }
        }
        ####

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        The compiler should compile the piece of code without any error (perhaps a warning that in inner interface Inner static import CONST and static field CONST are hiding each other).
        ACTUAL -
        The compiler produces the following compiler error
        ####
        test\Outer.java:9: cannot find symbol
        symbol : class Iterator
        location: class test.Outer
                interface Inner extends Iterator {
                                        ^
        1 error
        ####

        ERROR MESSAGES/STACK TRACES THAT OCCUR :
        test\Outer.java:9: cannot find symbol
        symbol : class Iterator
        location: class test.Outer
                interface Inner extends Iterator {
                                        ^
        1 error


        REPRODUCIBILITY :
        This bug can be reproduced always.

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

        import static test.Outer.Inner.CONST;
        import java.util.Iterator;

        class Outer {
          interface Inner extends Iterator {
            static String CONST = "CONST";
          }
        }

        ---------- END SOURCE ----------

        CUSTOMER SUBMITTED WORKAROUND :
        Simply put the non-static import in front of the static import.
        Same problem reported by a CAP member:

        J2SE Version (please include all output from java -version flag):
        java version "1.6.0"
        Java(TM) SE Runtime Environment (build 1.6.0-b105)
        Java HotSpot(TM) Client VM (build 1.6.0-b105, mixed mode, sharing)

        Does this problem occur on J2SE 1.4.x or 5.0.x or 6.0? Yes / No (pick one)
        Yes

        Operating System Configuration Information (be specific):
        Microsoft Windows XP [Version 5.1.2600]

        Compiler not finding classes following static imports

        Note: This might be a duplicate of Bug 6391197

        When compiling the code the compiler reports:

        test\Class1.java: cannot find symbol
        symbol : class JWindow
        location: class test\Class1
          private static class Class1Window extends JWindow {

        Workarounds:
        1) switching the order of the import statements works (although they often get changed back by our development tools).
        2) Commenting out the static import statements, then compiling, then putting the import statements back and compiling again with the old classes still there also works.
        This means that the compiler errors happen at the strangest of times, and in large programs it can be very difficult to work out how to workaround the problems.

        ----------------- test.Class1.java -------------------------
        package test;

        import static test.Class2.*;
        import javax.swing.*;

        public class Class1 {
          private static class Class1Window extends JWindow {
          }
        }
        ----------------- test.Class2.java -------------------------
        package test;

        import static test.Class1.*;

        public class Class2 {
        }
        --------------------------------------------------------------

        Attachments

          Issue Links

            Activity

              People

                mcimadamore Maurizio Cimadamore
                gmanwanisunw Girish Manwani (Inactive)
                Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                  Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: