git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v3 2/3] userdiff: support Java record types

From
Andrei Rybak <rybak.a.v@gmail.com>
Date
Feb 7, 2023, 23:42 UTC
Message-ID
<20230207234259.452141-3-rybak.a.v@gmail.com>
In-Reply-To
<20230207234259.452141-1-rybak.a.v@gmail.com>

A new kind of class was added in Java 16 -- records.[1] The syntax of records is similar to regular classes with one important distinction: the name of the record class is followed by a mandatory list of components. The list is enclosed in parentheses, it may be empty, and it may immediately follow the name of the class or type parameters, if any, with or without separating whitespace. For example:

    public record Example(int i, String s) {
    }
    public record WithTypeParameters<A, B>(A a, B b, String s) {
    }
    record SpaceBeforeComponents (String comp1, int comp2) {
    }

Support records in the builtin userdiff pattern for Java. Add "record" to the alternatives of keywords for kinds of class.

Allowing matching various possibilities for the type parameters and/or list of the components of a record has already been covered by the preceding patch.

[1] detailed description is available in "JEP 395: Records"
    https://openjdk.org/jeps/395
Signed-off-by: Andrei Rybak <rybak.a.v@gmail.com>
---
 t/t4018/java-record                         | 6 ++++++
 t/t4018/java-record-space-before-components | 6 ++++++
 t/t4018/java-record-type-parameters         | 6 ++++++
 userdiff.c                                  | 4 ++--
 4 files changed, 20 insertions(+), 2 deletions(-)
 create mode 100644 t/t4018/java-record
 create mode 100644 t/t4018/java-record-space-before-components
 create mode 100644 t/t4018/java-record-type-parameters
diff --git a/t/t4018/java-record b/t/t4018/java-record
new file mode 100644
index 0000000000..97aa819dd8
--- /dev/null
+++ b/t/t4018/java-record
@@ -0,0 +1,6 @@
+public record RIGHT(int comp1, double comp2, String comp3) {
+    static int ONE;
+    static int TWO;
+    static int THREE;
+    static int ChangeMe;
+}
diff --git a/t/t4018/java-record-space-before-components b/t/t4018/java-record-space-before-components
new file mode 100644
index 0000000000..9827f22583
--- /dev/null
+++ b/t/t4018/java-record-space-before-components
@@ -0,0 +1,6 @@
+public record RIGHT (String components, String after, String space) {
+    static int ONE;
+    static int TWO;
+    static int THREE;
+    static int ChangeMe;
+}
diff --git a/t/t4018/java-record-type-parameters b/t/t4018/java-record-type-parameters
new file mode 100644
index 0000000000..f62a035cc8
--- /dev/null
+++ b/t/t4018/java-record-type-parameters
@@ -0,0 +1,6 @@
+public record RIGHT<A, N extends Number>(A comp1, N comp2, int comp3) {
+    static int ONE;
+    static int TWO;
+    static int THREE;
+    static int ChangeMe;
+}
diff --git a/userdiff.c b/userdiff.c
index bc5f3ed4c3..37ac98e177 100644
--- a/userdiff.c
+++ b/userdiff.c
@@ -170,8 +170,8 @@ PATTERNS("html",
 	 "[^<>= \t]+"),
 PATTERNS("java",
 	 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
-	 /* Class, enum, and interface declarations */
-	 "^[ \t]*(([a-z]+[ \t]+)*(class|enum|interface)[ \t]+.*)$\n"
+	 /* Class, enum, interface, and record declarations */
+	 "^[ \t]*(([a-z]+[ \t]+)*(class|enum|interface|record)[ \t]+.*)$\n"
 	 /* Method definitions; note that constructor signatures are not */
 	 /* matched because they are indistinguishable from method calls. */
 	 "^[ \t]*(([A-Za-z_<>&][][?&<>.,A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
-- 
2.39.1
Previous: Andrei RybakNext: Andrei Rybak
Message 16 of 19 in “userdiff: Java updates”
  1. 0/3 userdiff: Java updatesAndrei Rybak, Feb 3, 2023
  2. 1/3 userdiff: support Java type parametersAndrei Rybak, Feb 3, 2023
  3. 2/3 userdiff: support Java record typesAndrei Rybak, Feb 3, 2023
  4. 3/3 userdiff: support Java sealed classesAndrei Rybak, Feb 3, 2023
  5. Tassilo HornFeb 4, 2023
  6. 0/3 userdiff: Java updatesAndrei Rybak, Feb 4, 2023
  7. 1/3 userdiff: support Java type parametersAndrei Rybak, Feb 4, 2023
  8. 2/3 userdiff: support Java record typesAndrei Rybak, Feb 4, 2023
  9. 3/3 userdiff: support Java sealed classesAndrei Rybak, Feb 4, 2023
  10. Johannes SixtFeb 5, 2023
  11. Andrei RybakFeb 5, 2023
  12. Johannes SixtFeb 5, 2023
  13. 0/3 userdiff: Java updatesAndrei Rybak, Feb 7, 2023
  14. 1/3 userdiff: support Java type parametersAndrei Rybak, Feb 7, 2023
  15. Andrei RybakFeb 8, 2023
  16. 2/3 userdiff: support Java record typesAndrei Rybak, Feb 7, 2023
  17. 3/3 userdiff: support Java sealed classesAndrei Rybak, Feb 7, 2023
  18. Johannes SixtFeb 8, 2023
  19. Junio C HamanoFeb 8, 2023

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.