Repository navigation
UpgradeToJava25 generates invalid guarded enum switch case #1123
Description
What version of OpenRewrite are you using?
I am using:
- IntelliJ IDEA 2026.1.2
- OpenRewrite IntelliJ plugin version 261.22158.182
- OpenRewrite Maven plugin 6.40.0
- OpenRewrite version 8.83.0
- Recipe:
org.openrewrite.java.migrate.UpgradeToJava25
How are you running OpenRewrite?
I am running OpenRewrite from IntelliJ IDEA using the OpenRewrite plugin.
The project is a Maven multi-module project.
The active recipe is:
org.openrewrite.java.migrate.UpgradeToJava25
The invalid code was generated in one of the Maven submodules after applying the org.openrewrite.java.migrate.UpgradeToJava25 recipe from the IntelliJ plugin.
What is the smallest, simplest way to reproduce the problem?
Starting with this Java class:
class Reproducer {
enum EnumOptions {
OPTION_ONE,
OPTION_TWO
}
boolean checkMethod() {
return true;
}
void handleOptionOne() {
}
void buildMenu(EnumOptions item) {
switch (item) {
case OPTION_ONE -> {
if (checkMethod()) {
handleOptionOne();
}
}
case OPTION_TWO -> {
}
}
}
}Run the recipe:
org.openrewrite.java.migrate.UpgradeToJava25
OpenRewrite rewrites the class to:
class Reproducer {
enum EnumOptions {
OPTION_ONE,
OPTION_TWO
}
boolean checkMethod() {
return true;
}
void handleOptionOne() {
}
void buildMenu(EnumOptions item) {
switch (item) {
case OPTION_ONE when checkMethod() ->
handleOptionOne();
case OPTION_ONE -> {}
case OPTION_TWO -> {}
}
}
}This generated code does not compile because OPTION_ONE is an enum constant case label, not a pattern case label. A when guard is not valid on an enum constant case label.
The generated code also introduces a duplicate case OPTION_ONE label.
What did you expect to see?
I expected OpenRewrite to keep the original switch valid or to produce an equivalent valid Java 25 switch.
What did you see instead?
OpenRewrite generated invalid Java:
class Reproducer {
enum EnumOptions {
OPTION_ONE,
OPTION_TWO
}
boolean checkMethod() {
return true;
}
void handleOptionOne() {
}
void buildMenu(EnumOptions item) {
switch (item) {
case OPTION_ONE when checkMethod() ->
handleOptionOne();
case OPTION_ONE -> {}
case OPTION_TWO -> {}
}
}
}The problematic generated switch is:
switch (item) {
case OPTION_ONE when checkMethod() ->
handleOptionOne();
case OPTION_ONE -> {}
case OPTION_TWO -> {}
}There are two problems:
case OPTION_ONE when checkMethod() ->is invalid Java becauseOPTION_ONEis an enum constant case label.whenguards are valid for pattern case labels, not plain enum constant case labels.- The rewrite introduces a duplicate
case OPTION_ONElabel.
What is the full stack trace of any errors you encountered?
There is no OpenRewrite runtime stack trace. The recipe completes and produces source code that does not compile.
Are you interested in contributing a fix to OpenRewrite?
No.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
timtebeek commentedon Jun 1, 2026
Thanks a lot for the report; good to have this case covered as well.
Fix `RefineSwitchCases` generating invalid guard on enum constant lab…