From 39226a8dacc866417be19b0a95b45e82d5975a84 Mon Sep 17 00:00:00 2001 From: Andrei Rybak Date: Wed, 8 Feb 2023 00:42:57 +0100 Subject: [PATCH 1/3] userdiff: support Java type parameters A class or interface in Java can have type parameters following the name in the declared type, surrounded by angle brackets (paired less than and greater than signs).[2] The type parameters -- `A` and `B` in the examples -- may follow the class name immediately: public class ParameterizedClass { } or may be separated by whitespace: public class SpaceBeforeTypeParameters { } A part of the builtin userdiff pattern for Java matches declarations of classes, enums, and interfaces. The regular expression requires at least one whitespace character after the name of the declared type. This disallows matching for opening angle bracket of type parameters immediately after the name of the type. Mandatory whitespace after the name of the type also disallows using the pattern in repositories with a fairly common code style that puts braces for the body of a class on separate lines: class WithLineBreakBeforeOpeningBrace { } Support matching Java code in more diverse code styles and declarations of classes and interfaces with type parameters immediately following the name of the type in the builtin userdiff pattern for Java. Do so by just matching anything until the end of the line after the keywords for the kind of type being declared. [1] Since Java 5 released in 2004. [2] Detailed description is available in the Java Language Specification, sections "Type Variables" and "Parameterized Types": https://docs.oracle.com/javase/specs/jls/se17/html/jls-4.html#jls-4.4 Signed-off-by: Andrei Rybak Reviewed-by: Johannes Sixt Signed-off-by: Junio C Hamano --- t/t4018/java-class-brace-on-separate-line | 6 ++++++ t/t4018/java-class-space-before-type-parameters | 6 ++++++ t/t4018/java-class-type-parameters | 6 ++++++ t/t4018/java-class-type-parameters-implements | 6 ++++++ t/t4018/java-interface-type-parameters | 6 ++++++ t/t4018/java-interface-type-parameters-extends | 6 ++++++ userdiff.c | 2 +- 7 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 t/t4018/java-class-brace-on-separate-line create mode 100644 t/t4018/java-class-space-before-type-parameters create mode 100644 t/t4018/java-class-type-parameters create mode 100644 t/t4018/java-class-type-parameters-implements create mode 100644 t/t4018/java-interface-type-parameters create mode 100644 t/t4018/java-interface-type-parameters-extends diff --git a/t/t4018/java-class-brace-on-separate-line b/t/t4018/java-class-brace-on-separate-line new file mode 100644 index 0000000000..8795acd4cf --- /dev/null +++ b/t/t4018/java-class-brace-on-separate-line @@ -0,0 +1,6 @@ +class RIGHT +{ + static int ONE; + static int TWO; + static int ChangeMe; +} diff --git a/t/t4018/java-class-space-before-type-parameters b/t/t4018/java-class-space-before-type-parameters new file mode 100644 index 0000000000..0bdef1dfbe --- /dev/null +++ b/t/t4018/java-class-space-before-type-parameters @@ -0,0 +1,6 @@ +class RIGHT { + static int ONE; + static int TWO; + static int THREE; + private A ChangeMe; +} diff --git a/t/t4018/java-class-type-parameters b/t/t4018/java-class-type-parameters new file mode 100644 index 0000000000..579aa7af21 --- /dev/null +++ b/t/t4018/java-class-type-parameters @@ -0,0 +1,6 @@ +class RIGHT { + static int ONE; + static int TWO; + static int THREE; + private A ChangeMe; +} diff --git a/t/t4018/java-class-type-parameters-implements b/t/t4018/java-class-type-parameters-implements new file mode 100644 index 0000000000..b8038b1866 --- /dev/null +++ b/t/t4018/java-class-type-parameters-implements @@ -0,0 +1,6 @@ +class RIGHT implements List { + static int ONE; + static int TWO; + static int THREE; + private A ChangeMe; +} diff --git a/t/t4018/java-interface-type-parameters b/t/t4018/java-interface-type-parameters new file mode 100644 index 0000000000..a4baa1ae68 --- /dev/null +++ b/t/t4018/java-interface-type-parameters @@ -0,0 +1,6 @@ +interface RIGHT { + static int ONE; + static int TWO; + static int THREE; + public B foo(A ChangeMe); +} diff --git a/t/t4018/java-interface-type-parameters-extends b/t/t4018/java-interface-type-parameters-extends new file mode 100644 index 0000000000..31d7fb3244 --- /dev/null +++ b/t/t4018/java-interface-type-parameters-extends @@ -0,0 +1,6 @@ +interface RIGHT extends Function { + static int ONE; + static int TWO; + static int THREE; + public B foo(A ChangeMe); +} diff --git a/userdiff.c b/userdiff.c index d71b82feb7..bc5f3ed4c3 100644 --- a/userdiff.c +++ b/userdiff.c @@ -171,7 +171,7 @@ PATTERNS("html", 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]+[A-Za-z][A-Za-z0-9_$]*[ \t]+.*)$\n" + "^[ \t]*(([a-z]+[ \t]+)*(class|enum|interface)[ \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]*\\([^;]*)$", From 575e6fcfcc961a64a222e0241cdc117d24f9ec87 Mon Sep 17 00:00:00 2001 From: Andrei Rybak Date: Wed, 8 Feb 2023 00:42:58 +0100 Subject: [PATCH 2/3] userdiff: support Java record types 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 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 Reviewed-by: Johannes Sixt Signed-off-by: Junio C Hamano --- 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 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]*\\([^;]*)$", From 93d52ed050f5613897b73e75961df5c589d63a4b Mon Sep 17 00:00:00 2001 From: Andrei Rybak Date: Wed, 8 Feb 2023 00:42:59 +0100 Subject: [PATCH 3/3] userdiff: support Java sealed classes A new kind of class was added in Java 17 -- sealed classes.[1] This feature includes several new keywords that may appear in a declaration of a class. New modifiers before name of the class: "sealed" and "non-sealed", and a clause after name of the class marked by keyword "permits". The current set of regular expressions in userdiff.c already allows the modifier "sealed" and the "permits" clause, but not the modifier "non-sealed", which is the first hyphenated keyword in Java.[2] Allow hyphen in the words that precede the name of type to match the "non-sealed" modifier. In new input file "java-sealed" for the test t4018-diff-funcname.sh, use a Java code comment for the marker "RIGHT". This workaround is needed, because the name of the sealed class appears on the line of code that has the "ChangeMe" marker. [1] Detailed description in "JEP 409: Sealed Classes" https://openjdk.org/jeps/409 [2] "JEP draft: Keyword Management for the Java Language" https://openjdk.org/jeps/8223002 Signed-off-by: Andrei Rybak Reviewed-by: Johannes Sixt Signed-off-by: Junio C Hamano --- t/t4018/java-non-sealed | 8 ++++++++ t/t4018/java-sealed | 7 +++++++ t/t4018/java-sealed-permits | 6 ++++++ t/t4018/java-sealed-type-parameters | 6 ++++++ t/t4018/java-sealed-type-parameters-implements-permits | 6 ++++++ t/t4018/java-sealed-type-parameters-permits | 6 ++++++ userdiff.c | 2 +- 7 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 t/t4018/java-non-sealed create mode 100644 t/t4018/java-sealed create mode 100644 t/t4018/java-sealed-permits create mode 100644 t/t4018/java-sealed-type-parameters create mode 100644 t/t4018/java-sealed-type-parameters-implements-permits create mode 100644 t/t4018/java-sealed-type-parameters-permits diff --git a/t/t4018/java-non-sealed b/t/t4018/java-non-sealed new file mode 100644 index 0000000000..069087c1c6 --- /dev/null +++ b/t/t4018/java-non-sealed @@ -0,0 +1,8 @@ +public abstract sealed class SealedClass { + public static non-sealed class RIGHT extends SealedClass { + static int ONE; + static int TWO; + static int THREE; + private int ChangeMe; + } +} diff --git a/t/t4018/java-sealed b/t/t4018/java-sealed new file mode 100644 index 0000000000..785fbc62bc --- /dev/null +++ b/t/t4018/java-sealed @@ -0,0 +1,7 @@ +public abstract sealed class Sealed { // RIGHT + static int ONE; + static int TWO; + static int THREE; + public final class ChangeMe extends Sealed { + } +} diff --git a/t/t4018/java-sealed-permits b/t/t4018/java-sealed-permits new file mode 100644 index 0000000000..18dd4894cf --- /dev/null +++ b/t/t4018/java-sealed-permits @@ -0,0 +1,6 @@ +public abstract sealed class RIGHT permits PermittedA, PermittedB { + static int ONE; + static int TWO; + static int THREE; + private int ChangeMe; +} diff --git a/t/t4018/java-sealed-type-parameters b/t/t4018/java-sealed-type-parameters new file mode 100644 index 0000000000..e6530c47c3 --- /dev/null +++ b/t/t4018/java-sealed-type-parameters @@ -0,0 +1,6 @@ +public abstract sealed class RIGHT { + static int ONE; + static int TWO; + static int THREE; + private int ChangeMe; +} diff --git a/t/t4018/java-sealed-type-parameters-implements-permits b/t/t4018/java-sealed-type-parameters-implements-permits new file mode 100644 index 0000000000..bd6e6d3582 --- /dev/null +++ b/t/t4018/java-sealed-type-parameters-implements-permits @@ -0,0 +1,6 @@ +public abstract sealed class RIGHT implements List permits PermittedA, PermittedB { + static int ONE; + static int TWO; + static int THREE; + private int ChangeMe; +} diff --git a/t/t4018/java-sealed-type-parameters-permits b/t/t4018/java-sealed-type-parameters-permits new file mode 100644 index 0000000000..25a0da6442 --- /dev/null +++ b/t/t4018/java-sealed-type-parameters-permits @@ -0,0 +1,6 @@ +public abstract sealed class RIGHT permits PermittedA, PermittedB { + static int ONE; + static int TWO; + static int THREE; + private int ChangeMe; +} diff --git a/userdiff.c b/userdiff.c index 37ac98e177..94cca1a2a8 100644 --- a/userdiff.c +++ b/userdiff.c @@ -171,7 +171,7 @@ PATTERNS("html", PATTERNS("java", "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n" /* Class, enum, interface, and record declarations */ - "^[ \t]*(([a-z]+[ \t]+)*(class|enum|interface|record)[ \t]+.*)$\n" + "^[ \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]*\\([^;]*)$",