aboutsummaryrefslogtreecommitdiffstats
path: root/tests/features1920
diff options
context:
space:
mode:
authorAlexander Kriegisch <Alexander@Kriegisch.name>2023-03-28 16:29:12 +0200
committerAlexander Kriegisch <Alexander@Kriegisch.name>2023-06-24 13:10:46 +0700
commitec5e94cc188bfb54ff96b8312548176c3c02c80a (patch)
treeaf12ceda7b843e4dcef2d6585a5f326252027134 /tests/features1920
parentfbc79b2de7d6742883d41d7a8c91268bd1da3f55 (diff)
downloadaspectj-ec5e94cc188bfb54ff96b8312548176c3c02c80a.tar.gz
aspectj-ec5e94cc188bfb54ff96b8312548176c3c02c80a.zip
Add basic tests for Java 20, deactivate Java 19 preview tests
Signed-off-by: Alexander Kriegisch <Alexander@Kriegisch.name>
Diffstat (limited to 'tests/features1920')
-rw-r--r--tests/features1920/java20/SwitchPatternPreview4Error.java37
-rw-r--r--tests/features1920/java20/SwitchPatternPreview4OK.java147
2 files changed, 184 insertions, 0 deletions
diff --git a/tests/features1920/java20/SwitchPatternPreview4Error.java b/tests/features1920/java20/SwitchPatternPreview4Error.java
new file mode 100644
index 000000000..a682c15c4
--- /dev/null
+++ b/tests/features1920/java20/SwitchPatternPreview4Error.java
@@ -0,0 +1,37 @@
+/**
+ * This used to work up to preview 3 (Java 19), but no longer in preview 4 (Java 20).
+ * Now, the default case seems to need to be the last one in the list.
+ *
+ * Javac says 4x: "this case label is dominated by a preceding case label"
+ *
+ * ECJ/AJC says 11x: "This case label is dominated by one of the preceding case label" (yes, "label" singular)
+ * See https://github.com/eclipse-jdt/eclipse.jdt.core/issues/919.
+ */
+public class SwitchPatternPreview4Error {
+ static void defaultCanAppearBeforePattern(Integer i) {
+ switch (i) {
+ case null -> System.out.println("value unavailable: " + i);
+ case -1, 1 -> System.out.println("absolute value 1: " + i);
+ default -> System.out.println("other integer: " + i);
+ case Integer value when value > 0 -> System.out.println("positive integer: " + i);
+ }
+ }
+
+ static void defaultCanAppearBeforeNull(Integer i) {
+ switch (i) {
+ case -1, 1 -> System.out.println("absolute value 1: " + i);
+ default -> System.out.println("other integer: " + i);
+ case null -> System.out.println("value unavailable: " + i);
+ case Integer value when value > 0 -> System.out.println("positive integer: " + i);
+ }
+ }
+
+ static void defaultCanAppearBeforeConstantLabel(Integer i) {
+ switch (i) {
+ case null -> System.out.println("value unavailable: " + i);
+ default -> System.out.println("other integer: " + i);
+ case -1, 1 -> System.out.println("absolute value 1: " + i);
+ case Integer value when value > 0 -> System.out.println("positive integer: " + i);
+ }
+ }
+}
diff --git a/tests/features1920/java20/SwitchPatternPreview4OK.java b/tests/features1920/java20/SwitchPatternPreview4OK.java
new file mode 100644
index 000000000..f5518d496
--- /dev/null
+++ b/tests/features1920/java20/SwitchPatternPreview4OK.java
@@ -0,0 +1,147 @@
+import java.util.List;
+import java.util.Locale;
+
+/**
+ * Inspired by examples in https://openjdk.org/jeps/427
+ */
+public class SwitchPatternPreview4OK {
+ public static void main(String[] args) {
+
+ System.out.println(formatterPatternSwitch(null));
+ System.out.println(formatterPatternSwitch(123));
+ System.out.println(formatterPatternSwitch(999L));
+ System.out.println(formatterPatternSwitch(12.34));
+ System.out.println(formatterPatternSwitch("foo"));
+ System.out.println(formatterPatternSwitch(List.of(123, "foo", 999L, 12.34)));
+
+ System.out.println(testCircle(new Rectangle()));
+ System.out.println(testCircle(new Circle(5)));
+ System.out.println(testCircle(new Circle(6)));
+
+ System.out.println(testSealedCoverage(new A()));
+ System.out.println(testSealedCoverage(new B()));
+ System.out.println(testSealedCoverage(new C(5)));
+
+// constantLabelMustAppearBeforePattern(-1);
+// constantLabelMustAppearBeforePattern(0);
+// constantLabelMustAppearBeforePattern(42);
+// constantLabelMustAppearBeforePattern(-99);
+// constantLabelMustAppearBeforePattern(Integer.valueOf(123));
+// constantLabelMustAppearBeforePattern(null);
+
+ constantLabelMustAppearBeforePatternInteger(-1);
+ constantLabelMustAppearBeforePatternInteger(0);
+ constantLabelMustAppearBeforePatternInteger(42);
+ constantLabelMustAppearBeforePatternInteger(-99);
+ constantLabelMustAppearBeforePatternInteger(Integer.valueOf(123));
+ constantLabelMustAppearBeforePatternInteger(null);
+
+// System.out.println(testGenericSealedExhaustive(new E<Integer>()));
+ }
+
+ static String formatterPatternSwitch(Object o) {
+ return switch (o) {
+ case null -> "null";
+ case Integer i -> String.format("int %d", i);
+ case Long l -> String.format("long %d", l);
+ case Double d -> String.format(Locale.ENGLISH, "double %f", d);
+ case String s -> String.format("String %s", s);
+ default -> o.toString();
+ };
+ }
+
+ static class Shape {}
+ static class Rectangle extends Shape {}
+ static class Circle extends Shape {
+ private final double radius;
+ public Circle(double radius) { this.radius = radius; }
+ double calculateArea() { return Math.PI * radius * radius; }
+ }
+
+ static String testCircle(Shape s) {
+ return switch (s) {
+ case Circle c when (c.calculateArea() > 100) -> "Large circle";
+ case Circle c -> "Small circle";
+ default -> "Non-circle";
+ };
+ }
+
+ sealed interface S permits A, B, C {}
+ final static class A implements S {}
+ final static class B implements S {}
+ static record C(int i) implements S {} // Implicitly final
+
+ static String testSealedCoverage(S s) {
+ return switch (s) {
+ case A a -> "Sealed sub-class A";
+ case B b -> "Sealed sub-class B";
+ case C c -> "Sealed sub-record C";
+ };
+ }
+
+ /**
+ * According to an example from JEP 420, this should work, but it does not, neither with Javac nor ECJ.
+ *
+ * See:
+ * https://openjdk.java.net/jeps/420#1b--Dominance-of-pattern-labels
+ * https://bugs.openjdk.java.net/browse/JDK-8273326
+ * https://bugs.eclipse.org/bugs/show_bug.cgi?id=579355
+ *
+ * TODO: reactivate when implemented or move to preview 5 with Java 21, Eclipse 4.28.
+ */
+/*
+ static String constantLabelMustAppearBeforePattern(Object o) {
+ switch (o) {
+ case null -> System.out.println("value unavailable: " + o);
+ case -1, 1 -> System.out.println("special case:" + o);
+ case Integer value when value > 0 -> System.out.println("positive integer: " + o);
+ case Integer i -> System.out.println("other integer: " + o);
+ default -> System.out.println("non-integer: " + o);
+ }
+ return o == null ? "null" : o.toString();
+ }
+*/
+
+ static String constantLabelMustAppearBeforePatternInteger(Integer i) {
+ switch (i) {
+ case null -> System.out.println("value unavailable: " + i);
+ case -1, 1 -> System.out.println("absolute value 1: " + i);
+ case Integer value when value > 0 -> System.out.println("positive integer: " + i);
+ default -> System.out.println("other integer: " + i);
+ }
+ return i == null ? "null" : i.toString();
+ }
+
+ static void nullCanAppearAfterConstantLabel(Integer i) {
+ switch (i) {
+ case -1, 1 -> System.out.println("absolute value 1: " + i);
+ case null -> System.out.println("value unavailable: " + i);
+ case Integer value when value > 0 -> System.out.println("positive integer: " + i);
+ default -> System.out.println("other integer: " + i);
+ }
+ }
+
+ /**
+ * According to an example from JEP 420, this should work with preview 2 (Java 18), and it does with Javac,
+ * but not with ECJ for Java 18, 19 and 20.
+ *
+ * See:
+ * https://openjdk.java.net/jeps/420#2--Exhaustiveness-of-switch-expressions-and-statements
+ * https://bugs.eclipse.org/bugs/show_bug.cgi?id=579360
+ * https://github.com/eclipse-jdt/eclipse.jdt.core/issues/587
+ *
+ * TODO: reactivate when implemented or move to preview 5 with Java 21.
+ */
+/*
+ sealed interface I<T> permits D, E {}
+ final static class D<X> implements I<String> {}
+ final static class E<Y> implements I<Y> {}
+
+ static int testGenericSealedExhaustive(I<Integer> i) {
+ return switch (i) {
+ // Exhaustive as no D case possible!
+ case E<Integer> bi -> 42;
+ };
+ }
+*/
+}