Skip to content

Commit 23307eb

Browse files
Rawi01rspilker
authored andcommitted
[jdk22] Adds support for unnamed variables (JEP 456)
1 parent a54ec70 commit 23307eb

File tree

3 files changed

+151
-1
lines changed

3 files changed

+151
-1
lines changed

src/delombok/lombok/delombok/PrettyPrinter.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2016-2021 The Project Lombok Authors.
2+
* Copyright (C) 2016-2024 The Project Lombok Authors.
33
*
44
* Permission is hereby granted, free of charge, to any person obtaining a copy
55
* of this software and associated documentation files (the "Software"), to deal
@@ -263,6 +263,14 @@ private boolean suppress(JCTree tree) {
263263
return false;
264264
}
265265

266+
private void print(Name name) {
267+
if (name.isEmpty()) {
268+
print("_");
269+
} else {
270+
print(name.toString());
271+
}
272+
}
273+
266274
private void print(CharSequence s) {
267275
boolean align = needsAlign;
268276
if (needsNewLine && !onNewLine) println();
@@ -1722,6 +1730,8 @@ public void visitTypeBoundKind(TypeBoundKind tree) {
17221730
printPatternCaseLabel(tree);
17231731
} else if (className.endsWith("$JCRecordPattern")) { // Introduced in JDK19
17241732
printRecordPattern(tree);
1733+
} else if (className.endsWith("$JCAnyPattern")) { // Introduced in JDK22
1734+
print("_");
17251735
} else {
17261736
throw new AssertionError("Unhandled tree type: " + tree.getClass() + ": " + tree);
17271737
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// version 22:
2+
import java.util.Arrays;
3+
import java.util.Scanner;
4+
import java.util.stream.Stream;
5+
6+
record Box<T>(T content) {
7+
}
8+
9+
record Pair<T, U>(T first, U second) {
10+
}
11+
12+
public class UnnamedVariables {
13+
void enhancedLoop() {
14+
for (String _ : Arrays.asList("")) {
15+
}
16+
}
17+
18+
void initializationInForLoop() {
19+
for (int i = 0, _ = Integer.MAX_VALUE; i < 10; i++) {
20+
}
21+
}
22+
23+
void assignment() {
24+
var _ = 1;
25+
}
26+
27+
void catchBlock() {
28+
try {
29+
} catch (Exception _) {
30+
}
31+
}
32+
33+
void tryWithResources() {
34+
try (var _ = new Scanner("")) {
35+
}
36+
}
37+
38+
void lambda() {
39+
Stream.of(1).forEach(_ -> {
40+
});
41+
}
42+
43+
Object switchStatement(Object o) {
44+
return switch (o) {
45+
case String _, Integer _ -> o;
46+
case Long _ -> o;
47+
default -> o;
48+
};
49+
}
50+
51+
void recordPattern(Object o) {
52+
if (o instanceof Pair(Box _, Box(Integer _))) {
53+
}
54+
if (o instanceof Box(String _)) {
55+
}
56+
if (o instanceof Box(var _)) {
57+
}
58+
if (o instanceof Box(_)) {
59+
}
60+
}
61+
62+
Object recordPatternSwitch(Object o) {
63+
return switch (o) {
64+
case Box(String _), Box(Integer _) -> o;
65+
case Box(Long _) -> o;
66+
case Box(_) -> o;
67+
default -> o;
68+
};
69+
}
70+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// version 22:
2+
import java.util.Arrays;
3+
import java.util.Scanner;
4+
import java.util.stream.Stream;
5+
6+
record Box<T>(T content) {
7+
}
8+
9+
record Pair<T, U>(T first, U second) {
10+
}
11+
12+
public class UnnamedVariables {
13+
void enhancedLoop() {
14+
for (String _ : Arrays.asList("")) {
15+
}
16+
}
17+
18+
void initializationInForLoop() {
19+
for (int i = 0, _ = Integer.MAX_VALUE; i < 10; i++) {
20+
}
21+
}
22+
23+
void assignment() {
24+
var _ = 1;
25+
}
26+
27+
void catchBlock() {
28+
try {
29+
} catch (Exception _) {
30+
}
31+
}
32+
33+
void tryWithResources() {
34+
try (var _ = new Scanner("")) {
35+
}
36+
}
37+
38+
void lambda() {
39+
Stream.of(1).forEach(_ -> {
40+
});
41+
}
42+
43+
Object switchStatement(Object o) {
44+
return switch (o) {
45+
case String _, Integer _ -> o;
46+
case Long _ -> o;
47+
default -> o;
48+
};
49+
}
50+
51+
void recordPattern(Object o) {
52+
if (o instanceof Pair(Box _, Box(Integer _))) {
53+
}
54+
if (o instanceof Box(String _)) {
55+
}
56+
if (o instanceof Box(var _)) {
57+
}
58+
if (o instanceof Box(_)) {
59+
}
60+
}
61+
62+
Object recordPatternSwitch(Object o) {
63+
return switch (o) {
64+
case Box(String _), Box(Integer _) -> o;
65+
case Box(Long _) -> o;
66+
case Box(_) -> o;
67+
default -> o;
68+
};
69+
}
70+
}

0 commit comments

Comments
 (0)