What happened
When running org.openrewrite.java.migrate.UpgradeToJava21 twice in succession, the first run converts if-else instanceof chains to switch statements, but does not convert assignment-based switches to switch expressions. A second run of the same recipe is needed to produce the switch expression.
Expected behavior
Per the recipe execution docs, recipes can opt into additional cycles by overriding Recipe.causesAnotherCycle(). The sub-recipe that converts assigning switch statements to switch expressions should either run within the same cycle as the if-else-to-switch conversion, or the recipe should enable an additional cycle so both transformations complete in a single invocation.
Example
Original code:
private static double convertToDouble(Object value) {
double dValue;
if (value instanceof String string) {
dValue = Double.parseDouble(string);
} else if (value instanceof Integer integer) {
dValue = integer.doubleValue();
} else if (value instanceof Long long1) {
dValue = long1.doubleValue();
} else {
dValue = (double) value;
}
return dValue;
}
After the first run:
private static double convertToDouble(Object value) {
double dValue;
switch (value) {
case String string -> dValue = Double.parseDouble(string);
case Integer integer -> dValue = integer.doubleValue();
case Long long1 -> dValue = long1.doubleValue();
default -> dValue = (double) value;
}
return dValue;
}
After a second run of the same recipe:
private static double convertToDouble(Object value) {
return switch (value) {
case String string -> Double.parseDouble(string);
case Integer integer -> integer.doubleValue();
case Long long1 -> long1.doubleValue();
default -> (double) value;
};
}
Related
What happened
When running
org.openrewrite.java.migrate.UpgradeToJava21twice in succession, the first run convertsif-else instanceofchains to switch statements, but does not convert assignment-based switches to switch expressions. A second run of the same recipe is needed to produce the switch expression.Expected behavior
Per the recipe execution docs, recipes can opt into additional cycles by overriding
Recipe.causesAnotherCycle(). The sub-recipe that converts assigning switch statements to switch expressions should either run within the same cycle as the if-else-to-switch conversion, or the recipe should enable an additional cycle so both transformations complete in a single invocation.Example
Original code:
After the first run:
After a second run of the same recipe:
Related
IfElseIfConstructToSwitchto handle return statements directly.