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

Single static import and single type import collision not reported

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 7
    • tools
    • x86
    • windows_7

      FULL PRODUCT VERSION :
      Java SE Runtime Env. build 1.7.0-b147

      ADDITIONAL OS VERSION INFORMATION :
      MS Windows Version 6.1.7601

      A DESCRIPTION OF THE PROBLEM :
      According to JLS section 7.5.3, "If a compilation unit contains both a single-static-import declaration that imports a type whose simple name is n, and a single-type-import declaration (§7.5.1) that imports a type whose simple name is n, a compile-time error occurs."
      However, in the test case given below no error is reported, even though the (implicitly) static interface bug.Animal.FROG is imported by two import statements.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Try to compile this code snippet

      =========bug/Animal.java=======================

          package bug;

          public enum Animal {
            FROG;

            public interface FROG {}
          }

      =========p/Imports.java==========================
         package p;

          import bug.Animal;
          import bug.Animal.FROG; //(1)
          import static bug.Animal.FROG; //(2)

          public class Imports {
            public static void main(String[] args) {
              print(FROG);
            }

            static FROG print(Animal animal) {
              System.out.println(animal.name());
              return null;
            }
          }

      While import statement marked (1) imports the type bug.Animal.FROG, the statement marked (2) imports the field bug.Animal.FROG as well as the type bug.Animal.FROG. Hence, EXPECTED: there should be a compile error about conflicting imports. ACTUAL: compiles successfully.

      (To check that the single type import (1) indeed imports the type bug.Animal.FROG, the above snippet can be compiled by removing the static import and the sysout line)


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
        Program should report conflicting imports because both (1) and (2) import the interface bug.Animal.FROG
      ACTUAL -
        Program compiles

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      =========bug/Animal.java=======================

          package bug;

          public enum Animal {
            FROG;

            public interface FROG {}
          }

      =========p/Imports.java==========================
         package p;

          import bug.Animal;
          import bug.Animal.FROG; //(1)
          import static bug.Animal.FROG; //(2)

          public class Imports {
            public static void main(String[] args) {
              print(FROG);
            }

            static FROG print(Animal animal) {
              System.out.println(animal.name());
              return null;
            }
          }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Use the fully qualified name instead of importing

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: