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.

StringPatternsPreview1Aspect.aj 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import java.io.File;
  2. import java.net.InetAddress;
  3. import java.net.UnknownHostException;
  4. import java.time.LocalDate;
  5. import java.time.LocalDateTime;
  6. import java.time.LocalTime;
  7. import java.time.format.DateTimeFormatter;
  8. import static java.lang.StringTemplate.RAW;
  9. import static java.util.FormatProcessor.FMT;
  10. /**
  11. * Examples taken from <a href="https://openjdk.org/jeps/430">JEP 430</a>
  12. */
  13. public aspect StringPatternsPreview1Aspect {
  14. public static void main(String[] args) {}
  15. before() : execution(* main(String[])) {
  16. System.out.println(thisJoinPoint);
  17. // Embedded expressions can be strings
  18. String firstName = "Bill", lastName = "Duck";
  19. // TODO: Due to https://github.com/eclipse-jdt/eclipse.jdt.core/issues/1719, this is currently rendered incorrectly
  20. System.out.println(STR."\{firstName} \{lastName}");
  21. // Embedded expressions can perform arithmetic
  22. int x = 10, y = 20;
  23. System.out.println(STR."\{x} + \{y} = \{x + y}");
  24. // Embedded expressions can invoke methods and access fields
  25. System.out.println(STR."You have a \{getOfferType()} waiting for you!");
  26. Request req = new Request();
  27. System.out.println(STR."Access at \{req.date} \{req.time} from \{req.ipAddress}");
  28. // Embedded expressions can use double quotes without escaping them
  29. String filePath = "_dummy.dat";
  30. File file = new File(filePath);
  31. System.out.println(STR."The file \{filePath} \{file.exists() ? "does" : "does not"} exist");
  32. // Embedded expressions can span multiple lines
  33. System.out.println(
  34. STR."The time is \{
  35. DateTimeFormatter
  36. .ofPattern("HH:mm:ss")
  37. .format(LocalTime.of(11, 11, 11))
  38. } or roughly eleven after eleven"
  39. );
  40. // Embedded expressions can be nested
  41. String[] fruit = { "apples", "oranges", "peaches" };
  42. System.out.println(STR."\{fruit[0]}, \{STR."\{fruit[1]}, \{fruit[2]}"}\n");
  43. // Embedded expressions can be used in multi-line strings
  44. String title = "My Web Page";
  45. String text = "Hello, world";
  46. String html = STR."""
  47. <html>
  48. <head>
  49. <title>\{title}</title>
  50. </head>
  51. <body>
  52. <p>\{text}</p>
  53. </body>
  54. </html>
  55. """;
  56. System.out.println(html);
  57. // The FMT template processor interprets format specifiers which appear to the left of embedded expressions.
  58. // The format specifiers are the same as those defined in java.util.Formatter.
  59. Rectangle[] zone = new Rectangle[] {
  60. new Rectangle("Alfa", 17.8, 31.4),
  61. new Rectangle("Bravo", 9.6, 12.4),
  62. new Rectangle("Charlie", 7.1, 11.23),
  63. };
  64. String table = FMT."""
  65. Description Width Height Area
  66. %-12s\{zone[0].name} %7.2f\{zone[0].width} %7.2f\{zone[0].height} %7.2f\{zone[0].area()}
  67. %-12s\{zone[1].name} %7.2f\{zone[1].width} %7.2f\{zone[1].height} %7.2f\{zone[1].area()}
  68. %-12s\{zone[2].name} %7.2f\{zone[2].width} %7.2f\{zone[2].height} %7.2f\{zone[2].area()}
  69. \{" ".repeat(28)} Total %7.2f\{zone[0].area() + zone[1].area() + zone[2].area()}
  70. """;
  71. System.out.println(table);
  72. // Built-in security: Each template expression needs to pass through a processor.
  73. String name = "Joan";
  74. StringTemplate stringTemplate = RAW."My name is \{name}";
  75. String processedTemplate = STR.process(stringTemplate);
  76. System.out.println(processedTemplate);
  77. }
  78. static Object getOfferType() {
  79. return "special New Year's sale discount";
  80. }
  81. static class Request {
  82. LocalDate date;
  83. LocalTime time;
  84. InetAddress ipAddress;
  85. Request() {
  86. LocalDateTime dateTime = LocalDateTime.of(2011, 11, 11, 11, 11, 11);
  87. date = dateTime.toLocalDate();
  88. time = dateTime.toLocalTime();
  89. try {
  90. // localhost/127.0.0.1
  91. ipAddress = InetAddress.getByAddress("localhost", new byte[] { 127, 0, 0, 1 });
  92. } catch (UnknownHostException e) {
  93. throw new RuntimeException(e);
  94. }
  95. }
  96. }
  97. record Rectangle(String name, double width, double height) {
  98. double area() {
  99. return width * height;
  100. }
  101. }
  102. }