JDK-7031023 : Cannot access private field through generic reference in 1.7
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 7
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2011-03-25
  • Updated: 2012-09-06
  • Resolved: 2011-03-25
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
javac 1.7.0-ea

A DESCRIPTION OF THE PROBLEM :
The code below compiles with Java 1.6 but compiles with errors in 1.7. The code is legal according to JLS 6.6.1 Otherwise, if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class (��7.6) that encloses the declaration of the member or constructor.

It does not happen if the access isn't through a generic type.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile the following classes below (each is standalone):
// generic on itself
public class CopyAttributes<T extends CopyAttributes<T>> {

    private int copy;

    public CopyAttributes() {
    }

    public void copyAttributesTo(T that) {
        that.copy = this.copy;
    }
}

// generic method on top level class
public class CopyAttributes2 {

    private int copy;

    public <T extends CopyAttributes2> void copyAttributesTo(T that) {
        that.copy = this.copy;
    }
}

// nested class generic on the top level class
public class CopyAttributes3 {

    private int copy;

    public static abstract class Importer<T extends CopyAttributes3> {
        public void update(T object) {
            object.copy = 0;
        }
    }
}

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
No compilers errors
ACTUAL -
Compiler errors

ERROR MESSAGES/STACK TRACES THAT OCCUR :
// Example 1
CopyAttributes.java:11: copy has private access in CopyAttributes
        that.copy = this.copy;
            ^

// Example 2
CopyAttributes2.java:8: copy has private access in CopyAttributes2
        that.copy = this.copy;
            ^

// Example 3
CopyAttributes3.java:9: copy has private access in CopyAttributes3
            object.copy = 0;
                  ^

REPRODUCIBILITY :
This bug can be reproduced always.

CUSTOMER SUBMITTED WORKAROUND :
Explicitly cast to the actual class: ((CopyAttributes<?>) that).copy = this.copy; but that requires modifying the source to compile under 1.7