From d6c7cd9ca37cbfe80267b16df857a89460253fa5 Mon Sep 17 00:00:00 2001 From: Roland Grunberg Date: Fri, 14 Feb 2025 00:11:37 -0500 Subject: [PATCH] Add 'useSwitchForInstanceofPattern' to the cleanup settings. Signed-off-by: Roland Grunberg --- document/_java.learnMoreAboutCleanUps.md | 41 ++++++++++++++++++++++++ package.json | 3 +- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/document/_java.learnMoreAboutCleanUps.md b/document/_java.learnMoreAboutCleanUps.md index 09841c622..61377ebdd 100644 --- a/document/_java.learnMoreAboutCleanUps.md +++ b/document/_java.learnMoreAboutCleanUps.md @@ -432,3 +432,44 @@ switch (r) { case R _ -> {} } ``` + +### `useSwitchForInstanceofPattern` + +Convert if/else chains to pattern matching switch statements. + +For example: + +```java +int i, j; +double d; +boolean b; +if (x instanceof Integer xint) { + i = xint.intValue(); +} else if (x instanceof Double xdouble) { + d = xdouble.doubleValue(); +} else if (x instanceof Boolean xboolean) { + b = xboolean.booleanValue(); +} else { + i = 0; + d = 0.0D; + b = false; +} +``` + +becomes: + +```java +int i, j; +double d; +boolean b; +switch (x) { + case Integer xint -> i = xint.intValue(); + case Double xdouble -> d = xdouble.doubleValue(); + case Boolean xboolean -> b = xboolean.booleanValue(); + case null, default -> { + i = 0; + d = 0.0D; + b = false; + } +} +``` diff --git a/package.json b/package.json index d44eacdac..2faa6f00c 100644 --- a/package.json +++ b/package.json @@ -1314,7 +1314,8 @@ "tryWithResource", "renameFileToType", "organizeImports", - "renameUnusedLocalVariables" + "renameUnusedLocalVariables", + "useSwitchForInstanceofPattern" ] }, "default": [