From e107aeadc83761211a48e5170eb0c64756a0c20f Mon Sep 17 00:00:00 2001 From: cpovirk Date: Fri, 12 Jul 2024 14:45:44 -0700 Subject: [PATCH] Annotate the rest of the main package (basically just the Java 8 subjects) for nullness. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit And move the `@NullMarked` annotation from individual classes up to the package. Motivation: - The annotating is about potentially making things nicer for callers (or any future J2KT use?). - Moving `@NullMarked` to the package is about saving Truth users from [a warning when running Error Prone with `--release 8`](https://github.com/google/truth/issues/1320): When `@NullMarked` appears on a _class_, Error Prone processes the entire `NullMarked` class in order to check whether `NullMarked` is `@Inherited`. This leads to `warning: unknown enum constant ElementType.MODULE` because `NullMarked` has `@Target(MODULE, ...)` but `MODULE` isn't available until Java 9. We'll also be fixing this on the Error Prone side, but we might as well work around it on the Truth side, too—and annotate the rest of Truth while we're at it. (In principle, I should now add `@NullUnmarked` to all of Truth's test classes, since they haven't been annotated. But doing so would have little to no effect in practice unless maybe IntelliJ recognizes `@NullUnmarked` (probably not now?) or we improve our nullness offerings in Error Prone. I'm planning to not bother, but let me know if I'm being overly lazy.) Note that this CL applies `@Nullable` to some assertion methods' parameters even though those assertions would always fail if the callers passed `null`. This follows a principle that we'd applied (albeit incompletely) in cl/516515683, which showed that such changes avoided producing build errors in existing, working code. The principle is the same as that discussed for `EqualsTester` in cl/578260904. Fixes https://github.com/google/truth/issues/1320 RELNOTES=Annotated the rest of the main package for nullness, and moved the `@NullMarked` annotation from individual classes up to the package to avoid [a warning under `--release 8`](https://github.com/google/truth/issues/1320). PiperOrigin-RevId: 651891918 --- .../common/truth/AbstractArraySubject.java | 2 -- .../common/truth/ActualValueInference.java | 2 -- .../common/truth/AssertionErrorWithFacts.java | 2 -- .../common/truth/BigDecimalSubject.java | 2 -- .../google/common/truth/BooleanSubject.java | 2 -- .../com/google/common/truth/ClassSubject.java | 2 -- .../common/truth/ComparableSubject.java | 2 -- .../truth/ComparisonFailureWithFacts.java | 2 -- .../common/truth/ComparisonFailures.java | 2 -- .../google/common/truth/Correspondence.java | 2 -- .../common/truth/CustomSubjectBuilder.java | 3 --- .../com/google/common/truth/DiffUtils.java | 2 -- .../google/common/truth/DoubleSubject.java | 2 -- .../google/common/truth/ErrorWithFacts.java | 2 -- .../java/com/google/common/truth/Expect.java | 2 -- .../google/common/truth/ExpectFailure.java | 2 -- .../java/com/google/common/truth/Fact.java | 2 -- .../google/common/truth/FailureMetadata.java | 2 -- .../google/common/truth/FailureStrategy.java | 2 -- .../com/google/common/truth/FloatSubject.java | 2 -- .../google/common/truth/GraphMatching.java | 2 -- .../common/truth/GuavaOptionalSubject.java | 2 -- .../common/truth/IgnoreJRERequirement.java | 2 -- .../google/common/truth/IntStreamSubject.java | 25 ++++++++++++++----- .../google/common/truth/IntegerSubject.java | 2 -- .../google/common/truth/IterableSubject.java | 7 +++--- .../com/google/common/truth/LazyMessage.java | 2 -- .../common/truth/LongStreamSubject.java | 25 ++++++++++++++----- .../com/google/common/truth/LongSubject.java | 2 -- .../com/google/common/truth/MapSubject.java | 2 -- .../com/google/common/truth/MathUtil.java | 2 -- .../google/common/truth/MultimapSubject.java | 2 -- .../google/common/truth/MultisetSubject.java | 2 -- .../common/truth/ObjectArraySubject.java | 2 -- .../common/truth/OptionalDoubleSubject.java | 2 +- .../common/truth/OptionalIntSubject.java | 2 +- .../common/truth/OptionalLongSubject.java | 2 +- .../java/com/google/common/truth/Ordered.java | 2 -- .../com/google/common/truth/PathSubject.java | 3 ++- .../com/google/common/truth/Platform.java | 2 -- .../truth/PrimitiveBooleanArraySubject.java | 2 -- .../truth/PrimitiveByteArraySubject.java | 2 -- .../truth/PrimitiveCharArraySubject.java | 2 -- .../truth/PrimitiveDoubleArraySubject.java | 2 -- .../truth/PrimitiveFloatArraySubject.java | 2 -- .../truth/PrimitiveIntArraySubject.java | 2 -- .../truth/PrimitiveLongArraySubject.java | 2 -- .../truth/PrimitiveShortArraySubject.java | 2 -- .../common/truth/SimpleSubjectBuilder.java | 2 -- .../common/truth/StackTraceCleaner.java | 2 -- .../common/truth/StandardSubjectBuilder.java | 2 -- .../google/common/truth/StreamSubject.java | 10 ++++---- .../google/common/truth/StringSubject.java | 2 -- .../java/com/google/common/truth/Subject.java | 2 -- .../com/google/common/truth/SubjectUtils.java | 8 +++--- .../com/google/common/truth/TableSubject.java | 2 -- .../google/common/truth/ThrowableSubject.java | 2 -- .../java/com/google/common/truth/Truth.java | 2 -- .../common/truth/TruthFailureSubject.java | 2 -- .../com/google/common/truth/TruthJUnit.java | 2 -- .../google/common/truth/UsedByReflection.java | 2 -- .../com/google/common/truth/package-info.java | 2 ++ 62 files changed, 58 insertions(+), 133 deletions(-) diff --git a/core/src/main/java/com/google/common/truth/AbstractArraySubject.java b/core/src/main/java/com/google/common/truth/AbstractArraySubject.java index 760c18042..a08863a9a 100644 --- a/core/src/main/java/com/google/common/truth/AbstractArraySubject.java +++ b/core/src/main/java/com/google/common/truth/AbstractArraySubject.java @@ -20,7 +20,6 @@ import static com.google.common.truth.Fact.simpleFact; import java.lang.reflect.Array; -import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; /** @@ -28,7 +27,6 @@ * * @author Christian Gruber (cgruber@israfil.net) */ -@NullMarked abstract class AbstractArraySubject extends Subject { private final @Nullable Object actual; diff --git a/core/src/main/java/com/google/common/truth/ActualValueInference.java b/core/src/main/java/com/google/common/truth/ActualValueInference.java index 26d813665..d90203c63 100644 --- a/core/src/main/java/com/google/common/truth/ActualValueInference.java +++ b/core/src/main/java/com/google/common/truth/ActualValueInference.java @@ -33,7 +33,6 @@ import java.io.InputStream; import java.util.ArrayList; import java.util.Map.Entry; -import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; import org.objectweb.asm.ClassReader; import org.objectweb.asm.ClassVisitor; @@ -70,7 +69,6 @@ */ @GwtIncompatible @J2ktIncompatible -@NullMarked final class ActualValueInference { /** Call {@link Platform#inferDescription} rather than calling this directly. */ static @Nullable String describeActualValue(String className, String methodName, int lineNumber) { diff --git a/core/src/main/java/com/google/common/truth/AssertionErrorWithFacts.java b/core/src/main/java/com/google/common/truth/AssertionErrorWithFacts.java index 48232c7f7..097e8ca3c 100644 --- a/core/src/main/java/com/google/common/truth/AssertionErrorWithFacts.java +++ b/core/src/main/java/com/google/common/truth/AssertionErrorWithFacts.java @@ -19,7 +19,6 @@ import static com.google.common.truth.Fact.makeMessage; import com.google.common.collect.ImmutableList; -import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; /** @@ -27,7 +26,6 @@ * messages. */ @SuppressWarnings("OverrideThrowableToString") // We intentionally hide the class name. -@NullMarked final class AssertionErrorWithFacts extends AssertionError implements ErrorWithFacts { private final ImmutableList facts; diff --git a/core/src/main/java/com/google/common/truth/BigDecimalSubject.java b/core/src/main/java/com/google/common/truth/BigDecimalSubject.java index 0bde0e79f..c98fa1cfe 100644 --- a/core/src/main/java/com/google/common/truth/BigDecimalSubject.java +++ b/core/src/main/java/com/google/common/truth/BigDecimalSubject.java @@ -20,7 +20,6 @@ import static com.google.common.truth.Fact.simpleFact; import java.math.BigDecimal; -import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; /** @@ -28,7 +27,6 @@ * * @author Kurt Alfred Kluever */ -@NullMarked public final class BigDecimalSubject extends ComparableSubject { private final @Nullable BigDecimal actual; diff --git a/core/src/main/java/com/google/common/truth/BooleanSubject.java b/core/src/main/java/com/google/common/truth/BooleanSubject.java index 406845e8d..ad0e17ba7 100644 --- a/core/src/main/java/com/google/common/truth/BooleanSubject.java +++ b/core/src/main/java/com/google/common/truth/BooleanSubject.java @@ -17,7 +17,6 @@ import static com.google.common.truth.Fact.simpleFact; -import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; /** @@ -25,7 +24,6 @@ * * @author Christian Gruber (cgruber@israfil.net) */ -@NullMarked public final class BooleanSubject extends Subject { private final @Nullable Boolean actual; diff --git a/core/src/main/java/com/google/common/truth/ClassSubject.java b/core/src/main/java/com/google/common/truth/ClassSubject.java index 2e32da77a..24f6283ce 100644 --- a/core/src/main/java/com/google/common/truth/ClassSubject.java +++ b/core/src/main/java/com/google/common/truth/ClassSubject.java @@ -18,7 +18,6 @@ import static com.google.common.base.Preconditions.checkNotNull; import com.google.common.annotations.GwtIncompatible; -import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; /** @@ -26,7 +25,6 @@ * * @author Kurt Alfred Kluever */ -@NullMarked @GwtIncompatible("reflection") @J2ktIncompatible public final class ClassSubject extends Subject { diff --git a/core/src/main/java/com/google/common/truth/ComparableSubject.java b/core/src/main/java/com/google/common/truth/ComparableSubject.java index 567333008..0d766ec1c 100644 --- a/core/src/main/java/com/google/common/truth/ComparableSubject.java +++ b/core/src/main/java/com/google/common/truth/ComparableSubject.java @@ -18,7 +18,6 @@ import static com.google.common.base.Preconditions.checkNotNull; import com.google.common.collect.Range; -import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; /** @@ -27,7 +26,6 @@ * @author Kurt Alfred Kluever * @param the type of the object being tested by this {@code ComparableSubject} */ -@NullMarked // TODO(b/136040841): Consider further tightening this to the proper `extends Comparable` public abstract class ComparableSubject> extends Subject { /** diff --git a/core/src/main/java/com/google/common/truth/ComparisonFailureWithFacts.java b/core/src/main/java/com/google/common/truth/ComparisonFailureWithFacts.java index bf49932a2..88d031c81 100644 --- a/core/src/main/java/com/google/common/truth/ComparisonFailureWithFacts.java +++ b/core/src/main/java/com/google/common/truth/ComparisonFailureWithFacts.java @@ -21,14 +21,12 @@ import com.google.common.collect.ImmutableList; import com.google.common.truth.Platform.PlatformComparisonFailure; -import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; /** * An {@link AssertionError} (usually a JUnit {@code ComparisonFailure}, but not under GWT) composed * of structured {@link Fact} instances and other string messages. */ -@NullMarked final class ComparisonFailureWithFacts extends PlatformComparisonFailure implements ErrorWithFacts { private final ImmutableList facts; diff --git a/core/src/main/java/com/google/common/truth/ComparisonFailures.java b/core/src/main/java/com/google/common/truth/ComparisonFailures.java index c1cdf5a62..af7ad300c 100644 --- a/core/src/main/java/com/google/common/truth/ComparisonFailures.java +++ b/core/src/main/java/com/google/common/truth/ComparisonFailures.java @@ -26,7 +26,6 @@ import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.ImmutableList; -import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; /** @@ -43,7 +42,6 @@ * different implementation under GWT/j2cl, where {@code ComparisonFailure} is also unavailable but * we can't just recover from that at runtime. */ -@NullMarked final class ComparisonFailures { static ImmutableList makeComparisonFailureFacts( ImmutableList headFacts, diff --git a/core/src/main/java/com/google/common/truth/Correspondence.java b/core/src/main/java/com/google/common/truth/Correspondence.java index f34066cbe..5e5453b10 100644 --- a/core/src/main/java/com/google/common/truth/Correspondence.java +++ b/core/src/main/java/com/google/common/truth/Correspondence.java @@ -31,7 +31,6 @@ import com.google.common.collect.ImmutableList; import java.util.Arrays; import java.util.List; -import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; /** @@ -66,7 +65,6 @@ * * @author Pete Gillin */ -@NullMarked public abstract class Correspondence { /** diff --git a/core/src/main/java/com/google/common/truth/CustomSubjectBuilder.java b/core/src/main/java/com/google/common/truth/CustomSubjectBuilder.java index 20a130e06..9fee6f553 100644 --- a/core/src/main/java/com/google/common/truth/CustomSubjectBuilder.java +++ b/core/src/main/java/com/google/common/truth/CustomSubjectBuilder.java @@ -18,8 +18,6 @@ import static com.google.common.base.Preconditions.checkNotNull; -import org.jspecify.annotations.NullMarked; - /** * In a fluent assertion chain, exposes one or more "custom" {@code that} methods, which accept a * value under test and return a {@link Subject}. @@ -36,7 +34,6 @@ * extensions. It explains the cases in which {@code CustomSubjectBuilder} is necessary, and it * links to further instructions. */ -@NullMarked public abstract class CustomSubjectBuilder { /** * In a fluent assertion chain, the argument to the "custom" overload of {@link diff --git a/core/src/main/java/com/google/common/truth/DiffUtils.java b/core/src/main/java/com/google/common/truth/DiffUtils.java index c90d3a19d..b8b4122e5 100644 --- a/core/src/main/java/com/google/common/truth/DiffUtils.java +++ b/core/src/main/java/com/google/common/truth/DiffUtils.java @@ -23,7 +23,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import org.jspecify.annotations.NullMarked; /** * A custom implementation of the diff algorithm based on the solution described at @@ -31,7 +30,6 @@ * * @author Yun Peng (pcloudy@google.com) */ -@NullMarked final class DiffUtils { // A list of unique strings appeared in compared texts. // The index of each string is its incremental Id. diff --git a/core/src/main/java/com/google/common/truth/DoubleSubject.java b/core/src/main/java/com/google/common/truth/DoubleSubject.java index 4da26c149..f1a87351f 100644 --- a/core/src/main/java/com/google/common/truth/DoubleSubject.java +++ b/core/src/main/java/com/google/common/truth/DoubleSubject.java @@ -26,7 +26,6 @@ import static java.lang.Double.NaN; import static java.lang.Double.doubleToLongBits; -import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; /** @@ -34,7 +33,6 @@ * * @author Kurt Alfred Kluever */ -@NullMarked public final class DoubleSubject extends ComparableSubject { private static final long NEG_ZERO_BITS = doubleToLongBits(-0.0); diff --git a/core/src/main/java/com/google/common/truth/ErrorWithFacts.java b/core/src/main/java/com/google/common/truth/ErrorWithFacts.java index 9c9e71c85..61f404b60 100644 --- a/core/src/main/java/com/google/common/truth/ErrorWithFacts.java +++ b/core/src/main/java/com/google/common/truth/ErrorWithFacts.java @@ -17,13 +17,11 @@ package com.google.common.truth; import com.google.common.collect.ImmutableList; -import org.jspecify.annotations.NullMarked; /** * Supertype of Truth's {@link AssertionError} subclasses that are created from a list of {@link * Fact} instances. */ -@NullMarked interface ErrorWithFacts { ImmutableList facts(); } diff --git a/core/src/main/java/com/google/common/truth/Expect.java b/core/src/main/java/com/google/common/truth/Expect.java index b495cebeb..8577c14ff 100644 --- a/core/src/main/java/com/google/common/truth/Expect.java +++ b/core/src/main/java/com/google/common/truth/Expect.java @@ -30,7 +30,6 @@ import com.google.errorprone.annotations.concurrent.GuardedBy; import java.util.ArrayList; import java.util.List; -import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; import org.junit.internal.AssumptionViolatedException; import org.junit.rules.ErrorCollector; @@ -84,7 +83,6 @@ */ @GwtIncompatible("JUnit4") @J2ktIncompatible -@NullMarked public final class Expect extends StandardSubjectBuilder implements TestRule { private static final class ExpectationGatherer implements FailureStrategy { diff --git a/core/src/main/java/com/google/common/truth/ExpectFailure.java b/core/src/main/java/com/google/common/truth/ExpectFailure.java index 9625886cc..98b625cc3 100644 --- a/core/src/main/java/com/google/common/truth/ExpectFailure.java +++ b/core/src/main/java/com/google/common/truth/ExpectFailure.java @@ -24,7 +24,6 @@ import com.google.common.annotations.GwtIncompatible; import com.google.common.truth.Truth.SimpleAssertionError; import com.google.errorprone.annotations.CanIgnoreReturnValue; -import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; import org.junit.runner.Description; import org.junit.runners.model.Statement; @@ -67,7 +66,6 @@ * also checks that the assertion you're testing uses the supplied {@link FailureStrategy} and calls * {@link FailureStrategy#fail} only once. */ -@NullMarked public final class ExpectFailure implements Platform.JUnitTestRule { private boolean inRuleContext = false; private boolean failureExpected = false; diff --git a/core/src/main/java/com/google/common/truth/Fact.java b/core/src/main/java/com/google/common/truth/Fact.java index 173a929db..289177901 100644 --- a/core/src/main/java/com/google/common/truth/Fact.java +++ b/core/src/main/java/com/google/common/truth/Fact.java @@ -22,7 +22,6 @@ import com.google.common.collect.ImmutableList; import java.io.Serializable; -import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; /** @@ -35,7 +34,6 @@ *

If you are writing a custom {@code Subject}, see our tips on writing failure messages. */ -@NullMarked public final class Fact implements Serializable { /** * Creates a fact with the given key and value, which will be printed in a format like "key: diff --git a/core/src/main/java/com/google/common/truth/FailureMetadata.java b/core/src/main/java/com/google/common/truth/FailureMetadata.java index df13c7db3..6a9c90fdd 100644 --- a/core/src/main/java/com/google/common/truth/FailureMetadata.java +++ b/core/src/main/java/com/google/common/truth/FailureMetadata.java @@ -29,7 +29,6 @@ import com.google.common.base.Function; import com.google.common.collect.ImmutableList; -import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; /** @@ -52,7 +51,6 @@ * using their {@link CustomSubjectBuilder#metadata()} method to get an instance to pass to the * constructor.) */ -@NullMarked public final class FailureMetadata { static FailureMetadata forFailureStrategy(FailureStrategy failureStrategy) { return new FailureMetadata( diff --git a/core/src/main/java/com/google/common/truth/FailureStrategy.java b/core/src/main/java/com/google/common/truth/FailureStrategy.java index b1cbd225f..b10d5509a 100644 --- a/core/src/main/java/com/google/common/truth/FailureStrategy.java +++ b/core/src/main/java/com/google/common/truth/FailureStrategy.java @@ -15,7 +15,6 @@ */ package com.google.common.truth; -import org.jspecify.annotations.NullMarked; /** * Defines what to do when a check fails. @@ -54,7 +53,6 @@ * StandardSubjectBuilder#forCustomFailureStrategy * StandardSubjectBuilder.forCustomFailureStrategy(STRATEGY)}. */ -@NullMarked public interface FailureStrategy { /** * Handles a failure. The parameter is an {@code AssertionError} or subclass thereof, and it diff --git a/core/src/main/java/com/google/common/truth/FloatSubject.java b/core/src/main/java/com/google/common/truth/FloatSubject.java index 47a5db2c1..c5d661185 100644 --- a/core/src/main/java/com/google/common/truth/FloatSubject.java +++ b/core/src/main/java/com/google/common/truth/FloatSubject.java @@ -26,7 +26,6 @@ import static java.lang.Float.NaN; import static java.lang.Float.floatToIntBits; -import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; /** @@ -34,7 +33,6 @@ * * @author Kurt Alfred Kluever */ -@NullMarked public final class FloatSubject extends ComparableSubject { private static final int NEG_ZERO_BITS = floatToIntBits(-0.0f); diff --git a/core/src/main/java/com/google/common/truth/GraphMatching.java b/core/src/main/java/com/google/common/truth/GraphMatching.java index 58f37f68d..ef21fef5b 100644 --- a/core/src/main/java/com/google/common/truth/GraphMatching.java +++ b/core/src/main/java/com/google/common/truth/GraphMatching.java @@ -27,7 +27,6 @@ import java.util.HashMap; import java.util.Map; import java.util.Queue; -import org.jspecify.annotations.NullMarked; /** * Helper routines related to graph @@ -35,7 +34,6 @@ * * @author Pete Gillin */ -@NullMarked final class GraphMatching { /** diff --git a/core/src/main/java/com/google/common/truth/GuavaOptionalSubject.java b/core/src/main/java/com/google/common/truth/GuavaOptionalSubject.java index fb530b2fd..cd9776908 100644 --- a/core/src/main/java/com/google/common/truth/GuavaOptionalSubject.java +++ b/core/src/main/java/com/google/common/truth/GuavaOptionalSubject.java @@ -19,7 +19,6 @@ import static com.google.common.truth.Fact.simpleFact; import com.google.common.base.Optional; -import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; /** @@ -29,7 +28,6 @@ * * @author Christian Gruber */ -@NullMarked public final class GuavaOptionalSubject extends Subject { @SuppressWarnings("NullableOptional") // Truth always accepts nulls, no matter the type private final @Nullable Optional actual; diff --git a/core/src/main/java/com/google/common/truth/IgnoreJRERequirement.java b/core/src/main/java/com/google/common/truth/IgnoreJRERequirement.java index 96f0b7b7d..a2d484272 100644 --- a/core/src/main/java/com/google/common/truth/IgnoreJRERequirement.java +++ b/core/src/main/java/com/google/common/truth/IgnoreJRERequirement.java @@ -21,11 +21,9 @@ import static java.lang.annotation.ElementType.TYPE; import java.lang.annotation.Target; -import org.jspecify.annotations.NullMarked; /** * Disables Animal Sniffer's checking of compatibility with older versions of Java/Android. */ @Target({METHOD, CONSTRUCTOR, TYPE}) -@NullMarked @interface IgnoreJRERequirement {} diff --git a/core/src/main/java/com/google/common/truth/IntStreamSubject.java b/core/src/main/java/com/google/common/truth/IntStreamSubject.java index 1c1a6f6a7..3699e089e 100644 --- a/core/src/main/java/com/google/common/truth/IntStreamSubject.java +++ b/core/src/main/java/com/google/common/truth/IntStreamSubject.java @@ -15,6 +15,7 @@ */ package com.google.common.truth; +import static com.google.common.base.Preconditions.checkNotNull; import static java.util.stream.Collectors.toCollection; import com.google.errorprone.annotations.CanIgnoreReturnValue; @@ -48,7 +49,7 @@ @IgnoreJRERequirement public final class IntStreamSubject extends Subject { - private final List actualList; + private final @Nullable List actualList; IntStreamSubject(FailureMetadata failureMetadata, @Nullable IntStream stream) { super(failureMetadata, stream); @@ -118,7 +119,7 @@ public void containsAnyOf(int first, int second, int... rest) { } /** Fails if the subject does not contain at least one of the given elements. */ - public void containsAnyIn(Iterable expected) { + public void containsAnyIn(@Nullable Iterable expected) { check().that(actualList).containsAnyIn(expected); } @@ -147,7 +148,7 @@ public Ordered containsAtLeast(int first, int second, int... rest) { * within the actual elements, but they are not required to be consecutive. */ @CanIgnoreReturnValue - public Ordered containsAtLeastElementsIn(Iterable expected) { + public Ordered containsAtLeastElementsIn(@Nullable Iterable expected) { return check().that(actualList).containsAtLeastElementsIn(expected); } @@ -161,7 +162,19 @@ public Ordered containsAtLeastElementsIn(Iterable expected) { * on the object returned by this method. */ @CanIgnoreReturnValue - public Ordered containsExactly(int... varargs) { + public Ordered containsExactly(int @Nullable ... varargs) { + /* + * We declare a parameter type that lets callers pass a nullable array, even though the + * assertion will fail if the array is ever actually null. This can be convenient if the + * expected value comes from a nullable source (e.g., a map lookup): Users would otherwise have + * to use {@code requireNonNull} or {@code !!} or similar, all to address a compile error + * warning about a runtime failure that might never happen—a runtime failure that Truth could + * produce a better exception message for, since it could make the message express that the + * caller is performing a containsExactly assertion. + * + * TODO(cpovirk): Actually produce such a better exception message. + */ + checkNotNull(varargs); return check().that(actualList).containsExactlyElementsIn(box(varargs)); } @@ -175,7 +188,7 @@ public Ordered containsExactly(int... varargs) { * on the object returned by this method. */ @CanIgnoreReturnValue - public Ordered containsExactlyElementsIn(Iterable expected) { + public Ordered containsExactlyElementsIn(@Nullable Iterable expected) { return check().that(actualList).containsExactlyElementsIn(expected); } @@ -192,7 +205,7 @@ public void containsNoneOf(int first, int second, int... rest) { * Fails if the subject contains any of the given elements. (Duplicates are irrelevant to this * test, which fails if any of the actual elements equal any of the excluded.) */ - public void containsNoneIn(Iterable excluded) { + public void containsNoneIn(@Nullable Iterable excluded) { check().that(actualList).containsNoneIn(excluded); } diff --git a/core/src/main/java/com/google/common/truth/IntegerSubject.java b/core/src/main/java/com/google/common/truth/IntegerSubject.java index 2ceca9179..99fd82fd5 100644 --- a/core/src/main/java/com/google/common/truth/IntegerSubject.java +++ b/core/src/main/java/com/google/common/truth/IntegerSubject.java @@ -20,7 +20,6 @@ import static com.google.common.truth.Fact.fact; import static com.google.common.truth.MathUtil.equalWithinTolerance; -import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; /** @@ -30,7 +29,6 @@ * @author Christian Gruber (cgruber@israfil.net) * @author Kurt Alfred Kluever */ -@NullMarked public class IntegerSubject extends ComparableSubject { private final @Nullable Integer actual; diff --git a/core/src/main/java/com/google/common/truth/IterableSubject.java b/core/src/main/java/com/google/common/truth/IterableSubject.java index 8b3bc667c..b982014bc 100644 --- a/core/src/main/java/com/google/common/truth/IterableSubject.java +++ b/core/src/main/java/com/google/common/truth/IterableSubject.java @@ -67,7 +67,6 @@ import java.util.List; import java.util.Map; import java.util.Set; -import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; /** @@ -89,7 +88,6 @@ * @author Pete Gillin */ // Can't be final since MultisetSubject and SortedSetSubject extend it -@NullMarked public class IterableSubject extends Subject { private final @Nullable Iterable actual; @@ -283,7 +281,7 @@ public final Ordered containsAtLeast( * within the actual elements, but they are not required to be consecutive. */ @CanIgnoreReturnValue - public final Ordered containsAtLeastElementsIn(Iterable expectedIterable) { + public final Ordered containsAtLeastElementsIn(@Nullable Iterable expectedIterable) { List actual = Lists.newLinkedList(checkNotNull(this.actual)); Collection expected = iterableToCollection(expectedIterable); @@ -727,8 +725,9 @@ public final void containsNoneOf( * iterable or fails. (Duplicates are irrelevant to this test, which fails if any of the actual * elements equal any of the excluded.) */ - public final void containsNoneIn(Iterable excluded) { + public final void containsNoneIn(@Nullable Iterable excluded) { Collection actual = iterableToCollection(checkNotNull(this.actual)); + checkNotNull(excluded); // TODO(cpovirk): Produce a better exception message. List<@Nullable Object> present = new ArrayList<>(); for (Object item : Sets.newLinkedHashSet(excluded)) { if (actual.contains(item)) { diff --git a/core/src/main/java/com/google/common/truth/LazyMessage.java b/core/src/main/java/com/google/common/truth/LazyMessage.java index a70e94da1..ef6d6cd4e 100644 --- a/core/src/main/java/com/google/common/truth/LazyMessage.java +++ b/core/src/main/java/com/google/common/truth/LazyMessage.java @@ -20,10 +20,8 @@ import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.ImmutableList; -import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; -@NullMarked final class LazyMessage { private final String format; private final @Nullable Object[] args; diff --git a/core/src/main/java/com/google/common/truth/LongStreamSubject.java b/core/src/main/java/com/google/common/truth/LongStreamSubject.java index 01e680ca7..2c8f3a041 100644 --- a/core/src/main/java/com/google/common/truth/LongStreamSubject.java +++ b/core/src/main/java/com/google/common/truth/LongStreamSubject.java @@ -15,6 +15,7 @@ */ package com.google.common.truth; +import static com.google.common.base.Preconditions.checkNotNull; import static java.util.stream.Collectors.toCollection; import com.google.errorprone.annotations.CanIgnoreReturnValue; @@ -48,7 +49,7 @@ @IgnoreJRERequirement public final class LongStreamSubject extends Subject { - private final List actualList; + private final @Nullable List actualList; LongStreamSubject(FailureMetadata failureMetadata, @Nullable LongStream stream) { super(failureMetadata, stream); @@ -118,7 +119,7 @@ public void containsAnyOf(long first, long second, long... rest) { } /** Fails if the subject does not contain at least one of the given elements. */ - public void containsAnyIn(Iterable expected) { + public void containsAnyIn(@Nullable Iterable expected) { check().that(actualList).containsAnyIn(expected); } @@ -147,7 +148,7 @@ public Ordered containsAtLeast(long first, long second, long... rest) { * within the actual elements, but they are not required to be consecutive. */ @CanIgnoreReturnValue - public Ordered containsAtLeastElementsIn(Iterable expected) { + public Ordered containsAtLeastElementsIn(@Nullable Iterable expected) { return check().that(actualList).containsAtLeastElementsIn(expected); } @@ -161,7 +162,19 @@ public Ordered containsAtLeastElementsIn(Iterable expected) { * on the object returned by this method. */ @CanIgnoreReturnValue - public Ordered containsExactly(long... varargs) { + public Ordered containsExactly(long @Nullable ... varargs) { + /* + * We declare a parameter type that lets callers pass a nullable array, even though the + * assertion will fail if the array is ever actually null. This can be convenient if the + * expected value comes from a nullable source (e.g., a map lookup): Users would otherwise have + * to use {@code requireNonNull} or {@code !!} or similar, all to address a compile error + * warning about a runtime failure that might never happen—a runtime failure that Truth could + * produce a better exception message for, since it could make the message express that the + * caller is performing a containsExactly assertion. + * + * TODO(cpovirk): Actually produce such a better exception message. + */ + checkNotNull(varargs); return check().that(actualList).containsExactlyElementsIn(box(varargs)); } @@ -175,7 +188,7 @@ public Ordered containsExactly(long... varargs) { * on the object returned by this method. */ @CanIgnoreReturnValue - public Ordered containsExactlyElementsIn(Iterable expected) { + public Ordered containsExactlyElementsIn(@Nullable Iterable expected) { return check().that(actualList).containsExactlyElementsIn(expected); } @@ -192,7 +205,7 @@ public void containsNoneOf(long first, long second, long... rest) { * Fails if the subject contains any of the given elements. (Duplicates are irrelevant to this * test, which fails if any of the actual elements equal any of the excluded.) */ - public void containsNoneIn(Iterable excluded) { + public void containsNoneIn(@Nullable Iterable excluded) { check().that(actualList).containsNoneIn(excluded); } diff --git a/core/src/main/java/com/google/common/truth/LongSubject.java b/core/src/main/java/com/google/common/truth/LongSubject.java index 4b0ae7bb8..746d3cc63 100644 --- a/core/src/main/java/com/google/common/truth/LongSubject.java +++ b/core/src/main/java/com/google/common/truth/LongSubject.java @@ -20,7 +20,6 @@ import static com.google.common.truth.Fact.fact; import static com.google.common.truth.MathUtil.equalWithinTolerance; -import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; /** @@ -30,7 +29,6 @@ * @author Christian Gruber (cgruber@israfil.net) * @author Kurt Alfred Kluever */ -@NullMarked public class LongSubject extends ComparableSubject { private final @Nullable Long actual; diff --git a/core/src/main/java/com/google/common/truth/MapSubject.java b/core/src/main/java/com/google/common/truth/MapSubject.java index eb6f8d275..2b0e496aa 100644 --- a/core/src/main/java/com/google/common/truth/MapSubject.java +++ b/core/src/main/java/com/google/common/truth/MapSubject.java @@ -42,7 +42,6 @@ import java.util.List; import java.util.Map; import java.util.Set; -import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; /** @@ -51,7 +50,6 @@ * @author Christian Gruber * @author Kurt Alfred Kluever */ -@NullMarked public class MapSubject extends Subject { private final @Nullable Map actual; diff --git a/core/src/main/java/com/google/common/truth/MathUtil.java b/core/src/main/java/com/google/common/truth/MathUtil.java index 986cd1e5c..2fb44da5c 100644 --- a/core/src/main/java/com/google/common/truth/MathUtil.java +++ b/core/src/main/java/com/google/common/truth/MathUtil.java @@ -19,10 +19,8 @@ import static java.lang.Math.subtractExact; import com.google.common.primitives.Doubles; -import org.jspecify.annotations.NullMarked; /** Math utilities to be shared by numeric subjects. */ -@NullMarked final class MathUtil { private MathUtil() {} diff --git a/core/src/main/java/com/google/common/truth/MultimapSubject.java b/core/src/main/java/com/google/common/truth/MultimapSubject.java index 4f767350e..96cef441b 100644 --- a/core/src/main/java/com/google/common/truth/MultimapSubject.java +++ b/core/src/main/java/com/google/common/truth/MultimapSubject.java @@ -46,7 +46,6 @@ import java.util.List; import java.util.Map; import java.util.Set; -import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; /** @@ -55,7 +54,6 @@ * @author Daniel Ploch * @author Kurt Alfred Kluever */ -@NullMarked public class MultimapSubject extends Subject { /** Ordered implementation that does nothing because an earlier check already caused a failure. */ diff --git a/core/src/main/java/com/google/common/truth/MultisetSubject.java b/core/src/main/java/com/google/common/truth/MultisetSubject.java index 0ab2450c0..264b30c89 100644 --- a/core/src/main/java/com/google/common/truth/MultisetSubject.java +++ b/core/src/main/java/com/google/common/truth/MultisetSubject.java @@ -19,7 +19,6 @@ import static com.google.common.base.Preconditions.checkNotNull; import com.google.common.collect.Multiset; -import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; /** @@ -27,7 +26,6 @@ * * @author Kurt Alfred Kluever */ -@NullMarked public final class MultisetSubject extends IterableSubject { private final @Nullable Multiset actual; diff --git a/core/src/main/java/com/google/common/truth/ObjectArraySubject.java b/core/src/main/java/com/google/common/truth/ObjectArraySubject.java index b50caac56..712fc30a0 100644 --- a/core/src/main/java/com/google/common/truth/ObjectArraySubject.java +++ b/core/src/main/java/com/google/common/truth/ObjectArraySubject.java @@ -18,7 +18,6 @@ import static com.google.common.base.Preconditions.checkNotNull; import java.util.Arrays; -import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; /** @@ -26,7 +25,6 @@ * * @author Christian Gruber */ -@NullMarked public final class ObjectArraySubject extends AbstractArraySubject { private final T @Nullable [] actual; diff --git a/core/src/main/java/com/google/common/truth/OptionalDoubleSubject.java b/core/src/main/java/com/google/common/truth/OptionalDoubleSubject.java index 2aefa170f..96403a4bc 100644 --- a/core/src/main/java/com/google/common/truth/OptionalDoubleSubject.java +++ b/core/src/main/java/com/google/common/truth/OptionalDoubleSubject.java @@ -31,7 +31,7 @@ @IgnoreJRERequirement public final class OptionalDoubleSubject extends Subject { - private final OptionalDouble actual; + private final @Nullable OptionalDouble actual; OptionalDoubleSubject( FailureMetadata failureMetadata, diff --git a/core/src/main/java/com/google/common/truth/OptionalIntSubject.java b/core/src/main/java/com/google/common/truth/OptionalIntSubject.java index 12d0ed932..37921f8b4 100644 --- a/core/src/main/java/com/google/common/truth/OptionalIntSubject.java +++ b/core/src/main/java/com/google/common/truth/OptionalIntSubject.java @@ -30,7 +30,7 @@ @SuppressWarnings("Java7ApiChecker") // used only from APIs with Java 8 in their signatures @IgnoreJRERequirement public final class OptionalIntSubject extends Subject { - private final OptionalInt actual; + private final @Nullable OptionalInt actual; OptionalIntSubject( FailureMetadata failureMetadata, diff --git a/core/src/main/java/com/google/common/truth/OptionalLongSubject.java b/core/src/main/java/com/google/common/truth/OptionalLongSubject.java index ddf51066a..237706b6c 100644 --- a/core/src/main/java/com/google/common/truth/OptionalLongSubject.java +++ b/core/src/main/java/com/google/common/truth/OptionalLongSubject.java @@ -30,7 +30,7 @@ @SuppressWarnings("Java7ApiChecker") // used only from APIs with Java 8 in their signatures @IgnoreJRERequirement public final class OptionalLongSubject extends Subject { - private final OptionalLong actual; + private final @Nullable OptionalLong actual; OptionalLongSubject( FailureMetadata failureMetadata, diff --git a/core/src/main/java/com/google/common/truth/Ordered.java b/core/src/main/java/com/google/common/truth/Ordered.java index 2eb1f0d18..2f7efed38 100644 --- a/core/src/main/java/com/google/common/truth/Ordered.java +++ b/core/src/main/java/com/google/common/truth/Ordered.java @@ -15,7 +15,6 @@ */ package com.google.common.truth; -import org.jspecify.annotations.NullMarked; /** * Returned by calls like {@link IterableSubject#containsExactly}, {@code Ordered} lets the caller @@ -27,7 +26,6 @@ * assertThat(supportedCharsets).containsExactly("UTF-8", "US-ASCII").inOrder(); // does check order * } */ -@NullMarked public interface Ordered { /** diff --git a/core/src/main/java/com/google/common/truth/PathSubject.java b/core/src/main/java/com/google/common/truth/PathSubject.java index d470c8656..5957779d1 100644 --- a/core/src/main/java/com/google/common/truth/PathSubject.java +++ b/core/src/main/java/com/google/common/truth/PathSubject.java @@ -18,6 +18,7 @@ import com.google.common.annotations.GwtIncompatible; import com.google.j2objc.annotations.J2ObjCIncompatible; import java.nio.file.Path; +import org.jspecify.annotations.Nullable; /** * Assertions for {@link Path} instances. @@ -28,7 +29,7 @@ @J2ObjCIncompatible @J2ktIncompatible public final class PathSubject extends Subject { - PathSubject(FailureMetadata failureMetadata, Path actual) { + PathSubject(FailureMetadata failureMetadata, @Nullable Path actual) { super(failureMetadata, actual); } diff --git a/core/src/main/java/com/google/common/truth/Platform.java b/core/src/main/java/com/google/common/truth/Platform.java index 59e47c37d..f3e9059f6 100644 --- a/core/src/main/java/com/google/common/truth/Platform.java +++ b/core/src/main/java/com/google/common/truth/Platform.java @@ -32,7 +32,6 @@ import java.lang.reflect.Method; import java.util.List; import java.util.regex.Pattern; -import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; import org.junit.ComparisonFailure; import org.junit.rules.TestRule; @@ -43,7 +42,6 @@ * * @author Christian Gruber (cgruber@google.com) */ -@NullMarked final class Platform { private Platform() {} diff --git a/core/src/main/java/com/google/common/truth/PrimitiveBooleanArraySubject.java b/core/src/main/java/com/google/common/truth/PrimitiveBooleanArraySubject.java index 30e221d9c..925ea571f 100644 --- a/core/src/main/java/com/google/common/truth/PrimitiveBooleanArraySubject.java +++ b/core/src/main/java/com/google/common/truth/PrimitiveBooleanArraySubject.java @@ -18,7 +18,6 @@ import static com.google.common.base.Preconditions.checkNotNull; import com.google.common.primitives.Booleans; -import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; /** @@ -26,7 +25,6 @@ * * @author Christian Gruber (cgruber@israfil.net) */ -@NullMarked public final class PrimitiveBooleanArraySubject extends AbstractArraySubject { private final boolean @Nullable [] actual; diff --git a/core/src/main/java/com/google/common/truth/PrimitiveByteArraySubject.java b/core/src/main/java/com/google/common/truth/PrimitiveByteArraySubject.java index 510d953d6..1f649c6e3 100644 --- a/core/src/main/java/com/google/common/truth/PrimitiveByteArraySubject.java +++ b/core/src/main/java/com/google/common/truth/PrimitiveByteArraySubject.java @@ -18,7 +18,6 @@ import static com.google.common.base.Preconditions.checkNotNull; import com.google.common.primitives.Bytes; -import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; /** @@ -26,7 +25,6 @@ * * @author Kurt Alfred Kluever */ -@NullMarked public final class PrimitiveByteArraySubject extends AbstractArraySubject { private final byte @Nullable [] actual; diff --git a/core/src/main/java/com/google/common/truth/PrimitiveCharArraySubject.java b/core/src/main/java/com/google/common/truth/PrimitiveCharArraySubject.java index bfeceea06..0ee8c0577 100644 --- a/core/src/main/java/com/google/common/truth/PrimitiveCharArraySubject.java +++ b/core/src/main/java/com/google/common/truth/PrimitiveCharArraySubject.java @@ -18,7 +18,6 @@ import static com.google.common.base.Preconditions.checkNotNull; import com.google.common.primitives.Chars; -import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; /** @@ -26,7 +25,6 @@ * * @author Christian Gruber (cgruber@israfil.net) */ -@NullMarked public final class PrimitiveCharArraySubject extends AbstractArraySubject { private final char @Nullable [] actual; diff --git a/core/src/main/java/com/google/common/truth/PrimitiveDoubleArraySubject.java b/core/src/main/java/com/google/common/truth/PrimitiveDoubleArraySubject.java index 840b6f704..ce22539f9 100644 --- a/core/src/main/java/com/google/common/truth/PrimitiveDoubleArraySubject.java +++ b/core/src/main/java/com/google/common/truth/PrimitiveDoubleArraySubject.java @@ -23,7 +23,6 @@ import com.google.common.primitives.Doubles; import com.google.errorprone.annotations.CanIgnoreReturnValue; import java.util.Arrays; -import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; /** @@ -31,7 +30,6 @@ * * @author Christian Gruber (cgruber@israfil.net) */ -@NullMarked public final class PrimitiveDoubleArraySubject extends AbstractArraySubject { private final double @Nullable [] actual; diff --git a/core/src/main/java/com/google/common/truth/PrimitiveFloatArraySubject.java b/core/src/main/java/com/google/common/truth/PrimitiveFloatArraySubject.java index c53267d0f..9ce5a2894 100644 --- a/core/src/main/java/com/google/common/truth/PrimitiveFloatArraySubject.java +++ b/core/src/main/java/com/google/common/truth/PrimitiveFloatArraySubject.java @@ -23,7 +23,6 @@ import com.google.common.primitives.Floats; import com.google.errorprone.annotations.CanIgnoreReturnValue; import java.util.Arrays; -import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; /** @@ -31,7 +30,6 @@ * * @author Christian Gruber (cgruber@israfil.net) */ -@NullMarked public final class PrimitiveFloatArraySubject extends AbstractArraySubject { private final float @Nullable [] actual; diff --git a/core/src/main/java/com/google/common/truth/PrimitiveIntArraySubject.java b/core/src/main/java/com/google/common/truth/PrimitiveIntArraySubject.java index f2458b88c..b8ee605a1 100644 --- a/core/src/main/java/com/google/common/truth/PrimitiveIntArraySubject.java +++ b/core/src/main/java/com/google/common/truth/PrimitiveIntArraySubject.java @@ -18,7 +18,6 @@ import static com.google.common.base.Preconditions.checkNotNull; import com.google.common.primitives.Ints; -import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; /** @@ -26,7 +25,6 @@ * * @author Christian Gruber (cgruber@israfil.net) */ -@NullMarked public final class PrimitiveIntArraySubject extends AbstractArraySubject { private final int @Nullable [] actual; diff --git a/core/src/main/java/com/google/common/truth/PrimitiveLongArraySubject.java b/core/src/main/java/com/google/common/truth/PrimitiveLongArraySubject.java index 4b5ed5ebe..8b34ddd4e 100644 --- a/core/src/main/java/com/google/common/truth/PrimitiveLongArraySubject.java +++ b/core/src/main/java/com/google/common/truth/PrimitiveLongArraySubject.java @@ -18,7 +18,6 @@ import static com.google.common.base.Preconditions.checkNotNull; import com.google.common.primitives.Longs; -import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; /** @@ -26,7 +25,6 @@ * * @author Christian Gruber (cgruber@israfil.net) */ -@NullMarked public final class PrimitiveLongArraySubject extends AbstractArraySubject { private final long @Nullable [] actual; diff --git a/core/src/main/java/com/google/common/truth/PrimitiveShortArraySubject.java b/core/src/main/java/com/google/common/truth/PrimitiveShortArraySubject.java index ff36a947e..147eeb102 100644 --- a/core/src/main/java/com/google/common/truth/PrimitiveShortArraySubject.java +++ b/core/src/main/java/com/google/common/truth/PrimitiveShortArraySubject.java @@ -18,7 +18,6 @@ import static com.google.common.base.Preconditions.checkNotNull; import com.google.common.primitives.Shorts; -import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; /** @@ -26,7 +25,6 @@ * * @author Christian Gruber (cgruber@israfil.net) */ -@NullMarked public final class PrimitiveShortArraySubject extends AbstractArraySubject { private final short @Nullable [] actual; diff --git a/core/src/main/java/com/google/common/truth/SimpleSubjectBuilder.java b/core/src/main/java/com/google/common/truth/SimpleSubjectBuilder.java index 7bae5be04..1bb1b2c5e 100644 --- a/core/src/main/java/com/google/common/truth/SimpleSubjectBuilder.java +++ b/core/src/main/java/com/google/common/truth/SimpleSubjectBuilder.java @@ -17,7 +17,6 @@ import static com.google.common.base.Preconditions.checkNotNull; -import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; /** @@ -32,7 +31,6 @@ *

You won't extend this type. When you write a custom subject, see our doc on extensions. */ -@NullMarked public final class SimpleSubjectBuilder { private final FailureMetadata metadata; private final Subject.Factory subjectFactory; diff --git a/core/src/main/java/com/google/common/truth/StackTraceCleaner.java b/core/src/main/java/com/google/common/truth/StackTraceCleaner.java index 290aea0d1..6284a0168 100644 --- a/core/src/main/java/com/google/common/truth/StackTraceCleaner.java +++ b/core/src/main/java/com/google/common/truth/StackTraceCleaner.java @@ -27,13 +27,11 @@ import java.util.List; import java.util.ListIterator; import java.util.Set; -import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; /** Utility that cleans stack traces to remove noise from common frameworks. */ @GwtIncompatible @J2ktIncompatible -@NullMarked final class StackTraceCleaner { static final String CLEANER_LINK = "https://goo.gl/aH3UyP"; diff --git a/core/src/main/java/com/google/common/truth/StandardSubjectBuilder.java b/core/src/main/java/com/google/common/truth/StandardSubjectBuilder.java index 7a450367d..288662afe 100644 --- a/core/src/main/java/com/google/common/truth/StandardSubjectBuilder.java +++ b/core/src/main/java/com/google/common/truth/StandardSubjectBuilder.java @@ -33,7 +33,6 @@ import java.util.stream.IntStream; import java.util.stream.LongStream; import java.util.stream.Stream; -import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; /** @@ -54,7 +53,6 @@ *

You won't extend this type. When you write a custom subject, see our doc on extensions. */ -@NullMarked public class StandardSubjectBuilder { /** * Returns a new instance that invokes the given {@code FailureStrategy} when a check fails. Most diff --git a/core/src/main/java/com/google/common/truth/StreamSubject.java b/core/src/main/java/com/google/common/truth/StreamSubject.java index 2baed2ccd..0140d6dcd 100644 --- a/core/src/main/java/com/google/common/truth/StreamSubject.java +++ b/core/src/main/java/com/google/common/truth/StreamSubject.java @@ -50,7 +50,7 @@ public final class StreamSubject extends Subject { // Storing the FailureMetadata instance is not usually advisable. private final FailureMetadata metadata; - private final Stream actual; + private final @Nullable Stream actual; private final Supplier<@Nullable List> listSupplier; StreamSubject( @@ -145,7 +145,7 @@ public void containsAnyOf( } /** Fails if the subject does not contain at least one of the given elements. */ - public void containsAnyIn(Iterable expected) { + public void containsAnyIn(@Nullable Iterable expected) { checkThatContentsList().containsAnyIn(expected); } @@ -174,7 +174,7 @@ public Ordered containsAtLeast( * within the actual elements, but they are not required to be consecutive. */ @CanIgnoreReturnValue - public Ordered containsAtLeastElementsIn(Iterable expected) { + public Ordered containsAtLeastElementsIn(@Nullable Iterable expected) { return checkThatContentsList().containsAtLeastElementsIn(expected); } @@ -209,7 +209,7 @@ public Ordered containsExactly(@Nullable Object @Nullable ... varargs) { * on the object returned by this method. */ @CanIgnoreReturnValue - public Ordered containsExactlyElementsIn(Iterable expected) { + public Ordered containsExactlyElementsIn(@Nullable Iterable expected) { return checkThatContentsList().containsExactlyElementsIn(expected); } @@ -226,7 +226,7 @@ public void containsNoneOf( * Fails if the subject contains any of the given elements. (Duplicates are irrelevant to this * test, which fails if any of the actual elements equal any of the excluded.) */ - public void containsNoneIn(Iterable excluded) { + public void containsNoneIn(@Nullable Iterable excluded) { checkThatContentsList().containsNoneIn(excluded); } diff --git a/core/src/main/java/com/google/common/truth/StringSubject.java b/core/src/main/java/com/google/common/truth/StringSubject.java index bfd55f10d..8819c9218 100644 --- a/core/src/main/java/com/google/common/truth/StringSubject.java +++ b/core/src/main/java/com/google/common/truth/StringSubject.java @@ -23,7 +23,6 @@ import com.google.common.annotations.GwtIncompatible; import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; /** @@ -32,7 +31,6 @@ * @author David Saff * @author Christian Gruber (cgruber@israfil.net) */ -@NullMarked public class StringSubject extends ComparableSubject { private final @Nullable String actual; diff --git a/core/src/main/java/com/google/common/truth/Subject.java b/core/src/main/java/com/google/common/truth/Subject.java index d97f3791b..08e14bb93 100644 --- a/core/src/main/java/com/google/common/truth/Subject.java +++ b/core/src/main/java/com/google/common/truth/Subject.java @@ -53,7 +53,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; /** @@ -73,7 +72,6 @@ * @author David Saff * @author Christian Gruber */ -@NullMarked public class Subject { /** * In a fluent assertion chain, the argument to the common overload of {@link diff --git a/core/src/main/java/com/google/common/truth/SubjectUtils.java b/core/src/main/java/com/google/common/truth/SubjectUtils.java index 20fe72e15..84cf1f6ef 100644 --- a/core/src/main/java/com/google/common/truth/SubjectUtils.java +++ b/core/src/main/java/com/google/common/truth/SubjectUtils.java @@ -15,6 +15,7 @@ */ package com.google.common.truth; +import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Strings.lenientFormat; import static com.google.common.collect.Iterables.isEmpty; import static com.google.common.collect.Iterables.transform; @@ -36,7 +37,6 @@ import java.util.Collection; import java.util.List; import java.util.Map; -import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; /** @@ -45,7 +45,6 @@ * @author Christian Gruber * @author Jens Nyman */ -@NullMarked final class SubjectUtils { private SubjectUtils() {} @@ -343,7 +342,10 @@ private static List addTypeInfoToEveryItem(Iterable items) { return itemsWithTypeInfo; } - static Collection iterableToCollection(Iterable iterable) { + static Collection iterableToCollection( + @Nullable Iterable iterable) { + // TODO(cpovirk): For null inputs, produce a better exception message (ideally in callers). + checkNotNull(iterable); if (iterable instanceof Collection) { // Should be safe to assume that any Iterable implementing Collection isn't a one-shot // iterable, right? I sure hope so. diff --git a/core/src/main/java/com/google/common/truth/TableSubject.java b/core/src/main/java/com/google/common/truth/TableSubject.java index 21e9e1041..61e2c2f0c 100644 --- a/core/src/main/java/com/google/common/truth/TableSubject.java +++ b/core/src/main/java/com/google/common/truth/TableSubject.java @@ -23,7 +23,6 @@ import com.google.common.collect.Table; import com.google.common.collect.Table.Cell; import com.google.common.collect.Tables; -import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; /** @@ -31,7 +30,6 @@ * * @author Kurt Alfred Kluever */ -@NullMarked public final class TableSubject extends Subject { private final @Nullable Table actual; diff --git a/core/src/main/java/com/google/common/truth/ThrowableSubject.java b/core/src/main/java/com/google/common/truth/ThrowableSubject.java index 8566dbd70..7a03a67a6 100644 --- a/core/src/main/java/com/google/common/truth/ThrowableSubject.java +++ b/core/src/main/java/com/google/common/truth/ThrowableSubject.java @@ -17,7 +17,6 @@ import static com.google.common.base.Preconditions.checkNotNull; -import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; /** @@ -39,7 +38,6 @@ * * @author Kurt Alfred Kluever */ -@NullMarked public class ThrowableSubject extends Subject { private final @Nullable Throwable actual; diff --git a/core/src/main/java/com/google/common/truth/Truth.java b/core/src/main/java/com/google/common/truth/Truth.java index 7ece562cf..407f6df63 100644 --- a/core/src/main/java/com/google/common/truth/Truth.java +++ b/core/src/main/java/com/google/common/truth/Truth.java @@ -32,7 +32,6 @@ import java.util.stream.IntStream; import java.util.stream.LongStream; import java.util.stream.Stream; -import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; /** @@ -79,7 +78,6 @@ * @author David Saff * @author Christian Gruber (cgruber@israfil.net) */ -@NullMarked public final class Truth { private Truth() {} diff --git a/core/src/main/java/com/google/common/truth/TruthFailureSubject.java b/core/src/main/java/com/google/common/truth/TruthFailureSubject.java index d4b6d6066..6439cf7d8 100644 --- a/core/src/main/java/com/google/common/truth/TruthFailureSubject.java +++ b/core/src/main/java/com/google/common/truth/TruthFailureSubject.java @@ -23,7 +23,6 @@ import static com.google.common.truth.Fact.simpleFact; import com.google.common.collect.ImmutableList; -import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; /** @@ -39,7 +38,6 @@ *

This class accepts any {@code AssertionError} value, but it will throw an exception if a * caller tries to access the facts of an error that wasn't produced by Truth. */ -@NullMarked public final class TruthFailureSubject extends ThrowableSubject { static final Fact HOW_TO_TEST_KEYS_WITHOUT_VALUES = simpleFact( diff --git a/core/src/main/java/com/google/common/truth/TruthJUnit.java b/core/src/main/java/com/google/common/truth/TruthJUnit.java index 94f47cb75..648d51236 100644 --- a/core/src/main/java/com/google/common/truth/TruthJUnit.java +++ b/core/src/main/java/com/google/common/truth/TruthJUnit.java @@ -15,7 +15,6 @@ */ package com.google.common.truth; -import org.jspecify.annotations.NullMarked; import org.junit.AssumptionViolatedException; /** @@ -40,7 +39,6 @@ * @author David Saff * @author Christian Gruber (cgruber@israfil.net) */ -@NullMarked @com.google.common.annotations.GwtIncompatible("JUnit4") public final class TruthJUnit { @SuppressWarnings("ConstantCaseForConstants") // Despite the "Builder" name, it's not mutable. diff --git a/core/src/main/java/com/google/common/truth/UsedByReflection.java b/core/src/main/java/com/google/common/truth/UsedByReflection.java index 50713931e..ce41dfd8d 100644 --- a/core/src/main/java/com/google/common/truth/UsedByReflection.java +++ b/core/src/main/java/com/google/common/truth/UsedByReflection.java @@ -20,8 +20,6 @@ import static java.lang.annotation.ElementType.METHOD; import java.lang.annotation.Target; -import org.jspecify.annotations.NullMarked; @Target({METHOD, FIELD, CONSTRUCTOR}) -@NullMarked @interface UsedByReflection {} diff --git a/core/src/main/java/com/google/common/truth/package-info.java b/core/src/main/java/com/google/common/truth/package-info.java index 89839f730..99ee86a05 100644 --- a/core/src/main/java/com/google/common/truth/package-info.java +++ b/core/src/main/java/com/google/common/truth/package-info.java @@ -29,6 +29,8 @@ * other docs. */ @CheckReturnValue +@NullMarked package com.google.common.truth; import com.google.errorprone.annotations.CheckReturnValue; +import org.jspecify.annotations.NullMarked;