Skip to content

Commit

Permalink
Add 'useSwitchForInstanceofPattern' to the cleanup settings.
Browse files Browse the repository at this point in the history
Signed-off-by: Roland Grunberg <[email protected]>
  • Loading branch information
rgrunber committed Feb 14, 2025
1 parent 907578a commit d6c7cd9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
41 changes: 41 additions & 0 deletions document/_java.learnMoreAboutCleanUps.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
```
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1314,7 +1314,8 @@
"tryWithResource",
"renameFileToType",
"organizeImports",
"renameUnusedLocalVariables"
"renameUnusedLocalVariables",
"useSwitchForInstanceofPattern"
]
},
"default": [
Expand Down

0 comments on commit d6c7cd9

Please sign in to comment.