]> source.dussan.org Git - aspectj.git/commitdiff
Add tests for Java 21 unnamed patterns
authorAlexander Kriegisch <Alexander@Kriegisch.name>
Sun, 10 Dec 2023 09:55:33 +0000 (16:55 +0700)
committerAlexander Kriegisch <Alexander@Kriegisch.name>
Mon, 11 Dec 2023 02:38:37 +0000 (03:38 +0100)
TODO: Activate after
https://github.com/eclipse-jdt/eclipse.jdt.core/issues/893 is done.
Signed-off-by: Alexander Kriegisch <Alexander@Kriegisch.name>
tests/features1921/java21/UnnamedPatternsPreview1.java [new file with mode: 0644]
tests/features1921/java21/UnnamedPatternsPreview1Aspect.aj [new file with mode: 0644]
tests/src/test/java/org/aspectj/systemtest/ajc1921/Java21PreviewFeaturesTests.java
tests/src/test/resources/org/aspectj/systemtest/ajc1921/ajc1921.xml

diff --git a/tests/features1921/java21/UnnamedPatternsPreview1.java b/tests/features1921/java21/UnnamedPatternsPreview1.java
new file mode 100644 (file)
index 0000000..badb34c
--- /dev/null
@@ -0,0 +1,85 @@
+import java.awt.*;
+import java.util.List;
+import java.util.PriorityQueue;
+import java.util.Queue;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+/**
+ * Examples taken from <a href="https://openjdk.org/jeps/443">JEP 443</a>
+ */
+public class UnnamedPatternsPreview1 {
+  public static void main(String[] args) {
+    // An enhanced for loop with side effects
+    int acc = 0;
+    final int LIMIT = 2;
+    for (Order _ : List.of(new Order(), new Order(), new Order())) {
+      if (acc < LIMIT)
+        acc++;
+    }
+    System.out.println(acc);
+
+    // The initialisation of a basic for loop can declare unnamed local variables
+    for (int i = 0, _ = sideEffect(); i < 2; i++) {
+      System.out.println(i);
+    }
+
+    // An assignment statement, where the result of the expression on the right hand side is not needed
+    Queue<Integer> q = new PriorityQueue<>(List.of(1, 2, 3, 4, 5, 6));
+    while (q.size() >= 3) {
+      var x = q.remove();
+      var y = q.remove();
+      var _ = q.remove();
+      System.out.println(new Point(x, y));
+    }
+
+    // The same unnamed variable name '_' can be used in multiple assignment statements
+    q = new PriorityQueue<>(List.of(1, 2, 3, 4, 5, 6));
+    while (q.size() >= 3) {
+      var x = q.remove();
+      var _ = q.remove();
+      var _ = q.remove();
+      System.out.println(new Point(x, 0));
+    }
+
+    // Unnamed variables can be used in one or multiple catch blocks
+    String s = "123xy";
+    try {
+      int i = Integer.parseInt(s);
+      System.out.println(i);
+    } catch (NumberFormatException _) {
+      System.out.println("Bad number: " + s);
+    } catch (Exception _) {
+      System.out.println("Unexpected error");
+    }
+
+    // Try with resources
+    try (var _ = ScopedContext.acquire()) {
+      System.out.println("Doing something within scoped context");
+    }
+
+    // A lambda whose parameter is irrelevant
+    System.out.println(
+      Stream.of("one", "two", "three")
+        .collect(Collectors.toMap(String::toUpperCase, _ -> "NODATA"))
+    );
+  }
+
+  static int sideEffect() {
+    System.out.println("side effect");
+    return 42;
+  }
+
+  static class Order {}
+
+  static class ScopedContext implements AutoCloseable {
+    public static ScopedContext acquire() {
+      return new ScopedContext();
+    }
+
+    @Override
+    public void close() {
+      System.out.println("Closing scoped context");
+    }
+  }
+}
diff --git a/tests/features1921/java21/UnnamedPatternsPreview1Aspect.aj b/tests/features1921/java21/UnnamedPatternsPreview1Aspect.aj
new file mode 100644 (file)
index 0000000..d224a94
--- /dev/null
@@ -0,0 +1,89 @@
+import java.awt.*;
+import java.util.List;
+import java.util.PriorityQueue;
+import java.util.Queue;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+/**
+ * Examples taken from <a href="https://openjdk.org/jeps/443">JEP 443</a>
+ */
+public class UnnamedPatternsPreview1Aspect {
+  public static void main(String[] args) {}
+
+  before() : execution(* main(String[])) {
+    System.out.println(thisJoinPoint);
+
+    // An enhanced for loop with side effects
+    int acc = 0;
+    final int LIMIT = 2;
+    for (Order _ : List.of(new Order(), new Order(), new Order())) {
+      if (acc < LIMIT)
+        acc++;
+    }
+    System.out.println(acc);
+
+    // The initialisation of a basic for loop can declare unnamed local variables
+    for (int i = 0, _ = sideEffect(); i < 2; i++) {
+      System.out.println(i);
+    }
+
+    // An assignment statement, where the result of the expression on the right hand side is not needed
+    Queue<Integer> q = new PriorityQueue<>(List.of(1, 2, 3, 4, 5, 6));
+    while (q.size() >= 3) {
+      var x = q.remove();
+      var y = q.remove();
+      var _ = q.remove();
+      System.out.println(new Point(x, y));
+    }
+
+    // The same unnamed variable name '_' can be used in multiple assignment statements
+    q = new PriorityQueue<>(List.of(1, 2, 3, 4, 5, 6));
+    while (q.size() >= 3) {
+      var x = q.remove();
+      var _ = q.remove();
+      var _ = q.remove();
+      System.out.println(new Point(x, 0));
+    }
+
+    // Unnamed variables can be used in one or multiple catch blocks
+    String s = "123xy";
+    try {
+      int i = Integer.parseInt(s);
+      System.out.println(i);
+    } catch (NumberFormatException _) {
+      System.out.println("Bad number: " + s);
+    } catch (Exception _) {
+      System.out.println("Unexpected error");
+    }
+
+    // Try with resources
+    try (var _ = ScopedContext.acquire()) {
+      System.out.println("Doing something within scoped context");
+    }
+
+    // A lambda whose parameter is irrelevant
+    System.out.println(
+      Stream.of("one", "two", "three")
+        .collect(Collectors.toMap(String::toUpperCase, _ -> "NODATA"))
+    );
+  }
+
+  static int sideEffect() {
+    System.out.println("side effect");
+    return 42;
+  }
+
+  static class Order {}
+
+  static class ScopedContext implements AutoCloseable {
+    public static ScopedContext acquire() {
+      return new ScopedContext();
+    }
+
+    @Override
+    public void close() {
+      System.out.println("Closing scoped context");
+    }
+  }
+}
index 5ef087602f16162b27742559d6d3ecad08d1363a..bdde4b67e15fc376b9fb1ca87b53228593cc82f0 100644 (file)
@@ -24,6 +24,28 @@ public class Java21PreviewFeaturesTests extends XMLBasedAjcTestCaseForJava21Only
     runTest("string patterns aspect");
   }
 
