JDK-7041853 : findGetter throws unexpected IllegalAccessException
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang.invoke
  • Affected Version: 7
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic
  • CPU: generic
  • Submitted: 2011-05-04
  • Updated: 2012-03-22
  • Resolved: 2011-05-14
Related Reports
Duplicate :  
Description
Please see minimized test below:

$ cat test.java
import java.lang.invoke.MethodType;
import static java.lang.invoke.MethodHandles.*;
import static java.lang.invoke.MethodHandles.Lookup.*;

public class test {

    public static void main(String[] args) throws Throwable {
        lookup().findGetter(TestCls.class, "f2", int.class);
        TestCls c =  new TestCls();
        c.f2 =  1;
    }
}

class TestCls {
    protected int f2 = 2;
}

$ javac test.java
$ java -showversion test
java version "1.7.0-ea"
Java(TM) SE Runtime Environment (build 1.7.0-ea-b140)
Java HotSpot(TM) Server VM (build 21.0-b10, mixed mode)

Exception in thread "main" java.lang.IllegalAccessException: caller class must be a subclass below the method: TestCls.f2/int, from class test
        at java.lang.invoke.MemberName.makeAccessException(MemberName.java:507)
        at java.lang.invoke.MethodHandles$Lookup.restrictReceiver(MethodHandles.java:1078)
        at java.lang.invoke.MethodHandles$Lookup.restrictProtectedReceiver(MethodHandles.java:1072)
        at java.lang.invoke.MethodHandles$Lookup.makeAccessor(MethodHandles.java:1104)
        at java.lang.invoke.MethodHandles$Lookup.makeAccessor(MethodHandles.java:1094)
        at java.lang.invoke.MethodHandles$Lookup.findGetter(MethodHandles.java:724)
        at test.main(test.java:8)
Note that findSetter has the same issue.

Comments
EVALUATION This is a bug. Same-package access to protected is allowed by the API. The logic of restrictReceiver should allow the caller class to be anything in the same package as the defining class, and return a MH which is unrestricted. The receiver type narrowing for protected members need to be documented, also.
13-05-2011