Skip to content
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static javax.lang.model.element.ElementKind.TYPE_PARAMETER;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.errorprone.util.MoreAnnotations;
import com.sun.source.tree.AnnotationTree;
import com.sun.source.tree.IdentifierTree;
Expand All @@ -38,6 +39,7 @@
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Name;
import javax.lang.model.element.Parameterizable;
import javax.lang.model.element.QualifiedNameable;
import javax.lang.model.element.TypeParameterElement;
import javax.lang.model.type.IntersectionType;
import javax.lang.model.type.TypeMirror;
Expand All @@ -60,6 +62,9 @@ public class NullnessAnnotations {
+ "ProtoMethodMayReturnNull|ProtoMethodAcceptsNullParameter|"
+ "ProtoPassThroughNullness")
.asMatchPredicate();
private static final ImmutableSet<String> FALSE_NULLNESS_ANNOTATIONS =
ImmutableSet.of(
"jakarta.validation.constraints.NotNull", "javax.validation.constraints.NotNull");

private NullnessAnnotations() {} // static methods only

Expand All @@ -84,12 +89,13 @@ public static boolean annotationsAreAmbiguous(
public static ImmutableList<AnnotationMirror> annotationsRelevantToNullness(
Collection<? extends AnnotationMirror> annotations) {
return annotations.stream()
.filter(a -> ANNOTATION_RELEVANT_TO_NULLNESS.test(simpleName(a).toString()))
.filter(NullnessAnnotations::isAnnotationRelevant)
.collect(toImmutableList());
}

public static ImmutableList<AnnotationTree> annotationsRelevantToNullness(
List<? extends AnnotationTree> annotations) {
// Missing support for FALSE_NULLNESS_ANNOTATIONS
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what to do about this or how much it matters. There are test cases that hit this method but none that hit the MemberSelectTree inside simpleName(AnnotationTree). I can get a qualified name from a MemberSelectTree but I don't see how that can be accomplished for an IdentifierTree.

Copy link
Contributor Author

@commonquail commonquail Dec 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That must be what Matchers::isType is for. This looks like it could work:

  private static final Matcher<AnnotationTree> NOT_FALSE_NULLNESS_ANNOT =
      Matchers.not(
          Matchers.anyOf(FALSE_NULLNESS_ANNOTATIONS.stream().map(Matchers::isType).toList()));

  public static ImmutableList<AnnotationTree> annotationsRelevantToNullness(
      List<? extends AnnotationTree> annotations, final VisitorState state) {
    return annotations.stream()
        .filter(a -> NOT_FALSE_NULLNESS_ANNOT.matches(a, state))
        .filter(a -> ANNOTATION_RELEVANT_TO_NULLNESS.test(simpleName(a)))
        .collect(toImmutableList());
  }

but requires passing VisitorState to (one) annotationsRelevantToNullness.

For your consideration: commonquail@67be368

return annotations.stream()
.filter(a -> ANNOTATION_RELEVANT_TO_NULLNESS.test(simpleName(a)))
.collect(toImmutableList());
Expand Down Expand Up @@ -216,7 +222,10 @@ public static Optional<Nullness> getUpperBound(TypeVariable typeVar) {

private static Optional<Nullness> fromAnnotationStream(
Stream<? extends AnnotationMirror> annotations) {
return fromAnnotationSimpleNames(annotations.map(a -> simpleName(a).toString()));
return fromAnnotationSimpleNames(
annotations
.filter(NullnessAnnotations::isAnnotationRelevant)
.map(a -> simpleName(a).toString()));
}

private static Optional<Nullness> fromAnnotationSimpleNames(Stream<String> annotations) {
Expand All @@ -225,4 +234,21 @@ private static Optional<Nullness> fromAnnotationSimpleNames(Stream<String> annot
.map(annot -> NULLABLE_ANNOTATION.test(annot) ? Nullness.NULLABLE : Nullness.NONNULL)
.reduce(Nullness::greatestLowerBound);
}

private static boolean isAnnotationRelevant(AnnotationMirror annotation) {
// https://github.com/google/error-prone/issues/4334
// Some @NotNull annotations, e.g. com.google.firebase.database.annotations.NotNull, resemble
// JSpecify @NonNull semantics. However, the Jakarta Bean Validation @NotNull constraint instead
// represents a runtime check to reject a value that turns out to be null, rather than a static
// promise that null will not be observed. Therefore, "@j.v.c.NotNull @o.j.a.Nullable Object o"
// is a sensible and unambiguous declaration.
if (annotation.getAnnotationType().asElement() instanceof QualifiedNameable qn) {
var fqn = qn.getQualifiedName().toString();
if (FALSE_NULLNESS_ANNOTATIONS.contains(fqn)) {
return false;
}
return ANNOTATION_RELEVANT_TO_NULLNESS.test(simpleName(annotation).toString());
}
return true;
}
}
7 changes: 7 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,13 @@
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<dependency>
<!-- Apache 2.0 -->
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>3.1.0</version>
<scope>test</scope>
</dependency>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the test framework can create additional files on the side. That could probably be used to simulate the names as an alternative to pulling in a dependency.

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,26 @@ class T {
""")
.doTest();
}

@Test
public void falseNullnessAnnotations() {
testHelper
.addSourceLines(
"T.java",
"""
import static java.lang.annotation.ElementType.*;
import java.lang.annotation.Target;
import org.jspecify.annotations.Nullable;

@Target(FIELD)
@interface NotNull {}

abstract class T {
@jakarta.validation.constraints.NotNull @Nullable Object negative;
// BUG: Diagnostic contains:
@NotNull @Nullable Object positive;
}
""")
.doTest();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1034,4 +1034,32 @@ void foo(@NonNull String s) {
""")
.doTest();
}

@Test
public void negative_falseNullnessAnnotation_inNullMarkedScope() {
compilationHelper
.addSourceLines(
"Test.java",
"""
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.Nullable;
import jakarta.validation.constraints.NotNull;

@NullMarked
class Test {
@NotNull @Nullable String negative;
@NonNull @Nullable String positive;

void foo() {
if (negative == null) {
/* This is fine */
}
// BUG: Diagnostic contains: RedundantNullCheck
if (positive == null) {}
}
}
""")
.doTest();
}
}