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

WrongMethodTypeException with pattern matching on switch on sealed classes

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P2 P2
    • 23
    • None
    • tools
    • None
    • b18
    • Verified

      I'm seeing a WrongMethodTypeException with pattern matching on switch and sealed classes, when compiling with a recent JDK 23 javac version and targeting JDK 21 with --release.

      The change bisects to JDK-8303374 (Implement JEP 455: Primitive Types in Patterns, instanceof, and switch (Preview))

      Repro:

      public class Z {

        public static void main(String[] args) {
          f(new P());
          f(new O());
        }

        private static void f(I info) {
          switch (info) {
            case P p -> System.err.println(p);
            case O o -> System.err.println(o);
          }
        }

        static sealed interface I permits P, O {}
        private abstract static class A {}
        static final class P extends A implements I {}
        static final class O extends A implements I {}
      }

      Compile with a JDK 23 javac, and --release 21:

      $ javac -fullversion
      javac full version "23-ea+14-1075"
      $ javac --release 21 Z.java

      Executing on JDK 21 fails:

      $ java -fullversion
      openjdk full version "21-ea+34-2500"
      $ java Z
      Exception in thread "main" java.lang.BootstrapMethodError: CallSite bootstrap method initialization exception
              at java.base/java.lang.invoke.CallSite.makeSite(CallSite.java:345)
              at java.base/java.lang.invoke.MethodHandleNatives.linkCallSiteImpl(MethodHandleNatives.java:274)
              at java.base/java.lang.invoke.MethodHandleNatives.linkCallSite(MethodHandleNatives.java:264)
              at Z.f(Z.java:9)
              at Z.main(Z.java:4)
      Caused by: java.lang.invoke.WrongMethodTypeException: MethodHandle(Object,int)int should be of type (I,int)int
              at java.base/java.lang.invoke.CallSite.wrongTargetType(CallSite.java:204)
              at java.base/java.lang.invoke.CallSite.makeSite(CallSite.java:334)
              ... 4 more

      The difference between JDK 21 javac and JDK 23 javac output is:

      < 11: invokedynamic #25, 0 // InvokeDynamic #0:typeSwitch:(Ljava/lang/Object;I)I
      ---
      > 11: invokedynamic #25, 0 // InvokeDynamic #0:typeSwitch:(LZ$I;I)I

            abimpoudis Angelos Bimpoudis
            cushon Liam Miller-Cushon
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: