Convert assigning Switch statements to Switch expressions#795
Convert assigning Switch statements to Switch expressions#795
Conversation
479d2a7 to
a579d9b
Compare
|
Appreciate the draft with extensive tests here! |
src/main/java/org/openrewrite/java/migrate/lang/SwitchUtils.java
Outdated
Show resolved
Hide resolved
|
Ran it against some 600 local LSTs; only saw results for a single repository: apache/maven diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/logging/impl/LogbackConfiguration.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/logging/impl/LogbackConfiguration.java
index 4481486..da4519e 100644
--- a/impl/maven-cli/src/main/java/org/apache/maven/cling/logging/impl/LogbackConfiguration.java
+++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/logging/impl/LogbackConfiguration.java
@@ -30,20 +30,13 @@ org.openrewrite.java.migrate.lang.SwitchCaseAssigningToSwitchExpression
public class LogbackConfiguration extends BaseSlf4jConfiguration {
@Override
public void setRootLoggerLevel(Level level) {
- ch.qos.logback.classic.Level value;
- switch (level) {
- case DEBUG:
- value = ch.qos.logback.classic.Level.DEBUG;
- break;
+ ch.qos.logback.classic.Level value = switch (level) {
+ case DEBUG: yield ch.qos.logback.classic.Level.DEBUG;
- case INFO:
- value = ch.qos.logback.classic.Level.INFO;
- break;
+ case INFO: yield ch.qos.logback.classic.Level.INFO;
- default:
- value = ch.qos.logback.classic.Level.ERROR;
- break;
- }
+ default: yield ch.qos.logback.classic.Level.ERROR;
+ };
((ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME)).setLevel(value);
}
diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/logging/impl/Log4j2Configuration.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/logging/impl/Log4j2Configuration.java
index 1f02e57..07e84c8 100644
--- a/impl/maven-cli/src/main/java/org/apache/maven/cling/logging/impl/Log4j2Configuration.java
+++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/logging/impl/Log4j2Configuration.java
@@ -28,20 +28,13 @@ org.openrewrite.java.migrate.lang.SwitchCaseAssigningToSwitchExpression
public class Log4j2Configuration extends BaseSlf4jConfiguration {
@Override
public void setRootLoggerLevel(Level level) {
- String value;
- switch (level) {
- case DEBUG:
- value = "debug";
- break;
+ String value = switch (level) {
+ case DEBUG: yield "debug";
- case INFO:
- value = "info";
- break;
+ case INFO: yield "info";
- default:
- value = "error";
- break;
- }
+ default: yield "error";
+ };
System.setProperty("maven.logging.root.level", value);
}
diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/logging/impl/MavenSimpleConfiguration.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/logging/impl/MavenSimpleConfiguration.java
index 5147cd8..0c5f4e2 100644
--- a/impl/maven-cli/src/main/java/org/apache/maven/cling/logging/impl/MavenSimpleConfiguration.java
+++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/logging/impl/MavenSimpleConfiguration.java
@@ -31,20 +31,13 @@ org.openrewrite.java.migrate.lang.SwitchCaseAssigningToSwitchExpression
public class MavenSimpleConfiguration extends BaseSlf4jConfiguration {
@Override
public void setRootLoggerLevel(Level level) {
- String value;
- switch (level) {
- case DEBUG:
- value = "debug";
- break;
+ String value = switch (level) {
+ case DEBUG: yield "debug";
- case INFO:
- value = "info";
- break;
+ case INFO: yield "info";
- default:
- value = "error";
- break;
- }
+ default: yield "error";
+ };
System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", value);
}Does that align with what you were seeing there? |
| void exhaustiveButCantAddDefaultAfterEmptyLabel(TrafficLight light) { | ||
| String status = "initialValue"; | ||
| switch (light) { | ||
| case RED: status = "stop"; break; | ||
| case GREEN: status = "go"; break; | ||
| case YELLOW: | ||
| } | ||
| } |
There was a problem hiding this comment.
@timtebeek
While the switch is already exhaustive, wouldn't still adding default: yield "initialValue"; at the end be safe and preserve the logic in any situation, given the last case is empty?
This would require a fix in the code but that new test made me wonder why not.
…h exhaustiveness on an enum switch selector
I've ran it on apache repos and got a few repos responding, but by far EnumOptionData optionData = new EnumOptionData(AccountType.INVALID.getValue().longValue(), AccountType.INVALID.getCode(), "Invalid");
switch (type) {
case INVALID:
optionData = new EnumOptionData(AccountType.INVALID.getValue().longValue(), AccountType.INVALID.getCode(), "Invalid");
...doesn't get migrated anymore because the |
src/main/java/org/openrewrite/java/migrate/lang/SwitchUtils.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/java/migrate/lang/SwitchUtils.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/java/migrate/lang/SwitchUtils.java
Outdated
Show resolved
Hide resolved
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
…12.0 to 3.15.0 [skip ci] Bumps [org.openrewrite.recipe:rewrite-migrate-java](https://github.com/openrewrite/rewrite-migrate-java) from 3.12.0 to 3.15.0. Release notes *Sourced from [org.openrewrite.recipe:rewrite-migrate-java's releases](https://github.com/openrewrite/rewrite-migrate-java/releases).* > 3.15.0 > ------ > > What's Changed > -------------- > > * Convert assigning Switch statements to Switch expressions by [`@pdelagrave`](https://github.com/pdelagrave) in [openrewrite/rewrite-migrate-java#795](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/795) > * Convert switch expressions to arrows where possible by [`@timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#801](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/801) > * Convert switch cases with returns to returned switch expression by [`@timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#802](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/802) > * `ReplaceStreamCollectWithToList` should look at return type by [`@timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#803](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/803) > * New recipe to adopt Lombok setter method names by [`@timo-a`](https://github.com/timo-a) in [openrewrite/rewrite-migrate-java#632](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/632) > * refactor: Static imports for Collections and Collectors by [`@timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#804](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/804) > * Preserve whitespace in `MigrateCollectionsUnmodifiableList` by [`@timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#806](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/806) > * rename settings.local.json to settings.json by [`@zieka`](https://github.com/zieka) in [openrewrite/rewrite-migrate-java#808](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/808) > * The quick fix for the tests for Jaxws Dependencies by [`@steve-aom-elliott`](https://github.com/steve-aom-elliott) in [openrewrite/rewrite-migrate-java#809](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/809) > * Refactor RewriteTest to use defaults method by [`@timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#814](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/814) > * Create `is` methods for boolean fields in `LombokValueToRecord` by [`@timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#813](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/813) > * Joda-time: Make safe migration optional by [`@amishra-u`](https://github.com/amishra-u) in [openrewrite/rewrite-migrate-java#811](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/811) > * Improve "Add explicit Common Annotations dependencies" in "Migrate to Java 11" by [`@jevanlingen`](https://github.com/jevanlingen) in [openrewrite/rewrite-migrate-java#816](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/816) > * chore(ci): bump actions/checkout from 4 to 5 by [`@dependabot`](https://github.com/dependabot)[bot] in [openrewrite/rewrite-migrate-java#817](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/817) > > **Full Changelog**: <openrewrite/rewrite-migrate-java@v3.14.0...v3.15.0> > > 3.14.1 > ------ > > What's Changed > -------------- > > * OpenRewrite v8.59.1: <https://github.com/openrewrite/rewrite> > > **Full Changelog**: <https://github.com/openrewrite/rewrite-migrate-java> > > 3.14.0 > ------ > > What's Changed > -------------- > > * `NullCheckAsSwitchCase` should only add `null` case if the switch is exhaustive by [`@Jenson3210`](https://github.com/Jenson3210) in [openrewrite/rewrite-migrate-java#769](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/769) > * Let all change-to-var recipes use a created var-Identifier instead of using a JavaTemplate by [`@jevanlingen`](https://github.com/jevanlingen) in [openrewrite/rewrite-migrate-java#793](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/793) > * Fix NPE in IteratorNext recipe when iterator() has no explicit receiver by [`@jkschneider`](https://github.com/jkschneider) in [openrewrite/rewrite-migrate-java#796](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/796) > * `LombokValueToRecord` should rewrite method references too by [`@holgpar`](https://github.com/holgpar) in [openrewrite/rewrite-migrate-java#469](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/469) > * Update test expectations to now finish idempotent `ChangeTagAttribute` in a single cycle by [`@pdelagrave`](https://github.com/pdelagrave) in [openrewrite/rewrite-migrate-java#797](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/797) > * refactor: Unwrap else block after return or throw statement by [`@timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#798](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/798) > > **Full Changelog**: <openrewrite/rewrite-migrate-java@v3.13.0...v3.14.0> > > 3.13.0 > ------ > > What's Changed > -------------- > > * refactor: org.openrewrite.mavencentral by [`@Laurens-W`](https://github.com/Laurens-W) in [openrewrite/rewrite-migrate-java#778](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/778) > * Do not add explicit `Record` class import if using `var` keyword by [`@timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#781](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/781) > * Correct groupId for `jakarta.annotation:jakarta.annotation-api` upgrade by [`@steve-aom-elliott`](https://github.com/steve-aom-elliott) in [openrewrite/rewrite-migrate-java#784](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/784) > * Keep `jakarta.annotation-api` dependency when moving to Jakarta with Spring Boot project and Jakarta annotations are used by [`@jevanlingen`](https://github.com/jevanlingen) in [openrewrite/rewrite-migrate-java#782](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/782) > * Add Bouncy Castle upgrade recipe for Java < 8 by [`@pdelagrave`](https://github.com/pdelagrave) in [openrewrite/rewrite-migrate-java#790](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/790) > * Fix `LombokValToFinalVar` in loops by [`@MBoegers`](https://github.com/MBoegers) in [openrewrite/rewrite-migrate-java#768](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/768) > * Replace JavaTemplate by a `var` identifier element for UseVarForGenericsConstructors recipe by [`@jevanlingen`](https://github.com/jevanlingen) in [openrewrite/rewrite-migrate-java#789](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/789) > * Add missing type table entries by [`@timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#792](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/792) > * Migrate Lombok's `@__` syntax to `onX_` for Java 8+ by [`@timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#794](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/794) ... (truncated) Commits * [`f65c067`](openrewrite/rewrite-migrate-java@f65c067) Restore `UpdateJakartaAnnotations2` as a deprecated placeholder * [`aa973e9`](openrewrite/rewrite-migrate-java@aa973e9) chore(ci): bump actions/checkout from 4 to 5 ([#817](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/817)) * [`a6a509e`](openrewrite/rewrite-migrate-java@a6a509e) Improve "Add explicit Common Annotations dependencies" in "Migrate to Java 11... * [`cbe5ca5`](openrewrite/rewrite-migrate-java@cbe5ca5) [Auto] SDKMAN! Java candidates as of 2025-08-11T1018 * [`180b215`](openrewrite/rewrite-migrate-java@180b215) Revert "Joda-time: Make safe migration optional ([#811](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/811))" * [`531270b`](openrewrite/rewrite-migrate-java@531270b) Joda-time: Make safe migration optional ([#811](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/811)) * [`9f029e3`](openrewrite/rewrite-migrate-java@9f029e3) Create `is` methods for boolean fields in `LombokValueToRecord` ([#813](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/813)) * [`bc6f9dc`](openrewrite/rewrite-migrate-java@bc6f9dc) Allow BouncyCastleTest to match in any order * [`eef26e4`](openrewrite/rewrite-migrate-java@eef26e4) Refactor RewriteTest to use defaults method ([#814](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/814)) * [`d9674d1`](openrewrite/rewrite-migrate-java@d9674d1) Delegate from `RemoveIllegalSemicolons` to `RemoveExtraSemicolons` * Additional commits viewable in [compare view](openrewrite/rewrite-migrate-java@v3.12.0...v3.15.0) [](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores) Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`. [//]: # (dependabot-automerge-start) [//]: # (dependabot-automerge-end) --- Dependabot commands and options You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot merge` will merge this PR after your CI passes on it - `@dependabot squash and merge` will squash and merge this PR after your CI passes on it - `@dependabot cancel merge` will cancel a previously requested merge and block automerging - `@dependabot reopen` will reopen this PR if it is closed - `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually - `@dependabot show ignore conditions` will show all of the ignore conditions of the specified dependency - `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
What's changed?
A new recipe to convert switch statements for which all cases are doing an assignment to the same variable into a switch expression that is assigned to that variable:
From:
To:
What's your motivation?
Anything in particular you'd like reviewers to focus on?
Anyone you would like to review specifically?
Have you considered any alternatives or workarounds?
Any additional context
Checklist