diff options
author | Alexander Kriegisch <Alexander@Kriegisch.name> | 2021-07-28 10:55:02 +0700 |
---|---|---|
committer | Alexander Kriegisch <Alexander@Kriegisch.name> | 2021-09-07 08:44:33 +0200 |
commit | 620add3a3c18dc3e126a03254b6482ad9ab7ef36 (patch) | |
tree | 5a43d8a1db333183ac9e335772088e3c922e35de /tests/features198 | |
parent | c80551f1d36e288b4b3be658e70c0560b7e04c9e (diff) | |
download | aspectj-620add3a3c18dc3e126a03254b6482ad9ab7ef36.tar.gz aspectj-620add3a3c18dc3e126a03254b6482ad9ab7ef36.zip |
Add + activate some Java 17 tests
- Fix one fault sanity test configuration
- Deactivate Java 16 preview tests (no longer supported by Java 17
compiler)
- Test sealed classes as final on Java 17 (no longer preview)
- Add tests for JEP 406, pattern matching for switch (preview). At
present, the beta 17 branch of JDT Core does not handle the tested
features and expected compile errors correctly yet, so I had to
temporarily deactivate test execution, only printing TODO messages.
Signed-off-by: Alexander Kriegisch <Alexander@Kriegisch.name>
Diffstat (limited to 'tests/features198')
-rw-r--r-- | tests/features198/java17/SwitchPatternAspect.aj | 65 | ||||
-rw-r--r-- | tests/features198/java17/SwitchPatternError.java | 16 | ||||
-rw-r--r-- | tests/features198/java17/SwitchPatternOK.java | 63 |
3 files changed, 144 insertions, 0 deletions
diff --git a/tests/features198/java17/SwitchPatternAspect.aj b/tests/features198/java17/SwitchPatternAspect.aj new file mode 100644 index 000000000..5ca2b9611 --- /dev/null +++ b/tests/features198/java17/SwitchPatternAspect.aj @@ -0,0 +1,65 @@ +aspect SwitchPatternAspect { + Object around(Object o) : execution(* doSomethingWithObject(*)) && args(o) { + System.out.println(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("double %f", d); + case String s -> String.format("String %s", s); + default -> o.toString(); + }); + return proceed(o); + } + + before(Shape s) : execution(* doSomethingWithShape(*)) && args(s) { + System.out.println(switch (s) { + case Circle c && (c.calculateArea() > 100) -> "Large circle"; + case Circle c -> "Small circle"; + default -> "Non-circle"; + }); + } + + after(S s) : execution(* doSomethingWithSealedClass(*)) && args(s) { + System.out.println(switch (s) { + case A a -> "Sealed sub-class A"; + case B b -> "Sealed sub-class B"; + case C c -> "Sealed sub-record C"; + }); + } +} + +class Shape {} +class Rectangle extends Shape {} +class Circle extends Shape { + private final double radius; + public Circle(double radius) { this.radius = radius; } + double calculateArea() { return Math.PI * radius * radius; } +} + +sealed interface S permits A, B, C {} +final class A implements S {} +final class B implements S {} +record C(int i) implements S {} // Implicitly final + +public class Application { + public static void main(String[] args) { + doSomethingWithObject(null); + doSomethingWithObject(123); + doSomethingWithObject(999L); + doSomethingWithObject(12.34); + doSomethingWithObject("foo"); + doSomethingWithObject(List.of(123, "foo", 999L, 12.34)); + + doSomethingWithShape(new Rectangle()); + doSomethingWithShape(new Circle(5)); + doSomethingWithShape(new Circle(6)); + + doSomethingWithSealedClass(new A())); + doSomethingWithSealedClass(new B())); + doSomethingWithSealedClass(new C(5))); + } + + public Object doSomethingWithObject(Object o) { return o; } + public void doSomethingWithSealedClass(S s) {} + public void doSomethingWithShape(Shape s) {} +} diff --git a/tests/features198/java17/SwitchPatternError.java b/tests/features198/java17/SwitchPatternError.java new file mode 100644 index 000000000..745fd375c --- /dev/null +++ b/tests/features198/java17/SwitchPatternError.java @@ -0,0 +1,16 @@ +/** + * Inspired by examples in https://openjdk.java.net/jeps/406 + */ +public class SwitchPatternError { + static void error(Object o) { + switch(o) { + case CharSequence cs -> + System.out.println("A sequence of length " + cs.length()); + case String s -> // Error - pattern is dominated by previous pattern + System.out.println("A string: " + s); + default -> { + break; + } + } + } +} diff --git a/tests/features198/java17/SwitchPatternOK.java b/tests/features198/java17/SwitchPatternOK.java new file mode 100644 index 000000000..26c1cf755 --- /dev/null +++ b/tests/features198/java17/SwitchPatternOK.java @@ -0,0 +1,63 @@ +import java.util.List; + +/** + * Inspired by examples in https://openjdk.java.net/jeps/406 + */ +public class SwitchPatternOK { + 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))); + } + + 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("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 && (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"; + }; + } +} |