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

findGetter throws unexpected IllegalAccessException

XMLWordPrintable

      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.

            jrose John Rose
            ygaevsky Yuri Gaevsky (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: