Skip to contentSkip to conversation

userdiff: improve java regex for generic return types

PATCH Superseded Tassilo Horn 1 participant 2 messages Aug 10, 2021

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 userdiff: improve java regex for generic return types

Currently, the git diff hunk headers show the wrong method signature if the method has an array or generic return type because the regex doesn't allow [], or < and > in the return type. Also, type parameter declarations couldn't be matched.

Add several t4018 tests asserting the right hunk headers for increasingly complex method signatures:

public String[] secondMethod(String[] RIGHT)
public List<String> secondMethod(String[] RIGHT)
public <T> List<T> secondMethod(T[] RIGHT)
public <AType, B> Map<AType, B> secondMethod(String[] RIGHT) public <AType, B> Map<AType, Map<B, B[]>> secondMethod(String[] RIGHT)

---
 t/t4018/java-return-array    | 10 ++++++++++
 t/t4018/java-return-generic  | 10 ++++++++++
 t/t4018/java-return-generic2 | 10 ++++++++++
 t/t4018/java-return-generic3 | 10 ++++++++++
 t/t4018/java-return-generic4 | 10 ++++++++++
 userdiff.c                   | 10 +++++++++-
 6 files changed, 59 insertions(+), 1 deletion(-)
 create mode 100644 t/t4018/java-return-array
 create mode 100644 t/t4018/java-return-generic
 create mode 100644 t/t4018/java-return-generic2
 create mode 100644 t/t4018/java-return-generic3
 create mode 100644 t/t4018/java-return-generic4
t/t4018/java-return-array new file +10 -0
new file mode 100644
index 0000000000..fc8882a5a9
@@ -0,0 +1,10 @@
+class MyExample {
+    public void firstMethod() {
+        // Whatever...
+    }
+
+ public String[] secondMethod(String[] RIGHT) {
+        // Whatever...
+        return new; // ChangeMe
+    }
+}
t/t4018/java-return-generic new file +10 -0
new file mode 100644
index 0000000000..7ea7bf8f07
@@ -0,0 +1,10 @@
+class MyExample {
+    public void firstMethod() {
+        // Whatever...
+    }
+
+    public List<String> secondMethod(String[] RIGHT) {
+        // Whatever...
+        return Arrays.asList("ChangeMe");
+    }
+}
t/t4018/java-return-generic2 new file +10 -0
new file mode 100644
index 0000000000..d2d2ab7f37
@@ -0,0 +1,10 @@
+class MyExample {
+    public void firstMethod() {
+        // Whatever...
+    }
+
+    public <T> List<T> secondMethod(T[] RIGHT) {
+        // Whatever...
+        return (List<T>) Arrays.asList("ChangeMe");
+    }
+}
t/t4018/java-return-generic3 new file +10 -0
new file mode 100644
index 0000000000..3db1a1ddba
@@ -0,0 +1,10 @@
+class MyExample {
+    public void firstMethod() {
+        // Whatever...
+    }
+
+    public <AType, B> Map<AType, B> secondMethod(String[] RIGHT) {
+        // Whatever...
+        return new java.util.HashMap<>(); // ChangeMe
+    }
+}
t/t4018/java-return-generic4 new file +10 -0
new file mode 100644
index 0000000000..1fc18a8482
@@ -0,0 +1,10 @@
+class MyExample {
+    public void firstMethod() {
+        // Whatever...
+    }
+
+ public <AType, B> Map<AType, Map<B, B[]>> secondMethod(String[] RIGHT) {
+        // Whatever...
+        return new java.util.HashMap<>(); // ChangeMe
+    }
+}
userdiff.c +9 -1
index 3c3bbe38b0..22f17c1d3e 100644
@@ -142,7 +142,15 @@ PATTERNS("html",
 	 "[^<>= \t]+"),
 PATTERNS("java",
 	 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
-	 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
+         /* Method signatures contain: */
+         /*   modifiers: public static */
+         "^[ \t]*(([A-Za-z_][A-Za-z]*[ \t]+)*"
+         /*   optionally type parameters: <A, B, C> */
+         "(<[A-Za-z0-9_, \t]+>[ \t]+)?"
+         /*   a return type: Map<A, B[]> */
+         "([A-Za-z_]([A-Za-z_0-9<>,]|\\[[ \t]*\\])*[ \t]+)+"
+         /*   the method name followed by the parameter list: myMethod(...) */
+         "[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
 	 /* -- */
 	 "[a-zA-Z_][a-zA-Z0-9_]*"
 	 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
Signature · 1 line
2.32.0
#1 lore raw

Skip that one. I've just got aware that also qualified return types won't work, e.g.,

  private java.util.Date foo() {
    ...
  }

so patch version v3 is unterway.

Bye,
Tassilo