+  /**
+   * Still not implemented with the Java 21 release Eclipse 2023-12 (4.30),
+   * see <a href="https://github.com/eclipse-jdt/eclipse.jdt.core/issues/893">GitHub issue 893</a>.
+   * <p>
+   * TODO: Activate after JDT Core implementation and merge.
+   */
+  public void testUnnamedPatterns() {
+    //runTest("unnamed patterns");
+    System.out.println("Unnamed patterns still are not implemented with the Java 21 release Eclipse 2023-12 (4.30)");
+  }
+
+  /**
+   * Still not implemented with the Java 21 release Eclipse 2023-12 (4.30),
+   * see <a href="https://github.com/eclipse-jdt/eclipse.jdt.core/issues/893">GitHub issue 893</a>.
+   * <p>
+   * TODO: Activate after JDT Core implementation and merge.
+   */
+  public void testUnnamedPatternsAspect() {
+    //runTest("unnamed patterns aspect");
+    System.out.println("Unnamed patterns still are not implemented with the Java 21 release Eclipse 2023-12 (4.30)");
+  }
+
   public static Test suite() {
     return XMLBasedAjcTestCase.loadSuite(Java21PreviewFeaturesTests.class);
   }
index a7b5d0d9584b071d90799a0e51fa74e351b8544a..edb41fb31419d39d3d9485f19981ff76538c96cc 100644 (file)
                </run>
        </ajc-test>
 
+       <!-- Java 21 preview -->
+       <!-- TODO: Activate after https://github.com/eclipse-jdt/eclipse.jdt.core/issues/893 is done -->
+       <ajc-test dir="features1921/java21" vm="21" title="unnamed patterns">
+               <compile files="UnnamedPatternsPreview1.java" options="--enable-preview -21"/>
+               <run class="UnnamedPatternsPreview1" vmargs="--enable-preview">
+                       <stdout ordered="yes">
+                               <line text="0"/>
+                               <line text="1"/>
+                               <line text="java.awt.Point[x=1,y=2]"/>
+                               <line text="java.awt.Point[x=4,y=5]"/>
+                               <line text="java.awt.Point[x=1,y=0]"/>
+                               <line text="java.awt.Point[x=4,y=0]"/>
+                               <line text="Bad number: 123xy"/>
+                               <line text="Doing something within scoped context"/>
+                               <line text="Closing scoped context"/>
+                               <line text="{ONE=NODATA, TWO=NODATA, THREE=NODATA}"/>
+                       </stdout>
+               </run>
+       </ajc-test>
+
+       <!-- Java 21 preview -->
+       <!-- TODO: Activate after https://github.com/eclipse-jdt/eclipse.jdt.core/issues/893 is done -->
+       <ajc-test dir="features1921/java21" vm="21" title="unnamed patterns aspect">
+               <compile files="UnnamedPatternsPreview1Aspect.java" options="--enable-preview -21"/>
+               <run class="UnnamedPatternsPreview1Aspect" vmargs="--enable-preview">
+                       <stdout ordered="yes">
+                               <line text="execution(void UnnamedPatternsPreview1Aspect.main(String[]))"/>
+                               <line text="0"/>
+                               <line text="1"/>
+                               <line text="java.awt.Point[x=1,y=2]"/>
+                               <line text="java.awt.Point[x=4,y=5]"/>
+                               <line text="java.awt.Point[x=1,y=0]"/>
+                               <line text="java.awt.Point[x=4,y=0]"/>
+                               <line text="Bad number: 123xy"/>
+                               <line text="Doing something within scoped context"/>
+                               <line text="Closing scoped context"/>
+                               <line text="{ONE=NODATA, TWO=NODATA, THREE=NODATA}"/>
+                       </stdout>
+               </run>
+       </ajc-test>
+
 </suite>