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

Private field access via generic type - won't compile in 1.7; did in 1.6.

    Details

    • Subcomponent:
    • Introduced In Version:
      7

      Description

      FULL PRODUCT VERSION :
      java version " 1.7.0_10 "
      Java(TM) SE Runtime Environment (build 1.7.0_10-b18)
      Java HotSpot(TM) 64-Bit Server VM (build 23.6-b04, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      The attached source code compiles without warnings in Java 1.6 but fails with errors in Java 1.7 with:

      c:\temp>c:\tools\jdk1.7.0_10\bin\javac Foo.java
      Foo.java:16: error: data has private access in Foo
              x.data.clear();
               ^
      Foo.java:17: error: data has private access in Foo
              x.data.putAll(data);
               ^
      2 errors

      It appears that Java 1.7 will no longer allow access to private fields, even within the same class, if that access happens through a genericized reference.

      It is likewise program if the access is to a private method, rather than a private field, as in this variant of the test class:

      import java.util.TreeMap;
      import java.util.Map;

      public abstract class Foo2<V extends Foo2<V>> {

          private final Map<String,Object> data = new TreeMap<String,Object>();

          protected Foo2() {
          }

          public abstract V getThis(); /** Implement as return this */
          public abstract V getEmpty(); /** Implement as return new V() - whatever V is */

          // ... more methods here ...

          public V copy() {
              V x = getEmpty();
              x.theData().clear();
              x.theData().putAll(data);
              return x;
          }

          private Map<String,Object> theData() {
              return data;
          }

      }

      The attached code is a greatly reduced version of an in-house library class what worked in 1.6 (javac 1.6.0_31) but will no longer compile in 1.7 (1.7.0_10).

      REGRESSION. Last worked in version 6u31

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile the example Foo.java class in both 1.6 and 1.7. 1.7 won't compile it.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I believe 1.7 should compile this class.
      ACTUAL -
      1.7 doesn't compile with: 'error: data has private access in Foo'

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      c:\temp>c:\tools\jdk1.7.0_10\bin\javac Foo.java
      Foo.java:16: error: data has private access in Foo
              x.data.clear();
               ^
      Foo.java:17: error: data has private access in Foo
              x.data.putAll(data);
               ^
      2 errors

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.util.TreeMap;
      import java.util.Map;

      public abstract class Foo<V extends Foo<V>> {

          private final Map<String,Object> data = new TreeMap<String,Object>();

          protected Foo() {
          }

          public abstract V getThis(); /** Implement as return this */
          public abstract V getEmpty(); /** Implement as return new V() - whatever V is */

          // ... more methods here ...

          public V copy() {
              V x = getEmpty();
              x.data.clear();
              x.data.putAll(data);
              return x;
          }

      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      None at the moment.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                mcimadamore Maurizio Cimadamore
                Reporter:
                webbuggrp Webbug Group
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: