Details
Description
When switching over an enum, and for two or more case branches, groovy generates unreachable bytecode for each case branch except the last one.
For example
package mypackage import groovy.transform.CompileStatic @CompileStatic class SwitchDemo { void mySwitchMethod(MyEnum myEnum) { switch (myEnum) { case MyEnum.ONE: println "ONE" break case MyEnum.TWO: println "TWO" break case MyEnum.THREE: println "TWO" break default: println "default" break } } } @CompileStatic enum MyEnum { ONE, TWO, THREE }
generates bytecode sequences like
... L8 L9 LINENUMBER 12 L9 GOTO L10 L3 L11 LINENUMBER 12 L11 ALOAD 2 GETSTATIC mypackage/MyEnum.TWO : Lmypackage/MyEnum; INVOKESTATIC org/codehaus/groovy/runtime/ScriptBytecodeAdapter.isCase (Ljava/lang/Object;Ljava/lang/Object;)Z IFEQ L12 L10 ...
It looks like the first four lines above are not necessary. This results in
nop, nop, athrow
sequences in generated java bytecode.
It would be great if this can be cleaned up and fixed. My scenario is related to JaCoCo reports which are not accurate and a bit confusing for switch statements.
Tnx