Skip to content

Commit 4a9a9ef

Browse files
authored
Optimization to annotation stripping in library model matching (#1204)
Small optimization over #1203; pre-compile the `Pattern` used for matching.
1 parent d40de5e commit 4a9a9ef

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

nullaway/src/main/java/com/uber/nullaway/LibraryModels.java

+12-7
Original file line numberDiff line numberDiff line change
@@ -251,18 +251,23 @@ public static MethodRef fromSymbol(Symbol.MethodSymbol symbol) {
251251
symbol.owner.getQualifiedName().toString(), symbol.name.toString(), methodStr);
252252
}
253253

254-
/**
255-
* Strip annotations from a method symbol string. The logic is specialized to work for strings
256-
* produced by {@link Symbol.MethodSymbol#toString()} and will not work for arbitrary strings.
257-
*/
258-
private static String stripAnnotationsFromMethodSymbolString(String str) {
254+
private static final Pattern ANNOTATION_STRIPPING_PATTERN = buildAnnotationStrippingPattern();
255+
256+
private static Pattern buildAnnotationStrippingPattern() {
259257
String annotationWithSpace = "@[\\w.]+\\s";
260258
// annotations within a varargs array can look like:
261259
262260
// we want to match the annotation without consuming the ellipsis, so we use a lookahead
263261
String annotationOfVarargsArray = "@[\\w.]+(?=\\.\\.\\.)";
264-
String annotationRegex = annotationWithSpace + "|" + annotationOfVarargsArray;
265-
return str.replaceAll(annotationRegex, "");
262+
return Pattern.compile(annotationWithSpace + "|" + annotationOfVarargsArray);
263+
}
264+
265+
/**
266+
* Strip annotations from a method symbol string. The logic is specialized to work for strings
267+
* produced by {@link Symbol.MethodSymbol#toString()} and will not work for arbitrary strings.
268+
*/
269+
private static String stripAnnotationsFromMethodSymbolString(String str) {
270+
return ANNOTATION_STRIPPING_PATTERN.matcher(str).replaceAll("");
266271
}
267272

268273
@Override

0 commit comments

Comments
 (0)