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

Java 8 Double curly brace initialization and name collision (bug?)

XMLWordPrintable

    • x86
    • os_x

      FULL PRODUCT VERSION :
      java version "1.8.0_25"
      Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
      Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Darwin ATLMAC-105924 14.0.0 Darwin Kernel Version 14.0.0: Fri Sep 19 00:26:44 PDT 2014; root:xnu-2782.1.97~2/RELEASE_X86_64 x86_64


      A DESCRIPTION OF THE PROBLEM :
      The following class has an inner class called Entry. This code will not compile in Java 8 as the compiler assumes the Entry referenced within the double curly brace initializer is of type Map.Entry and not Scope.Entry. This code compiles in previous versions (at least 6 and 7) of the JDK but is broken in JDK 8. My question is "why?" Map.Entry is not imported in this class, so there is no reason for the compiler to assume that the value is of type Map.Entry. Is there some implicit scope being brought in or something for anonymous classes?


      REGRESSION. Last worked in version 7u67

      ADDITIONAL REGRESSION INFORMATION:
      java version "1.7.0_67"
      Java(TM) SE Runtime Environment (build 1.7.0_67-b01)
      Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mod

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Attempt to compile the code.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Compliation should be successful.
      ACTUAL -
      Compilation was unsuccessful:

      scope/Scope.java:23: error: incompatible types: scope.Scope.Entry cannot be converted to java.util.Map.Entry for (final Entry entry : entries) { ^

      scope/Scope.java:22: error: cannot find symbol put(entry.getName(), entry);

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      scope/Scope.java:23: error: incompatible types: scope.Scope.Entry cannot be converted to java.util.Map.Entry for (final Entry entry : entries) { ^

      scope/Scope.java:22: error: cannot find symbol put(entry.getName(), entry);

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      package scope;

      import java.util.HashMap;
      import java.util.HashSet;
      import java.util.Set;

      public class Scope {

          public static class Entry<T> {
              public String getName() {
                  return "Scope";
              }
          }

          public static void main(String[] args) {
              //Set of Scope.Entry
              final Set<Entry> entries = new HashSet<>();

              new HashMap<String, Entry>() {{
                  // Why does the Java 8 compiler assume this is a Map.Entry
                  // as it is not imported?
                  for (final Entry entry : entries) {
                      put(entry.getName(), entry);
                  }
              }};
          }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      The inner scoped class name needs to be fully qualified.

            dlsmith Dan Smith
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: