Skip to content

Commit 2925bdf

Browse files
cushonError Prone Team
authored andcommitted
Handle null in hasDirectAnnotationWithSimpleName
And use pattern switches and other small cleanups. #4307 PiperOrigin-RevId: 902595762
1 parent a701646 commit 2925bdf

1 file changed

Lines changed: 17 additions & 13 deletions

File tree

check_api/src/main/java/com/google/errorprone/util/ASTHelpers.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,30 +1007,34 @@ private static Set<Name> directAnnotationsAmong(
10071007
* annotation inheritance (see JLS 9.6.4.3).
10081008
*/
10091009
public static boolean hasDirectAnnotationWithSimpleName(Symbol sym, String simpleName) {
1010-
if (sym instanceof MethodSymbol methodSymbol) {
1011-
return hasDirectAnnotationWithSimpleName(methodSymbol, simpleName);
1012-
}
1013-
if (sym instanceof VarSymbol varSymbol) {
1014-
return hasDirectAnnotationWithSimpleName(varSymbol, simpleName);
1015-
}
1016-
return hasDirectAnnotation(
1017-
sym.getAnnotationMirrors().stream(),
1018-
element -> element.getSimpleName().contentEquals(simpleName));
1010+
return switch (sym) {
1011+
case MethodSymbol methodSymbol -> hasDirectAnnotationWithSimpleName(methodSymbol, simpleName);
1012+
case VarSymbol varSymbol -> hasDirectAnnotationWithSimpleName(varSymbol, simpleName);
1013+
case null -> false;
1014+
default -> hasDirectAnnotationWithSimpleName(sym.getAnnotationMirrors().stream(), simpleName);
1015+
};
10191016
}
10201017

10211018
public static boolean hasDirectAnnotationWithSimpleName(MethodSymbol sym, String simpleName) {
1022-
return hasDirectAnnotation(
1019+
return hasDirectAnnotationWithSimpleName(
10231020
Streams.concat(
10241021
sym.getAnnotationMirrors().stream(),
10251022
sym.getReturnType().getAnnotationMirrors().stream()),
1026-
element -> element.getSimpleName().contentEquals(simpleName));
1023+
simpleName);
10271024
}
10281025

10291026
public static boolean hasDirectAnnotationWithSimpleName(VarSymbol sym, String simpleName) {
1030-
return hasDirectAnnotation(
1027+
return hasDirectAnnotationWithSimpleName(
10311028
Streams.concat(
10321029
sym.getAnnotationMirrors().stream(), sym.asType().getAnnotationMirrors().stream()),
1033-
element -> element.getSimpleName().contentEquals(simpleName));
1030+
simpleName);
1031+
}
1032+
1033+
private static boolean hasDirectAnnotationWithSimpleName(
1034+
Stream<? extends AnnotationMirror> annotations, String simpleName) {
1035+
return annotations.anyMatch(
1036+
annotation ->
1037+
annotation.getAnnotationType().asElement().getSimpleName().contentEquals(simpleName));
10341038
}
10351039

10361040
/**

0 commit comments

Comments
 (0)