JDK-8328747 : WrongMethodTypeException with pattern matching on switch on sealed classes
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Priority: P2
  • Status: Closed
  • Resolution: Fixed
  • Submitted: 2024-03-21
  • Updated: 2024-07-29
  • Resolved: 2024-04-05
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 23
23 b18Fixed
Related Reports
Relates :  
Description
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
Comments
Changeset: 8efe569b Author: Aggelos Biboudis <abimpoudis@openjdk.org> Date: 2024-04-05 08:05:53 +0000 URL: https://git.openjdk.org/jdk/commit/8efe569b8dc0ae865aa75757ca0e5c4cda12aa61
05-04-2024

A pull request was submitted for review. URL: https://git.openjdk.org/jdk/pull/18606 Date: 2024-04-03 14:45:35 +0000
03-04-2024