Skip to content

Commit 0891649

Browse files
committed
change obtaining argument list logic
1 parent 239e1c4 commit 0891649

File tree

1 file changed

+53
-13
lines changed

1 file changed

+53
-13
lines changed

jdk-annotations/astubx-generator/src/main/java/com/uber/nullaway/jdkannotations/AstubxGenerator.java

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -208,19 +208,8 @@ private static void getMethodRecords(
208208
signatureForMethodRecords += methodName.substring(0, methodName.indexOf('(') + 1);
209209
Map<Integer, ImmutableSet<String>> argAnnotation = new LinkedHashMap<>();
210210

211-
// get the arguments
212-
String argsOnly = "";
213-
Pattern pattern = Pattern.compile(".*\\((.*)\\)");
214-
Matcher matcher = pattern.matcher(methodName);
215-
if (matcher.matches()) {
216-
argsOnly = matcher.group(1).trim();
217-
}
218-
// Split using comma but leave the commas that are inside any angle brackets(to leave
219-
// generics) or is a comma that divides annotations. After finding a comma, it checks two
220-
// conditions; 1) if the comma is followed by a '@' character, it skips the comma 2) it looks
221-
// at the rest of the string and if any angle brackets are not paired, skips the comma
222-
String[] argumentList =
223-
argsOnly.isEmpty() ? new String[0] : argsOnly.split(",(?!@)(?=(?:[^<]*<[^>]*>)*[^>]*$)");
211+
// get the argument lists
212+
String[] argumentList = getArgumentsAsArray(methodName);
224213

225214
for (int i = 0; i < argumentList.length; i++) {
226215
String typeSignature = argumentList[i].trim();
@@ -254,4 +243,55 @@ private static void getMethodRecords(
254243
MethodAnnotationsRecord.create(returnTypeNullness, ImmutableMap.copyOf(argAnnotation)));
255244
}
256245
}
246+
247+
private static String[] getArgumentsAsArray(String methodName) {
248+
// get String of only arguments
249+
String argsOnly = "";
250+
Pattern pattern = Pattern.compile(".*\\((.*)\\)");
251+
Matcher matcher = pattern.matcher(methodName);
252+
if (matcher.matches()) {
253+
argsOnly = matcher.group(1).trim();
254+
}
255+
256+
if (argsOnly.isEmpty()) {
257+
return new String[0];
258+
}
259+
260+
// make a list of arguments
261+
List<String> output = new ArrayList<>();
262+
StringBuilder cur = new StringBuilder();
263+
264+
int depth = 0; // nesting level for '<' ... '>'
265+
for (int i = 0; i < argsOnly.length(); i++) {
266+
char c = argsOnly.charAt(i);
267+
switch (c) {
268+
case '<' -> {
269+
depth++;
270+
cur.append(c);
271+
}
272+
case '>' -> {
273+
depth = Math.max(0, depth - 1);
274+
cur.append(c);
275+
}
276+
case ',' -> {
277+
if (depth == 0) {
278+
String token = cur.toString().trim();
279+
if (!token.isEmpty()) {
280+
output.add(token);
281+
}
282+
cur.setLength(0);
283+
} else {
284+
cur.append(c);
285+
}
286+
}
287+
default -> cur.append(c);
288+
}
289+
}
290+
String tail = cur.toString().trim();
291+
if (!tail.isEmpty()) {
292+
output.add(tail);
293+
}
294+
295+
return output.toArray(String[]::new);
296+
}
257297
}

0 commit comments

Comments
 (0)