I experienced a weird behavior of Java 1.8.311 (also older ones) and how it handles generic return types in combination with switch-statements:
public static void main(String[] args) {
String test = "test";
switch (Objects.requireNonNull(test)) {
case "test":
test = "OK";
break;
default:
test = "NOK";
}
System.err.println(test);
}
On execution the JVM seems to exit immediately with a JNI error. Can this be considered as a Java bug?
Edit: Stack-Trace:
Exception in thread "main" java.lang.VerifyError: Bad type on operand stack
Exception Details:
Location:
forTestCase/Main.main([Ljava/lang/String;)V @9: invokevirtual
Reason:
Type 'java/lang/Object' (current frame, stack[0]) is not assignable to 'java/lang/String'
Current Frame:
bci: @9
flags: { }
locals: { '[Ljava/lang/String;', 'java/lang/String', 'java/lang/Object' }
stack: { 'java/lang/Object' }
Bytecode:
0x0000000: 1210 4c2b b800 1259 4db6 0018 ab00 0000
0x0000010: 0000 0026 0000 0001 0036 4492 0000 0014
0x0000020: 2c12 10b6 001e 9a00 06a7 0009 1222 4ca7
0x0000030: 0006 1224 4cb2 0026 2bb6 002c b1
Stackmap Table:
append_frame(@32,Object[#25],Object[#25])
same_frame(@44)
same_frame(@50)
chop_frame(@53,1)
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:650)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:632)