Skip to content

Commit

Permalink
Add java22 samples from PMD
Browse files Browse the repository at this point in the history
  • Loading branch information
adangel committed Mar 5, 2024
1 parent caeaf59 commit f877241
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
- uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '21'
java-version: '22-ea'
cache: 'maven'
- name: Build with Maven
run: ./mvnw --show-version --no-transfer-progress --errors --batch-mode package
Binary file modified java-regression-tests-1.0.0-SNAPSHOT.jar
Binary file not shown.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
<packaging>jar</packaging>

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

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// SPDX-License-Identifier: BSD-3-Clause
package net.sourceforge.pmd.java.regression.tests.java22;

import java.util.ArrayDeque;
import java.util.List;
import java.util.Queue;
import java.util.stream.Collectors;

/**
* @see <a href="https://openjdk.org/jeps/443">JEP 443: Unnamed Patterns and Variables (Preview)</a> (Java 21)
* @see <a href="https://openjdk.org/jeps/456">JEP 456: Unnamed Variables & Patterns</a> (Java 22)
*/
class Jep456_UnamedPatternsAndVariables {
record Point(int x, int y) { }
enum Color { RED, GREEN, BLUE }
record ColoredPoint(Point p, Color c) { }

void unnamedPatterns1() {
ColoredPoint r = new ColoredPoint(new Point(3,4), Color.GREEN);

if (r instanceof ColoredPoint(Point p, Color _)) {
System.out.println(p.x() + " " + p.y());
}

if (r instanceof ColoredPoint(Point(int x, int y), _)) {
System.out.println(x + " " + y);
}
}

sealed abstract class Ball permits RedBall, BlueBall, GreenBall { }
final class RedBall extends Ball { }
final class BlueBall extends Ball { }
final class GreenBall extends Ball { }

record Box<T extends Ball>(T content) { }

void unnamedPatterns2() {
Box<? extends Ball> b = new Box<>(new RedBall());
switch (b) {
case Box(RedBall _) -> processBox(b);
case Box(BlueBall _) -> processBox(b);
case Box(GreenBall _) -> stopProcessing();
}

switch (b) {
case Box(RedBall _), Box(BlueBall _) -> processBox(b);
case Box(GreenBall _) -> stopProcessing();
case Box(_) -> pickAnotherBox();
}

int x = 42;
switch (b) {
// multiple patterns guarded by one guard
case Box(RedBall _), Box(BlueBall _) when x == 42 -> processBox(b);
case Box(_) -> pickAnotherBox();
}
}

private void processBox(Box<? extends Ball> b) {}
private void stopProcessing() {}
private void pickAnotherBox() {}

class Order {}
private static final int LIMIT = 10;
private int sideEffect() {
return 0;
}

void unnamedVariables(List<Order> orders) {
int total = 0;
for (Order _ : orders) {
if (total < LIMIT) {
total++;
}
}
System.out.println("total: " + total);

for (int i = 0, _ = sideEffect(); i < 10; i++) {
System.out.println(i);
}

Queue<Integer> q = new ArrayDeque<>(); // x1, y1, z1, x2, y2, z2 ..
while (q.size() >= 3) {
int x = q.remove();
int y = q.remove();
int _ = q.remove(); // z is unused
Point p = new Point(x, y);
}
while (q.size() >= 3) {
var x = q.remove();
var _ = q.remove();
var _ = q.remove();
Point p = new Point(x, 0);
}
}

static class ScopedContext implements AutoCloseable {
@Override
public void close() { }
public static ScopedContext acquire() {
return new ScopedContext();
}
}

void unusedVariables2() {
try (var _ = ScopedContext.acquire()) {
//... acquiredContext not used ...
}

String s = "123";
try {
int i = Integer.parseInt(s);
System.out.println(i);
} catch (NumberFormatException _) {
System.out.println("Bad number: " + s);
} catch (Exception _) {
System.out.println("error...");
}

List.of("a", "b").stream().collect(Collectors.toMap(String::toUpperCase, _ -> "NO_DATA"));
}
}

0 comments on commit f877241

Please sign in to comment.