diff options
author | Alexander Kriegisch <Alexander@Kriegisch.name> | 2022-12-21 12:57:44 +0100 |
---|---|---|
committer | Alexander Kriegisch <Alexander@Kriegisch.name> | 2022-12-21 12:57:44 +0100 |
commit | 5239ae0480d4f623aa9c9491b6bf75e3e568cc89 (patch) | |
tree | f6bd5d5e80275c25770282eb5b8de4f22db8c6cd /tests/features1919 | |
parent | 9be61fe259dfd3340050f0c4d80ee9e9555583e1 (diff) | |
download | aspectj-5239ae0480d4f623aa9c9491b6bf75e3e568cc89.tar.gz aspectj-5239ae0480d4f623aa9c9491b6bf75e3e568cc89.zip |
Add tests for Java 19 record patterns
Signed-off-by: Alexander Kriegisch <Alexander@Kriegisch.name>
Diffstat (limited to 'tests/features1919')
8 files changed, 175 insertions, 2 deletions
diff --git a/tests/features1919/java19/RecordPatternsPreview1Aspect.aj b/tests/features1919/java19/RecordPatternsPreview1Aspect.aj new file mode 100644 index 000000000..20f6a54e5 --- /dev/null +++ b/tests/features1919/java19/RecordPatternsPreview1Aspect.aj @@ -0,0 +1,35 @@ +public aspect RecordPatternsPreview1Aspect { + public static void main(String[] args) { + doSomething(new Point(2, 7)); + doSomething(new Rectangle( + new ColoredPoint(new Point(1, 6), Color.RED), + new ColoredPoint(new Point(4, 6), Color.BLUE) + )); + } + + public static void doSomething(Object object) { + System.out.println("Doing something with " + object); + } + + before(Object object): execution(* doSomething(*)) && args(object) { + if (object instanceof Point p) { + int x = p.x(); + int y = p.y(); + System.out.println(x + y); + } + if (object instanceof Point(int x, int y)) + System.out.println(x * y); + + if (object instanceof Rectangle(ColoredPoint ul, ColoredPoint lr)) + System.out.println("Upper-left color: " + ul.c()); + if (object instanceof Rectangle(ColoredPoint(Point p, Color c), ColoredPoint lr)) + System.out.println("Upper-left color: " + c); + if (object instanceof Rectangle(ColoredPoint(Point(var x, var y), var c), var lr)) + System.out.println("Upper-left x coordinate: " + x); + } +} + +record Point(int x,int y){} +enum Color { RED, GREEN, BLUE } +record ColoredPoint(Point p, Color c) {} +record Rectangle(ColoredPoint upperLeft, ColoredPoint lowerRight) {} diff --git a/tests/features1919/java19/RecordPatternsPreview1Error.java b/tests/features1919/java19/RecordPatternsPreview1Error.java new file mode 100644 index 000000000..2a5038e89 --- /dev/null +++ b/tests/features1919/java19/RecordPatternsPreview1Error.java @@ -0,0 +1,24 @@ +/** + * This was not working as expected in ECJ after the initial Java 19 merge the JDT Core main line, + * see https://github.com/eclipse-jdt/eclipse.jdt.core/issues/450. + */ +public class RecordPatternsPreview1Error { + public static void main(String[] args) { + erroneousTest1(new Box<>("A")); + erroneousTest2(new Box<>("B")); + } + + static void erroneousTest1(Box<Object> bo) { + if (bo instanceof Box(var s)) { // Javac error: raw deconstruction patterns are not allowed + System.out.println("I'm a box"); + } + } + + static void erroneousTest2(Box b) { + if (b instanceof Box(var t)) { // Javac error: raw deconstruction patterns are not allowed + System.out.println("I'm a box"); + } + } +} + +record Box<T>(T t) {} diff --git a/tests/features1919/java19/RecordPatternsPreview1ExhaustivenessAspect.aj b/tests/features1919/java19/RecordPatternsPreview1ExhaustivenessAspect.aj new file mode 100644 index 000000000..44c7f12d0 --- /dev/null +++ b/tests/features1919/java19/RecordPatternsPreview1ExhaustivenessAspect.aj @@ -0,0 +1,37 @@ +public aspect RecordPatternsPreview1ExhaustivenessAspect { + static Pair<I> p2 = new Pair<>(new C(), new D()); + + public static void main(String[] args) { + doSomething(p2); + } + + public static void doSomething(Pair pair) { + System.out.println(pair.toString().replaceAll("@[0-9a-f]+", "@000")); + } + + before(Pair pair) : execution(* doSomething(Pair)) && args(pair) { + switch (pair) { + case Pair<I>(I i, C c) -> System.out.println("x"); + case Pair<I>(I i, D d) -> System.out.println("y"); + // TODO: Remove redundant default clause when https://github.com/eclipse-jdt/eclipse.jdt.core/issues/455 + // has been fixed. But even with the default clause, it generates wrong byte code and throws runtime error: + // NoSuchMethodError: 'I Pair.x()' + default -> System.out.println("z"); + } + + switch (pair) { + case Pair<I>(C c, I i) -> System.out.println("a"); + case Pair<I>(D d, C c) -> System.out.println("b"); + case Pair<I>(D d1, D d2) -> System.out.println("c"); + // TODO: remove redundant default clause when https://github.com/eclipse-jdt/eclipse.jdt.core/issues/455 + // has been fixed. But even with the default clause, it generates wrong byte code and throws runtime error: + // NoSuchMethodError: 'I Pair.x()' + default -> System.out.println("d"); + } + } +} + +sealed interface I permits C, D { } +final class C implements I { } +final class D implements I { } +record Pair<T>(T x, T y) { } diff --git a/tests/features1919/java19/RecordPatternsPreview1ExhaustivenessError.java b/tests/features1919/java19/RecordPatternsPreview1ExhaustivenessError.java new file mode 100644 index 000000000..8df079474 --- /dev/null +++ b/tests/features1919/java19/RecordPatternsPreview1ExhaustivenessError.java @@ -0,0 +1,22 @@ +public class RecordPatternsPreview1ExhaustivenessError { + static Pair<I> p2 = new Pair<>(new C(), new D()); + + public static void main(String[] args) { + exhaustiveSwitch(); + } + + static void exhaustiveSwitch() { + switch (p2) { + case Pair<I>(C fst, D snd) -> System.out.println("a"); + case Pair<I>(D fst, C snd) -> System.out.println("b"); + case Pair<I>(I fst, C snd) -> System.out.println("c"); + } + } +} + +class A { } +class B extends A { } +sealed interface I permits C, D { } +final class C implements I { } +final class D implements I { } +record Pair<T>(T x, T y) { } diff --git a/tests/features1919/java19/RecordPatternsPreview1ExhaustivenessOK1.java b/tests/features1919/java19/RecordPatternsPreview1ExhaustivenessOK1.java new file mode 100644 index 000000000..4153d5021 --- /dev/null +++ b/tests/features1919/java19/RecordPatternsPreview1ExhaustivenessOK1.java @@ -0,0 +1,28 @@ +public class RecordPatternsPreview1ExhaustivenessOK1 { + static Pair<A> p1 = new Pair<>(new A(), new B()); + static Pair<I> p2 = new Pair<>(new C(), new D()); + + public static void main(String[] args) { + exhaustiveSwitch(); + } + + static void exhaustiveSwitch() { + switch (p2) { + case Pair<I>(I i, C c) -> System.out.println("x"); + case Pair<I>(I i, D d) -> System.out.println("y"); + } + + switch (p2) { + case Pair<I>(C c, I i) -> System.out.println("a"); + case Pair<I>(D d, C c) -> System.out.println("b"); + case Pair<I>(D d1, D d2) -> System.out.println("c"); + } + } +} + +class A { } +class B extends A { } +sealed interface I permits C, D { } +final class C implements I { } +final class D implements I { } +record Pair<T>(T x, T y) { } diff --git a/tests/features1919/java19/RecordPatternsPreview1ExhaustivenessOK2.java b/tests/features1919/java19/RecordPatternsPreview1ExhaustivenessOK2.java new file mode 100644 index 000000000..8f80aad90 --- /dev/null +++ b/tests/features1919/java19/RecordPatternsPreview1ExhaustivenessOK2.java @@ -0,0 +1,10 @@ +public class RecordPatternsPreview1ExhaustivenessOK2 { + public static void main(String[] args) { + Person person = new Person("Bob", 12); + switch (person) { + case Person(var name, var age) -> System.out.println(name + " " + age); + } + } +} + +record Person(String name, int age) { } diff --git a/tests/features1919/java19/RecordPatternsPreview1OK.java b/tests/features1919/java19/RecordPatternsPreview1OK.java new file mode 100644 index 000000000..ba62f79d5 --- /dev/null +++ b/tests/features1919/java19/RecordPatternsPreview1OK.java @@ -0,0 +1,15 @@ +public class RecordPatternsPreview1OK { + static void printGenericBoxString1(Box<Object> objectBox) { + if (objectBox instanceof Box<Object>(String s)) { + System.out.println(s); + } + } + + static void printGenericBoxString2(Box<String> stringBox) { + if (stringBox instanceof Box<String>(var s)) { + System.out.println(s); + } + } +} + +record Box<T>(T t) {} diff --git a/tests/features1919/java19/SwitchPatternPreview3OK.java b/tests/features1919/java19/SwitchPatternPreview3OK.java index 7060ec0c2..7395f08e9 100644 --- a/tests/features1919/java19/SwitchPatternPreview3OK.java +++ b/tests/features1919/java19/SwitchPatternPreview3OK.java @@ -149,13 +149,15 @@ public class SwitchPatternPreview3OK { } /** - * According to an example from JEP 420, this should work, and it does with Javac, but not with ECJ. + * 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 and 19. * * 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 3 with Java 19, Eclipse 4.24. + * TODO: reactivate when implemented or move to preview 4 with Java 20. */ /* sealed interface I<T> permits A, B {} |