JDK-8010429 : Private field access via generic type - won't compile in 1.7; did in 1.6.
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 7u1,7u10
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • Submitted: 2013-03-19
  • Updated: 2014-12-15
  • Resolved: 2013-05-29
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 7
7Fixed
Related Reports
Duplicate :  
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.