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

Improve IllegalAccessException message to include the cause of the exception

    XMLWordPrintable

Details

    Backports

      Description

        See report at http://mail.openjdk.java.net/pipermail/hotspot-dev/2021-May/052203.html

        If a MethodHandles.Lookup is used to locate a MethodHandle for a varargs method whose last parameter is not an array an IllegalArgumentException will be thrown:

        > java.lang.IllegalArgumentException: not an array type: int
        > at java.base/java.lang.invoke.MethodHandleStatics.newIllegalArgumentException(MethodHandleStatics.java:138)
        > at java.base/java.lang.invoke.MethodHandle.spreadArrayChecks(MethodHandle.java:1066)
        > at java.base/java.lang.invoke.MethodHandle.asCollectorChecks(MethodHandle.java:1259)
        > at java.base/java.lang.invoke.MethodHandle.asVarargsCollector(MethodHandle.java:1427)
        > at java.base/java.lang.invoke.MethodHandle.withVarargs(MethodHandle.java:1111)
        > at java.base/java.lang.invoke.MethodHandle.setVarargs(MethodHandle.java:1634)
        > at java.base/java.lang.invoke.MethodHandles$Lookup.getDirectMethodCommon(MethodHandles.java:3755)
        > at java.base/java.lang.invoke.MethodHandles$Lookup.getDirectMethod(MethodHandles.java:3691)
        > at java.base/java.lang.invoke.MethodHandles$Lookup.findStatic(MethodHandles.java:2399)

        This makes sense, however it seems that the exception is swallowed and instead a generic IllegalAccessException is thrown:

        > Caused by: java.lang.IllegalAccessException: cannot make variable arity: package.Class.method(Object[],int)Object/invokeStatic
        > at java.base/java.lang.invoke.MemberName.makeAccessException(MemberName.java:957)
        > at java.base/java.lang.invoke.MethodHandle.setVarargs(MethodHandle.java:1636)
        > at java.base/java.lang.invoke.MethodHandles$Lookup.getDirectMethodCommon(MethodHandles.java:3755)
        > at java.base/java.lang.invoke.MethodHandles$Lookup.getDirectMethod(MethodHandles.java:3691)
        > at java.base/java.lang.invoke.MethodHandles$Lookup.findStatic(MethodHandles.java:2399)

        It would probably be helpful to make the IllegalAccessException caused by the IllegalArgumentException, or at the very least add it as a suppressed exception.
        Otherwise the only way to find the helpful exception here is through runtime debugging.

        Attachments

          Issue Links

            Activity

              People

                mchung Mandy Chung
                mchung Mandy Chung
                Votes:
                0 Vote for this issue
                Watchers:
                4 Start watching this issue

                Dates

                  Created:
                  Updated:
                  Resolved: