aboutsummaryrefslogtreecommitdiffstats
path: root/tests/features198/java17/SwitchPatternAspect.aj
diff options
context:
space:
mode:
Diffstat (limited to 'tests/features198/java17/SwitchPatternAspect.aj')
-rw-r--r--tests/features198/java17/SwitchPatternAspect.aj65
1 files changed, 65 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) {}
+}