You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SwitchPatternPreview3Aspect.aj 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import java.util.List;
  2. import java.util.Locale;
  3. aspect SwitchPatternPreview3Aspect {
  4. Object around(Object o) : execution(* doSomethingWithObject(*)) && args(o) {
  5. System.out.println(switch (o) {
  6. case null -> "null";
  7. case Integer i -> String.format("int %d", i);
  8. case Long l -> String.format("long %d", l);
  9. case Double d -> String.format(Locale.ENGLISH, "double %f", d);
  10. case String s -> String.format("String %s", s);
  11. default -> o.toString();
  12. });
  13. return proceed(o);
  14. }
  15. before(Shape s) : execution(* doSomethingWithShape(*)) && args(s) {
  16. System.out.println(switch (s) {
  17. case Circle c when (c.calculateArea() > 100) -> "Large circle";
  18. case Circle c -> "Small circle";
  19. default -> "Non-circle";
  20. });
  21. }
  22. after(S s) : execution(* doSomethingWithSealedClass(*)) && args(s) {
  23. System.out.println(switch (s) {
  24. case A a -> "Sealed sub-class A";
  25. case B b -> "Sealed sub-class B";
  26. case C c -> "Sealed sub-record C";
  27. });
  28. }
  29. Object around(Integer i): execution(* doSomethingWithInteger(*)) && args(i) {
  30. System.out.println(
  31. switch (i) {
  32. case null -> "value unavailable: " + i;
  33. case -1, 1 -> "absolute value 1: " + i;
  34. case Integer value when value > 0 -> "positive integer: " + i;
  35. default -> "other integer: " + i;
  36. }
  37. );
  38. return proceed(i);
  39. }
  40. }
  41. class Shape {}
  42. class Rectangle extends Shape {}
  43. class Circle extends Shape {
  44. private final double radius;
  45. public Circle(double radius) { this.radius = radius; }
  46. double calculateArea() { return Math.PI * radius * radius; }
  47. }
  48. sealed interface S permits A, B, C {}
  49. final class A implements S {}
  50. final class B implements S {}
  51. record C(int i) implements S {} // Implicitly final
  52. class Application {
  53. public static void main(String[] args) {
  54. doSomethingWithObject(null);
  55. doSomethingWithObject(123);
  56. doSomethingWithObject(999L);
  57. doSomethingWithObject(12.34);
  58. doSomethingWithObject("foo");
  59. doSomethingWithObject(List.of(123, "foo", 999L, 12.34));
  60. doSomethingWithShape(new Rectangle());
  61. doSomethingWithShape(new Circle(5));
  62. doSomethingWithShape(new Circle(6));
  63. doSomethingWithSealedClass(new A());
  64. doSomethingWithSealedClass(new B());
  65. doSomethingWithSealedClass(new C(5));
  66. doSomethingWithInteger(-1);
  67. doSomethingWithInteger(0);
  68. doSomethingWithInteger(42);
  69. doSomethingWithInteger(-99);
  70. doSomethingWithInteger(Integer.valueOf(123));
  71. doSomethingWithInteger(null);
  72. }
  73. public static Object doSomethingWithObject(Object o) { return o; }
  74. public static void doSomethingWithSealedClass(S s) {}
  75. public static void doSomethingWithShape(Shape s) {}
  76. public static Object doSomethingWithInteger(Integer o) { return o; }
  77. }