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.