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

Method reference uses wrong qualifying type

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P4
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 9
    • Component/s: tools
    • Labels:
    • Subcomponent:
    • Resolved In Build:
      b47
    • Verification:
      Verified

      Backports

        Description

        A reference to a method declared in a package-access class (via a public subtype) compiles to a lambda bridge; the qualifying type in the bridge method is the declaring class, not the referenced class. This leads to an IllegalAccessError.

        ---
        package p1;
        class A {
            public static void m() { System.out.println("A.m"); }
        }
        ---
        package p1;
        public class B extends A {
        }
        ---
        package p2;
        public class Test {
            public static void main(String... args) {
                p1.B.m(); // ok
                Runnable r = p1.B::m; r.run(); // runtime error
            }
        }
        ---

        $ -> java p2.Test
        A.m
        Exception in thread "main" java.lang.IllegalAccessError: tried to access class p1.A from class p2.Test
        at p2.Test.lambda$MR$main$m$64fce12b$1(Test.java:7)
        at p2.Test$$Lambda$1/1160460865.run(Unknown Source)
        at p2.Test.main(Test.java:8)

        $ -> javap -verbose -private p2.Test
        ...
          public static void main(java.lang.String...);
            descriptor: ([Ljava/lang/String;)V
            flags: ACC_PUBLIC, ACC_STATIC, ACC_VARARGS
            Code:
              stack=1, locals=2, args_size=1
                 0: invokestatic #2 // Method p1/B.m:()V
                 3: invokedynamic #3, 0 // InvokeDynamic #0:run:()Ljava/lang/Runnable;
                 8: astore_1
                 9: aload_1
                10: invokeinterface #4, 1 // InterfaceMethod java/lang/Runnable.run:()V
                15: return
        ...
          private static void lambda$main$0();
            descriptor: ()V
            flags: ACC_PRIVATE, ACC_STATIC, ACC_SYNTHETIC
            Code:
              stack=0, locals=0, args_size=0
                 0: invokestatic #5 // Method p1/A.m:()V
                 3: return

          Issue Links

            Activity

            Hide
            dlsmith Dan Smith added a comment -
            The underlying issue -- the reason to use a bridge in the first place -- may be that the MethodHandle API doesn't provide a way to reconstruct the reference via B.m()V, rather than A.m()V. See JDK-8068253.
            Show
            dlsmith Dan Smith added a comment - The underlying issue -- the reason to use a bridge in the first place -- may be that the MethodHandle API doesn't provide a way to reconstruct the reference via B.m()V, rather than A.m()V. See JDK-8068253 .
            Hide
            sadayapalam Srikanth Adayapalam added a comment -
            I'll follow up on this one, Thanks.
            Show
            sadayapalam Srikanth Adayapalam added a comment - I'll follow up on this one, Thanks.
            Hide
            hgupdate HG Updates added a comment -
            URL: http://hg.openjdk.java.net/jdk9/dev/langtools/rev/4359df35c893
            User: jlahoda
            Date: 2015-01-13 12:02:38 +0000
            Show
            hgupdate HG Updates added a comment - URL: http://hg.openjdk.java.net/jdk9/dev/langtools/rev/4359df35c893 User: jlahoda Date: 2015-01-13 12:02:38 +0000
            Hide
            hgupdate HG Updates added a comment -
            URL: http://hg.openjdk.java.net/jdk9/jdk9/langtools/rev/4359df35c893
            User: lana
            Date: 2015-01-21 21:54:44 +0000
            Show
            hgupdate HG Updates added a comment - URL: http://hg.openjdk.java.net/jdk9/jdk9/langtools/rev/4359df35c893 User: lana Date: 2015-01-21 21:54:44 +0000

              People

              • Assignee:
                sadayapalam Srikanth Adayapalam
                Reporter:
                dlsmith Dan Smith
              • Votes:
                0 Vote for this issue
                Watchers:
                4 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: