Skip to content

Commit e107aea

Browse files
cpovirkGoogle Java Core Libraries
authored andcommitted
Annotate the rest of the main package (basically just the Java 8 subjects) for nullness.
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`](#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 #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`](#1320). PiperOrigin-RevId: 651891918
1 parent 8ac91a6 commit e107aea

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+58
-133
lines changed

core/src/main/java/com/google/common/truth/AbstractArraySubject.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,13 @@
2020
import static com.google.common.truth.Fact.simpleFact;
2121

2222
import java.lang.reflect.Array;
23-
import org.jspecify.annotations.NullMarked;
2423
import org.jspecify.annotations.Nullable;
2524

2625
/**
2726
* A common supertype for Array subjects, abstracting some common display and error infrastructure.
2827
*
2928
* @author Christian Gruber ([email protected])
3029
*/
31-
@NullMarked
3230
abstract class AbstractArraySubject extends Subject {
3331
private final @Nullable Object actual;
3432

core/src/main/java/com/google/common/truth/ActualValueInference.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import java.io.InputStream;
3434
import java.util.ArrayList;
3535
import java.util.Map.Entry;
36-
import org.jspecify.annotations.NullMarked;
3736
import org.jspecify.annotations.Nullable;
3837
import org.objectweb.asm.ClassReader;
3938
import org.objectweb.asm.ClassVisitor;
@@ -70,7 +69,6 @@
7069
*/
7170
@GwtIncompatible
7271
@J2ktIncompatible
73-
@NullMarked
7472
final class ActualValueInference {
7573
/** <b>Call {@link Platform#inferDescription} rather than calling this directly.</b> */
7674
static @Nullable String describeActualValue(String className, String methodName, int lineNumber) {

core/src/main/java/com/google/common/truth/AssertionErrorWithFacts.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@
1919
import static com.google.common.truth.Fact.makeMessage;
2020

2121
import com.google.common.collect.ImmutableList;
22-
import org.jspecify.annotations.NullMarked;
2322
import org.jspecify.annotations.Nullable;
2423

2524
/**
2625
* An {@link AssertionError} composed of structured {@link Fact} instances and other string
2726
* messages.
2827
*/
2928
@SuppressWarnings("OverrideThrowableToString") // We intentionally hide the class name.
30-
@NullMarked
3129
final class AssertionErrorWithFacts extends AssertionError implements ErrorWithFacts {
3230
private final ImmutableList<Fact> facts;
3331

core/src/main/java/com/google/common/truth/BigDecimalSubject.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,13 @@
2020
import static com.google.common.truth.Fact.simpleFact;
2121

2222
import java.math.BigDecimal;
23-
import org.jspecify.annotations.NullMarked;
2423
import org.jspecify.annotations.Nullable;
2524

2625
/**
2726
* Propositions for {@link BigDecimal} typed subjects.
2827
*
2928
* @author Kurt Alfred Kluever
3029
*/
31-
@NullMarked
3230
public final class BigDecimalSubject extends ComparableSubject<BigDecimal> {
3331
private final @Nullable BigDecimal actual;
3432

core/src/main/java/com/google/common/truth/BooleanSubject.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,13 @@
1717

1818
import static com.google.common.truth.Fact.simpleFact;
1919

20-
import org.jspecify.annotations.NullMarked;
2120
import org.jspecify.annotations.Nullable;
2221

2322
/**
2423
* Propositions for boolean subjects.
2524
*
2625
* @author Christian Gruber ([email protected])
2726
*/
28-
@NullMarked
2927
public final class BooleanSubject extends Subject {
3028
private final @Nullable Boolean actual;
3129

core/src/main/java/com/google/common/truth/ClassSubject.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,13 @@
1818
import static com.google.common.base.Preconditions.checkNotNull;
1919

2020
import com.google.common.annotations.GwtIncompatible;
21-
import org.jspecify.annotations.NullMarked;
2221
import org.jspecify.annotations.Nullable;
2322

2423
/**
2524
* Propositions for {@link Class} subjects.
2625
*
2726
* @author Kurt Alfred Kluever
2827
*/
29-
@NullMarked
3028
@GwtIncompatible("reflection")
3129
@J2ktIncompatible
3230
public final class ClassSubject extends Subject {

core/src/main/java/com/google/common/truth/ComparableSubject.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import static com.google.common.base.Preconditions.checkNotNull;
1919

2020
import com.google.common.collect.Range;
21-
import org.jspecify.annotations.NullMarked;
2221
import org.jspecify.annotations.Nullable;
2322

2423
/**
@@ -27,7 +26,6 @@
2726
* @author Kurt Alfred Kluever
2827
* @param <T> the type of the object being tested by this {@code ComparableSubject}
2928
*/
30-
@NullMarked
3129
// TODO(b/136040841): Consider further tightening this to the proper `extends Comparable<? super T>`
3230
public abstract class ComparableSubject<T extends Comparable<?>> extends Subject {
3331
/**

core/src/main/java/com/google/common/truth/ComparisonFailureWithFacts.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,12 @@
2121

2222
import com.google.common.collect.ImmutableList;
2323
import com.google.common.truth.Platform.PlatformComparisonFailure;
24-
import org.jspecify.annotations.NullMarked;
2524
import org.jspecify.annotations.Nullable;
2625

2726
/**
2827
* An {@link AssertionError} (usually a JUnit {@code ComparisonFailure}, but not under GWT) composed
2928
* of structured {@link Fact} instances and other string messages.
3029
*/
31-
@NullMarked
3230
final class ComparisonFailureWithFacts extends PlatformComparisonFailure implements ErrorWithFacts {
3331
private final ImmutableList<Fact> facts;
3432

core/src/main/java/com/google/common/truth/ComparisonFailures.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
import com.google.common.annotations.VisibleForTesting;
2828
import com.google.common.collect.ImmutableList;
29-
import org.jspecify.annotations.NullMarked;
3029
import org.jspecify.annotations.Nullable;
3130

3231
/**
@@ -43,7 +42,6 @@
4342
* different implementation under GWT/j2cl, where {@code ComparisonFailure} is also unavailable but
4443
* we can't just recover from that at runtime.
4544
*/
46-
@NullMarked
4745
final class ComparisonFailures {
4846
static ImmutableList<Fact> makeComparisonFailureFacts(
4947
ImmutableList<Fact> headFacts,

core/src/main/java/com/google/common/truth/Correspondence.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import com.google.common.collect.ImmutableList;
3232
import java.util.Arrays;
3333
import java.util.List;
34-
import org.jspecify.annotations.NullMarked;
3534
import org.jspecify.annotations.Nullable;
3635

3736
/**
@@ -66,7 +65,6 @@
6665
*
6766
* @author Pete Gillin
6867
*/
69-
@NullMarked
7068
public abstract class Correspondence<A extends @Nullable Object, E extends @Nullable Object> {
7169

7270
/**

core/src/main/java/com/google/common/truth/CustomSubjectBuilder.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
import static com.google.common.base.Preconditions.checkNotNull;
2020

21-
import org.jspecify.annotations.NullMarked;
22-
2321
/**
2422
* In a fluent assertion chain, exposes one or more "custom" {@code that} methods, which accept a
2523
* value under test and return a {@link Subject}.
@@ -36,7 +34,6 @@
3634
* extensions</a>. It explains the cases in which {@code CustomSubjectBuilder} is necessary, and it
3735
* links to further instructions.
3836
*/
39-
@NullMarked
4037
public abstract class CustomSubjectBuilder {
4138
/**
4239
* In a fluent assertion chain, the argument to the "custom" overload of {@link

core/src/main/java/com/google/common/truth/DiffUtils.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,13 @@
2323
import java.util.HashMap;
2424
import java.util.List;
2525
import java.util.Map;
26-
import org.jspecify.annotations.NullMarked;
2726

2827
/**
2928
* A custom implementation of the diff algorithm based on the solution described at
3029
* https://en.wikipedia.org/wiki/Longest_common_subsequence_problem
3130
*
3231
* @author Yun Peng ([email protected])
3332
*/
34-
@NullMarked
3533
final class DiffUtils {
3634
// A list of unique strings appeared in compared texts.
3735
// The index of each string is its incremental Id.

core/src/main/java/com/google/common/truth/DoubleSubject.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,13 @@
2626
import static java.lang.Double.NaN;
2727
import static java.lang.Double.doubleToLongBits;
2828

29-
import org.jspecify.annotations.NullMarked;
3029
import org.jspecify.annotations.Nullable;
3130

3231
/**
3332
* Propositions for {@link Double} subjects.
3433
*
3534
* @author Kurt Alfred Kluever
3635
*/
37-
@NullMarked
3836
public final class DoubleSubject extends ComparableSubject<Double> {
3937
private static final long NEG_ZERO_BITS = doubleToLongBits(-0.0);
4038

core/src/main/java/com/google/common/truth/ErrorWithFacts.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@
1717
package com.google.common.truth;
1818

1919
import com.google.common.collect.ImmutableList;
20-
import org.jspecify.annotations.NullMarked;
2120

2221
/**
2322
* Supertype of Truth's {@link AssertionError} subclasses that are created from a list of {@link
2423
* Fact} instances.
2524
*/
26-
@NullMarked
2725
interface ErrorWithFacts {
2826
ImmutableList<Fact> facts();
2927
}

core/src/main/java/com/google/common/truth/Expect.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import com.google.errorprone.annotations.concurrent.GuardedBy;
3131
import java.util.ArrayList;
3232
import java.util.List;
33-
import org.jspecify.annotations.NullMarked;
3433
import org.jspecify.annotations.Nullable;
3534
import org.junit.internal.AssumptionViolatedException;
3635
import org.junit.rules.ErrorCollector;
@@ -84,7 +83,6 @@
8483
*/
8584
@GwtIncompatible("JUnit4")
8685
@J2ktIncompatible
87-
@NullMarked
8886
public final class Expect extends StandardSubjectBuilder implements TestRule {
8987

9088
private static final class ExpectationGatherer implements FailureStrategy {

core/src/main/java/com/google/common/truth/ExpectFailure.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import com.google.common.annotations.GwtIncompatible;
2525
import com.google.common.truth.Truth.SimpleAssertionError;
2626
import com.google.errorprone.annotations.CanIgnoreReturnValue;
27-
import org.jspecify.annotations.NullMarked;
2827
import org.jspecify.annotations.Nullable;
2928
import org.junit.runner.Description;
3029
import org.junit.runners.model.Statement;
@@ -67,7 +66,6 @@
6766
* also checks that the assertion you're testing uses the supplied {@link FailureStrategy} and calls
6867
* {@link FailureStrategy#fail} only once.
6968
*/
70-
@NullMarked
7169
public final class ExpectFailure implements Platform.JUnitTestRule {
7270
private boolean inRuleContext = false;
7371
private boolean failureExpected = false;

core/src/main/java/com/google/common/truth/Fact.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
import com.google.common.collect.ImmutableList;
2424
import java.io.Serializable;
25-
import org.jspecify.annotations.NullMarked;
2625
import org.jspecify.annotations.Nullable;
2726

2827
/**
@@ -35,7 +34,6 @@
3534
* <p>If you are writing a custom {@code Subject}, see <a
3635
* href="https://truth.dev/failure_messages">our tips on writing failure messages</a>.
3736
*/
38-
@NullMarked
3937
public final class Fact implements Serializable {
4038
/**
4139
* Creates a fact with the given key and value, which will be printed in a format like "key:

core/src/main/java/com/google/common/truth/FailureMetadata.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929

3030
import com.google.common.base.Function;
3131
import com.google.common.collect.ImmutableList;
32-
import org.jspecify.annotations.NullMarked;
3332
import org.jspecify.annotations.Nullable;
3433

3534
/**
@@ -52,7 +51,6 @@
5251
* using their {@link CustomSubjectBuilder#metadata()} method to get an instance to pass to the
5352
* constructor.)
5453
*/
55-
@NullMarked
5654
public final class FailureMetadata {
5755
static FailureMetadata forFailureStrategy(FailureStrategy failureStrategy) {
5856
return new FailureMetadata(

core/src/main/java/com/google/common/truth/FailureStrategy.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package com.google.common.truth;
1717

18-
import org.jspecify.annotations.NullMarked;
1918

2019
/**
2120
* Defines what to do when a check fails.
@@ -54,7 +53,6 @@
5453
* StandardSubjectBuilder#forCustomFailureStrategy
5554
* StandardSubjectBuilder.forCustomFailureStrategy(STRATEGY)}.
5655
*/
57-
@NullMarked
5856
public interface FailureStrategy {
5957
/**
6058
* Handles a failure. The parameter is an {@code AssertionError} or subclass thereof, and it

core/src/main/java/com/google/common/truth/FloatSubject.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,13 @@
2626
import static java.lang.Float.NaN;
2727
import static java.lang.Float.floatToIntBits;
2828

29-
import org.jspecify.annotations.NullMarked;
3029
import org.jspecify.annotations.Nullable;
3130

3231
/**
3332
* Propositions for {@link Float} subjects.
3433
*
3534
* @author Kurt Alfred Kluever
3635
*/
37-
@NullMarked
3836
public final class FloatSubject extends ComparableSubject<Float> {
3937
private static final int NEG_ZERO_BITS = floatToIntBits(-0.0f);
4038

core/src/main/java/com/google/common/truth/GraphMatching.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,13 @@
2727
import java.util.HashMap;
2828
import java.util.Map;
2929
import java.util.Queue;
30-
import org.jspecify.annotations.NullMarked;
3130

3231
/**
3332
* Helper routines related to <a href="https://en.wikipedia.org/wiki/Matching_(graph_theory)">graph
3433
* matchings</a>.
3534
*
3635
* @author Pete Gillin
3736
*/
38-
@NullMarked
3937
final class GraphMatching {
4038

4139
/**

core/src/main/java/com/google/common/truth/GuavaOptionalSubject.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import static com.google.common.truth.Fact.simpleFact;
2020

2121
import com.google.common.base.Optional;
22-
import org.jspecify.annotations.NullMarked;
2322
import org.jspecify.annotations.Nullable;
2423

2524
/**
@@ -29,7 +28,6 @@
2928
*
3029
* @author Christian Gruber
3130
*/
32-
@NullMarked
3331
public final class GuavaOptionalSubject extends Subject {
3432
@SuppressWarnings("NullableOptional") // Truth always accepts nulls, no matter the type
3533
private final @Nullable Optional<?> actual;

core/src/main/java/com/google/common/truth/IgnoreJRERequirement.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@
2121
import static java.lang.annotation.ElementType.TYPE;
2222

2323
import java.lang.annotation.Target;
24-
import org.jspecify.annotations.NullMarked;
2524

2625
/**
2726
* Disables Animal Sniffer's checking of compatibility with older versions of Java/Android.
2827
*/
2928
@Target({METHOD, CONSTRUCTOR, TYPE})
30-
@NullMarked
3129
@interface IgnoreJRERequirement {}

0 commit comments

Comments
 (0)