Skip to content

Optimization to annotation stripping in library model matching #1204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 28, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions nullaway/src/main/java/com/uber/nullaway/LibraryModels.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,18 +251,23 @@ public static MethodRef fromSymbol(Symbol.MethodSymbol symbol) {
symbol.owner.getQualifiedName().toString(), symbol.name.toString(), methodStr);
}

/**
* Strip annotations from a method symbol string. The logic is specialized to work for strings
* produced by {@link Symbol.MethodSymbol#toString()} and will not work for arbitrary strings.
*/
private static String stripAnnotationsFromMethodSymbolString(String str) {
private static final Pattern ANNOTATION_STRIPPING_PATTERN = buildAnnotationStrippingPattern();

private static Pattern buildAnnotationStrippingPattern() {
String annotationWithSpace = "@[\\w.]+\\s";
// annotations within a varargs array can look like:
// [email protected] [email protected]...
// we want to match the annotation without consuming the ellipsis, so we use a lookahead
String annotationOfVarargsArray = "@[\\w.]+(?=\\.\\.\\.)";
String annotationRegex = annotationWithSpace + "|" + annotationOfVarargsArray;
return str.replaceAll(annotationRegex, "");
return Pattern.compile(annotationWithSpace + "|" + annotationOfVarargsArray);
}

/**
* Strip annotations from a method symbol string. The logic is specialized to work for strings
* produced by {@link Symbol.MethodSymbol#toString()} and will not work for arbitrary strings.
*/
private static String stripAnnotationsFromMethodSymbolString(String str) {
return ANNOTATION_STRIPPING_PATTERN.matcher(str).replaceAll("");
}

@Override
Expand Down