|
| 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