Skip to content

Commit

Permalink
[JDT] Use the general logic to read annotations for @Nullmarked to …
Browse files Browse the repository at this point in the history
…avoid trying to load annotations on annotation type declarations.

PiperOrigin-RevId: 733046670
  • Loading branch information
Googler authored and copybara-github committed Mar 3, 2025
1 parent a8accf3 commit b71e698
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ private static AnnotationBinding getAnnotationBinding(IAnnotationBinding annotat
}
}

@Nullable
public static IAnnotationBinding getAnnotationBinding(
PackageDeclaration packageDeclaration, Predicate<IAnnotationBinding> whichAnnotation) {
List<Annotation> packageAnnotations =
Expand All @@ -160,6 +161,15 @@ public static IAnnotationBinding getAnnotationBinding(
return annotationBinding.orElse(null);
}

@Nullable
public static IAnnotationBinding getAnnotationBinding(
ITypeBinding binding, Predicate<IAnnotationBinding> whichAnnotation) {
if (!shouldReadAnnotations(binding)) {
return null;
}
return Arrays.stream(binding.getAnnotations()).filter(whichAnnotation).findFirst().orElse(null);
}

public static boolean isWarningSuppressed(IBinding binding, String warning) {
IAnnotationBinding annotationBinding = getSuppressWarningsAnnotation(binding);
if (annotationBinding == null) {
Expand All @@ -174,13 +184,20 @@ public static IAnnotationBinding getSuppressWarningsAnnotation(IBinding binding)
return findAnnotationBindingByName(binding, SUPPRESS_WARNINGS_ANNOTATION_NAME);
}

public static boolean isNullMarked(PackageDeclaration packageDeclaration) {
public static boolean hasNullMarkedAnnotation(PackageDeclaration packageDeclaration) {
return getAnnotationBinding(
packageDeclaration,
(a) -> Nullability.isNullMarkedAnnotation(a.getAnnotationType().getQualifiedName()))
!= null;
}

public static boolean hasNullMarkedAnnotation(ITypeBinding typeBinding) {
return getAnnotationBinding(
typeBinding,
(a) -> Nullability.isNullMarkedAnnotation(a.getAnnotationType().getQualifiedName()))
!= null;
}

static boolean shouldReadAnnotations(IBinding binding) {
// TODO(b/399417397) Determine if we should handle annotations on all annotation types. Remove
// this method if necessary.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1248,25 +1248,20 @@ private ImmutableList<TypeDeclaration> createTypeDeclarations(ITypeBinding[] typ
.collect(ImmutableList.toImmutableList());
}

private boolean isNullMarked(ITypeBinding typeBinding) {
return hasNullMarkedAnnotation(typeBinding)
|| packageAnnotationsResolver.isNullMarked(typeBinding.getPackage().getName());
}

/**
* Returns true if {@code typeBinding} or one of its enclosing types has a @NullMarked annotation.
* Returns true if {@code typeBinding}, one of its enclosing types, or its package has
* a @NullMarked annotation
*/
private static boolean hasNullMarkedAnnotation(ITypeBinding typeBinding) {
if (hasNullMarkedAnnotation(Arrays.stream(typeBinding.getAnnotations()))) {
private boolean isNullMarked(ITypeBinding typeBinding) {
if (JdtAnnotationUtils.hasNullMarkedAnnotation(typeBinding)) {
return true;
}
return typeBinding.getDeclaringClass() != null
&& hasNullMarkedAnnotation(typeBinding.getDeclaringClass());
}

private static boolean hasNullMarkedAnnotation(Stream<IAnnotationBinding> annotations) {
return annotations.anyMatch(
a -> Nullability.isNullMarkedAnnotation(a.getAnnotationType().getQualifiedName()));
if (typeBinding.getDeclaringClass() != null) {
return isNullMarked(typeBinding.getDeclaringClass());
}

return packageAnnotationsResolver.isNullMarked(typeBinding.getPackage().getName());
}

private static boolean isAnnotatedWithFunctionalInterface(ITypeBinding typeBinding) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private void populateFromCompilationUnits(Stream<CompilationUnit> packageInfoCom
packageDeclaration.getName().getFullyQualifiedName(),
getJsNamespace(packageDeclaration),
getKtObjectiveCName(packageDeclaration),
JdtAnnotationUtils.isNullMarked(packageDeclaration));
JdtAnnotationUtils.hasNullMarkedAnnotation(packageDeclaration));
}
});
}
Expand Down

0 comments on commit b71e698

Please sign in to comment.