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

MethodHandle::invoke throws ClassCastException

    XMLWordPrintable

    Details

      Description

      A DESCRIPTION OF THE PROBLEM :
      Starting with jdk 18-ea, a ClassCastException is thrown on MethodHandle::invoke.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the attached test case.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      JRE 11.0.6+10-LTS
      callsite


      JRE 16.0.2+7
      callsite

      JRE 17+35-LTS
      callsite

      ACTUAL -
      JRE 18-ea+24-1608
      Exception in thread "main" java.lang.ClassCastException: Cannot cast [Ljava.lang.Object; to Invoke$MyParam
      at java.base/java.lang.Class.cast(Class.java:3921)
      at Invoke.<init>(Invoke.java:20)
      at Invoke.main(Invoke.java:11)


      ---------- BEGIN SOURCE ----------
      import java.lang.invoke.MethodHandle;
      import java.lang.invoke.MethodHandles;
      import java.lang.invoke.MethodHandles.Lookup;

      public class Invoke {
      private static final Lookup lookup = MethodHandles.lookup();
      private final MethodHandle mh;

      public static void main(final String[] args) throws Throwable {
      System.out.println("JRE " + Runtime.version());
      new Invoke();
      }

      public Invoke() throws Throwable {
      final var targetClass = this;
      final var method = targetClass.getClass().getDeclaredMethod("callsite", MyParam.class);

      final var privateLookup = MethodHandles.privateLookupIn(targetClass.getClass(), lookup);
      mh = privateLookup.unreflect(method).bindTo(targetClass);
      mh.invoke(new MyParam());
      }

      static class MyParam {}

      void callsite(final MyParam event) { System.out.println("callsite"); }
      }

      ---------- END SOURCE ----------

      FREQUENCY : always


        Attachments

          Activity

            People

            Assignee:
            tongwan Andrew Wang
            Reporter:
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: