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

Lookup::accessClass fails with IAE when accessing an arrayClass with a protected inner class as component class

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P3
    • Resolution: Fixed
    • Affects Version/s: 16
    • Fix Version/s: 17
    • Component/s: core-libs
    • Labels:
      None
    • Subcomponent:
    • Resolved In Build:
      b29
    • CPU:
      generic
    • OS:
      generic

      Backports

        Description

        MethodHandles$Lookup.accessClass does not follow its documentation:
        https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/lang/invoke/MethodHandles.Lookup.html#accessClass(java.lang.Class)

        Specifically, it throws an exception when accessing an Array Class with a protected inner class as component class when using lookup object with full capabilities
        (ie. java.lang.IllegalAccessException: access violation: class [Lp.A$TEST;, from q.B (unnamed module @60215eee) in the testcase provided)

        The expected behavior is accessClass should allow the access and return the passed in class.

        Testcase:

        package p;

        public class A {
            protected enum TEST { ONE }
        }



        package q;

        import p.A;

        import java.lang.invoke.MethodHandle;
        import java.lang.invoke.MethodHandles;
        import java.lang.invoke.MethodType;

        public class B extends A {
            public static void main(String[] args) throws Throwable {
                TEST[] arr = new TEST[1];
                Class<?> arrClass = arr.getClass();

                //1) is TEST[] visible
                try {
                    MethodHandles.Lookup l = MethodHandles.lookup();
                    Class<?> accessClass = l.accessClass(arrClass);
                    System.out.println("Passed! accessClass is: "
                                       + accessClass.getName());
                } catch (Exception e) {
                    System.out.println(e);
                }

                //2) Byte code behaviour with TEST[]
                meth(null);

                //3) MH lookup with TEST[]
                MethodHandle mh = MethodHandles.lookup().findStatic(B.class, "meth", MethodType.methodType(void.class, TEST[].class));
                mh.invoke(null);
            }

            public static void meth(TEST[] arr) {
                System.out.println("called method");
            }
        }

          Attachments

            Issue Links

              Activity

                People

                Assignee:
                mchung Mandy Chung
                Reporter:
                rpatil Ramanand Patil
                Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                  Dates

                  Created:
                  Updated:
                  Resolved: