-
Bug
-
Resolution: Cannot Reproduce
-
P4
-
None
-
17
-
generic
-
generic
-
Not verified
A DESCRIPTION OF THE PROBLEM :
While using the JDK 17.0.2 preview feature JEP 406 (pattern matching with switch), any type of switch case statement/expression that switches on an Interface with static imported constants produces this bug.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
-create two enums
-create an interface
-let both enums implement the interface
-create a class in a different file
-create a static import for one of the enum constants
-switch on any element that implements the interface
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
I expected an error that tells the user to specify which enum should get choosen, so that he has to write smth like:
switch (e) {
case E1.A -> ...
case E2.A -> ...
}
irregardless of the static import.
ACTUAL -
Fehler: Hauptklasse test.Test kann nicht initialisiert werden
Ursache: java.lang.VerifyError: Bad type on operand stack
Exception Details:
Location:
test/Test.main([Ljava/lang/String;)V @5: tableswitch
Reason:
Type 'test/E$E1' (current frame, stack[0]) is not assignable to integer
Current Frame:
bci: @5
flags: { }
locals: { '[Ljava/lang/String;', 'test/E$E1' }
stack: { 'test/E$E1' }
Bytecode:
0000000: b200 104c 2baa 0000 0000 001e 0000 0001
0000010: 0000 0001 0000 0013 b200 1612 1cb6 001e
0000020: a700 0dbb 0024 5912 26b7 0028 bfb1
Stackmap Table:
append_frame(@24,Object[#47])
same_frame(@35)
same_frame(@45)
---------- BEGIN SOURCE ----------
-------------------------------FILE 1------------------------------------------
package test;
import static test.E.E1.A; //Static import of E1.A
public class Test {
public static void main(String[] args) {
E e = A; // This should be E1.A
switch (e) {
case A -> System.out.println("Text"); // This should get triggered because e is E1.A but throws the error instead.
default -> throw new UnsupportedOperationException("Not implemented");
}
}
}
-------------------------------FILE 2------------------------------------------
public interface E {
enum E1 implements E {
A, B, C
}
enum E2 implements E {
A, B, C
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Use instanceof and a whole lot of if-else statements.
FREQUENCY : always
While using the JDK 17.0.2 preview feature JEP 406 (pattern matching with switch), any type of switch case statement/expression that switches on an Interface with static imported constants produces this bug.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
-create two enums
-create an interface
-let both enums implement the interface
-create a class in a different file
-create a static import for one of the enum constants
-switch on any element that implements the interface
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
I expected an error that tells the user to specify which enum should get choosen, so that he has to write smth like:
switch (e) {
case E1.A -> ...
case E2.A -> ...
}
irregardless of the static import.
ACTUAL -
Fehler: Hauptklasse test.Test kann nicht initialisiert werden
Ursache: java.lang.VerifyError: Bad type on operand stack
Exception Details:
Location:
test/Test.main([Ljava/lang/String;)V @5: tableswitch
Reason:
Type 'test/E$E1' (current frame, stack[0]) is not assignable to integer
Current Frame:
bci: @5
flags: { }
locals: { '[Ljava/lang/String;', 'test/E$E1' }
stack: { 'test/E$E1' }
Bytecode:
0000000: b200 104c 2baa 0000 0000 001e 0000 0001
0000010: 0000 0001 0000 0013 b200 1612 1cb6 001e
0000020: a700 0dbb 0024 5912 26b7 0028 bfb1
Stackmap Table:
append_frame(@24,Object[#47])
same_frame(@35)
same_frame(@45)
---------- BEGIN SOURCE ----------
-------------------------------FILE 1------------------------------------------
package test;
import static test.E.E1.A; //Static import of E1.A
public class Test {
public static void main(String[] args) {
E e = A; // This should be E1.A
switch (e) {
case A -> System.out.println("Text"); // This should get triggered because e is E1.A but throws the error instead.
default -> throw new UnsupportedOperationException("Not implemented");
}
}
}
-------------------------------FILE 2------------------------------------------
public interface E {
enum E1 implements E {
A, B, C
}
enum E2 implements E {
A, B, C
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Use instanceof and a whole lot of if-else statements.
FREQUENCY : always