Skip to content

Commit aa44a59

Browse files
committed
Add settings for some more cleanups.
- addFinalModifier: Add final modifier where possible - instanceofPatternMatch: Use pattern matching for instanceof checks - lambdaExpression: Convert anonymmous class usage to lambda expressions - switchExpression: Convert switch statement to switch expression Signed-off-by: Roland Grunberg <[email protected]>
1 parent df15a99 commit aa44a59

File tree

2 files changed

+107
-1
lines changed

2 files changed

+107
-1
lines changed

document/_java.learnMoreAboutCleanUps.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,106 @@ becomes:
171171
String message = getMessage();
172172
boolean result1 = "text".equals(message);
173173
boolean result2 = "text".equalsIgnoreCase(message);
174+
```
175+
176+
### `addFinalModifier`
177+
178+
Use the `final` modifier for variable declarations wherever it is possible.
179+
180+
For example:
181+
182+
```java
183+
private int i= 0;
184+
public void foo(int j) {
185+
int k, h;
186+
h= 0;
187+
}
188+
```
189+
190+
becomes:
191+
192+
```java
193+
private final int i= 0;
194+
public void foo(final int j) {
195+
final int k;
196+
int h;
197+
h= 0;
198+
}
199+
```
200+
201+
### `instanceofPatternMatch`
202+
203+
Use pattern matching for the `instanceof` operator wherever possible. It is only applicable for Java level 15 or higher.
204+
205+
For example:
206+
207+
```java
208+
if (object instanceof Integer) {
209+
Integer i = (Integer) object;
210+
return i.intValue();
211+
}
212+
```
213+
214+
becomes:
215+
216+
```java
217+
if (object instanceof Integer i) {
218+
return i.intValue();
219+
}
220+
```
221+
222+
223+
### `lambdaExpression`
224+
225+
Convert anonymous class declarations for functional interfaces to lambda expressions wherever possible. It is only applicable for Java level 8 or above.
226+
227+
For example:
228+
229+
```java
230+
IntConsumer c = new IntConsumer() {
231+
@Override public void accept(int value) {
232+
System.out.println(i);
233+
}
234+
};
235+
```
236+
237+
becomes:
238+
239+
```java
240+
IntConsumer c = i -> {
241+
System.out.println(i);
242+
};
243+
```
244+
245+
### `switchExpression`
246+
247+
Convert switch statements to switch expressions wherever possible. It is only applicable for Java level 14 or above.
248+
249+
**Note** : _Switch statements that use control statements such as nested switch statements, if/else blocks, for/while loops are not considered as is the case for return/continue statements. All cases of the switch statement must either have a last assignment statement that sets the same variable/field as other cases, or else has a throw statement. Fall-through is allowed between cases but only if there are no other statements in between. The switch statement must have a default case unless the switch expression is an enum type and all possible enum values are represented in the cases._
250+
251+
For example:
252+
253+
```java
254+
int i;
255+
switch(j) {
256+
case 1:
257+
i = 3;
258+
break;
259+
case 2:
260+
i = 4;
261+
break;
262+
default:
263+
i = 0;
264+
break;
265+
}
266+
```
267+
268+
becomes:
269+
270+
```java
271+
int i = switch(j) {
272+
case 1 -> 3;
273+
case 2 -> 4;
274+
default -> 0;
275+
};
174276
```

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,11 @@
10151015
"addOverride",
10161016
"addDeprecated",
10171017
"stringConcatToTextBlock",
1018-
"invertEquals"
1018+
"invertEquals",
1019+
"addFinalModifier",
1020+
"instanceofPatternMatch",
1021+
"lambdaExpression",
1022+
"switchExpression"
10191023
]
10201024
},
10211025
"default": [],

0 commit comments

Comments
 (0)