Skip to content

Commit f877241

Browse files
committed
Add java22 samples from PMD
1 parent caeaf59 commit f877241

File tree

4 files changed

+125
-3
lines changed

4 files changed

+125
-3
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
- uses: actions/setup-java@v3
99
with:
1010
distribution: 'temurin'
11-
java-version: '21'
11+
java-version: '22-ea'
1212
cache: 'maven'
1313
- name: Build with Maven
1414
run: ./mvnw --show-version --no-transfer-progress --errors --batch-mode package
11.4 KB
Binary file not shown.

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
<packaging>jar</packaging>
88

99
<properties>
10-
<maven.compiler.release>21</maven.compiler.release>
10+
<maven.compiler.release>22</maven.compiler.release>
1111
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1212
<!-- fixing the build timestamp of the jar so that the jar file only changes, if content changes.
1313
the actual timestamp is irrelevant. -->
14-
<project.build.outputTimestamp>2022-09-26T17:00:00Z</project.build.outputTimestamp>
14+
<project.build.outputTimestamp>1980-01-01T12:00:00Z</project.build.outputTimestamp>
1515
</properties>
1616

1717
<build>
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// SPDX-License-Identifier: BSD-3-Clause
2+
package net.sourceforge.pmd.java.regression.tests.java22;
3+
4+
import java.util.ArrayDeque;
5+
import java.util.List;
6+
import java.util.Queue;
7+
import java.util.stream.Collectors;
8+
9+
/**
10+
* @see <a href="https://openjdk.org/jeps/443">JEP 443: Unnamed Patterns and Variables (Preview)</a> (Java 21)
11+
* @see <a href="https://openjdk.org/jeps/456">JEP 456: Unnamed Variables & Patterns</a> (Java 22)
12+
*/
13+
class Jep456_UnamedPatternsAndVariables {
14+
record Point(int x, int y) { }
15+
enum Color { RED, GREEN, BLUE }
16+
record ColoredPoint(Point p, Color c) { }
17+
18+
void unnamedPatterns1() {
19+
ColoredPoint r = new ColoredPoint(new Point(3,4), Color.GREEN);
20+
21+
if (r instanceof ColoredPoint(Point p, Color _)) {
22+
System.out.println(p.x() + " " + p.y());
23+
}
24+
25+
if (r instanceof ColoredPoint(Point(int x, int y), _)) {
26+
System.out.println(x + " " + y);
27+
}
28+
}
29+
30+
sealed abstract class Ball permits RedBall, BlueBall, GreenBall { }
31+
final class RedBall extends Ball { }
32+
final class BlueBall extends Ball { }
33+
final class GreenBall extends Ball { }
34+
35+
record Box<T extends Ball>(T content) { }
36+
37+
void unnamedPatterns2() {
38+
Box<? extends Ball> b = new Box<>(new RedBall());
39+
switch (b) {
40+
case Box(RedBall _) -> processBox(b);
41+
case Box(BlueBall _) -> processBox(b);
42+
case Box(GreenBall _) -> stopProcessing();
43+
}
44+
45+
switch (b) {
46+
case Box(RedBall _), Box(BlueBall _) -> processBox(b);
47+
case Box(GreenBall _) -> stopProcessing();
48+
case Box(_) -> pickAnotherBox();
49+
}
50+
51+
int x = 42;
52+
switch (b) {
53+
// multiple patterns guarded by one guard
54+
case Box(RedBall _), Box(BlueBall _) when x == 42 -> processBox(b);
55+
case Box(_) -> pickAnotherBox();
56+
}
57+
}
58+
59+
private void processBox(Box<? extends Ball> b) {}
60+
private void stopProcessing() {}
61+
private void pickAnotherBox() {}
62+
63+
class Order {}
64+
private static final int LIMIT = 10;
65+
private int sideEffect() {
66+
return 0;
67+
}
68+
69+
void unnamedVariables(List<Order> orders) {
70+
int total = 0;
71+
for (Order _ : orders) {
72+
if (total < LIMIT) {
73+
total++;
74+
}
75+
}
76+
System.out.println("total: " + total);
77+
78+
for (int i = 0, _ = sideEffect(); i < 10; i++) {
79+
System.out.println(i);
80+
}
81+
82+
Queue<Integer> q = new ArrayDeque<>(); // x1, y1, z1, x2, y2, z2 ..
83+
while (q.size() >= 3) {
84+
int x = q.remove();
85+
int y = q.remove();
86+
int _ = q.remove(); // z is unused
87+
Point p = new Point(x, y);
88+
}
89+
while (q.size() >= 3) {
90+
var x = q.remove();
91+
var _ = q.remove();
92+
var _ = q.remove();
93+
Point p = new Point(x, 0);
94+
}
95+
}
96+
97+
static class ScopedContext implements AutoCloseable {
98+
@Override
99+
public void close() { }
100+
public static ScopedContext acquire() {
101+
return new ScopedContext();
102+
}
103+
}
104+
105+
void unusedVariables2() {
106+
try (var _ = ScopedContext.acquire()) {
107+
//... acquiredContext not used ...
108+
}
109+
110+
String s = "123";
111+
try {
112+
int i = Integer.parseInt(s);
113+
System.out.println(i);
114+
} catch (NumberFormatException _) {
115+
System.out.println("Bad number: " + s);
116+
} catch (Exception _) {
117+
System.out.println("error...");
118+
}
119+
120+
List.of("a", "b").stream().collect(Collectors.toMap(String::toUpperCase, _ -> "NO_DATA"));
121+
}
122+
}

0 commit comments

Comments
 (0)