Skip to content

Commit 2151add

Browse files
stefanhausteinGoogle Java Core Libraries
authored andcommitted
Null-mark Truth.
RELNOTES=Null-mark Truth PiperOrigin-RevId: 516515683
1 parent 99529a3 commit 2151add

Some content is hidden

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

48 files changed

+514
-399
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.google.common.truth;
1717

1818
import static com.google.common.base.Preconditions.checkArgument;
19+
import static com.google.common.base.Preconditions.checkNotNull;
1920
import static com.google.common.truth.Fact.simpleFact;
2021

2122
import java.lang.reflect.Array;
@@ -27,7 +28,7 @@
2728
* @author Christian Gruber ([email protected])
2829
*/
2930
abstract class AbstractArraySubject extends Subject {
30-
private final Object actual;
31+
private final @Nullable Object actual;
3132

3233
AbstractArraySubject(
3334
FailureMetadata metadata, @Nullable Object actual, @Nullable String typeDescription) {
@@ -60,6 +61,6 @@ public final void hasLength(int length) {
6061
}
6162

6263
private int length() {
63-
return Array.getLength(actual);
64+
return Array.getLength(checkNotNull(actual));
6465
}
6566
}

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import static com.google.common.base.MoreObjects.firstNonNull;
1717
import static com.google.common.base.Preconditions.checkArgument;
18+
import static com.google.common.base.Preconditions.checkNotNull;
1819
import static com.google.common.base.Preconditions.checkState;
1920
import static com.google.common.collect.Iterables.getOnlyElement;
2021
import static java.lang.Thread.currentThread;
@@ -974,9 +975,11 @@ private void pushDescriptor(String desc) {
974975
* @param invocation the method invocation being visited, or {@code null} if a non-method
975976
* descriptor is being visited
976977
*/
977-
private void pushDescriptorAndMaybeProcessMethodCall(String desc, Invocation invocation) {
978+
private void pushDescriptorAndMaybeProcessMethodCall(
979+
String desc, @Nullable Invocation invocation) {
978980
if (invocation != null && invocation.isOnSubjectInstance()) {
979-
actualValueAtLocation.put(labelsSeen.build(), invocation.receiver().actualValue());
981+
actualValueAtLocation.put(
982+
labelsSeen.build(), checkNotNull(invocation.receiver()).actualValue());
980983
}
981984

982985
boolean hasParams = invocation != null && (Type.getArgumentsAndReturnSizes(desc) >> 2) > 1;
@@ -1011,7 +1014,8 @@ private void pushDescriptorAndMaybeProcessMethodCall(String desc, Invocation inv
10111014
}
10121015
}
10131016

1014-
private void pushMaybeDescribed(InferredType type, Invocation invocation, boolean hasParams) {
1017+
private void pushMaybeDescribed(
1018+
InferredType type, @Nullable Invocation invocation, boolean hasParams) {
10151019
push(invocation == null ? opaque(type) : invocation.deriveEntry(type, hasParams));
10161020
}
10171021

@@ -1032,10 +1036,11 @@ private StackEntry pop(int count) {
10321036
operandStack,
10331037
methodSignature);
10341038
int expectedLastIndex = operandStack.size() - count - 1;
1035-
StackEntry lastPopped = null;
1036-
for (int i = operandStack.size() - 1; i > expectedLastIndex; --i) {
1037-
lastPopped = operandStack.remove(i);
1038-
}
1039+
1040+
StackEntry lastPopped;
1041+
do {
1042+
lastPopped = operandStack.remove(operandStack.size() - 1);
1043+
} while (operandStack.size() - 1 > expectedLastIndex);
10391044
return lastPopped;
10401045
}
10411046

@@ -1262,7 +1267,7 @@ final StackEntry deriveEntry(InferredType type, boolean hasParams) {
12621267
} else if (actualValue() != null) {
12631268
return subjectFor(type, actualValue());
12641269
} else if (isOnSubjectInstance()) {
1265-
return subjectFor(type, receiver().actualValue());
1270+
return subjectFor(type, checkNotNull(receiver()).actualValue());
12661271
} else if (BORING_NAMES.contains(name())) {
12671272
/*
12681273
* TODO(cpovirk): For no-arg instance methods like get(), return "foo.get()," where "foo" is
@@ -1496,7 +1501,7 @@ private static boolean isSet(int flags, int bitmask) {
14961501
return (flags & bitmask) == bitmask;
14971502
}
14981503

1499-
private static void closeQuietly(InputStream stream) {
1504+
private static void closeQuietly(@Nullable InputStream stream) {
15001505
if (stream == null) {
15011506
return;
15021507
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ final class AssertionErrorWithFacts extends AssertionError implements ErrorWithF
4747

4848
@Override
4949
@SuppressWarnings("UnsynchronizedOverridesSynchronized")
50-
public Throwable getCause() {
50+
public @Nullable Throwable getCause() {
5151
return cause;
5252
}
5353

5454
@Override
5555
public String toString() {
56-
return getLocalizedMessage();
56+
return checkNotNull(getLocalizedMessage());
5757
}
5858

5959
@Override

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

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

18+
import static com.google.common.base.Preconditions.checkNotNull;
1819
import static com.google.common.truth.Fact.fact;
1920
import static com.google.common.truth.Fact.simpleFact;
2021

@@ -27,7 +28,7 @@
2728
* @author Kurt Alfred Kluever
2829
*/
2930
public final class BigDecimalSubject extends ComparableSubject<BigDecimal> {
30-
private final BigDecimal actual;
31+
private final @Nullable BigDecimal actual;
3132

3233
BigDecimalSubject(FailureMetadata metadata, @Nullable BigDecimal actual) {
3334
super(metadata, actual);
@@ -88,12 +89,12 @@ public void isEqualTo(@Nullable Object expected) {
8889
* #isEqualTo(Object)}.
8990
*/
9091
@Override
91-
public void isEquivalentAccordingToCompareTo(BigDecimal expected) {
92+
public void isEquivalentAccordingToCompareTo(@Nullable BigDecimal expected) {
9293
compareValues(expected);
9394
}
9495

95-
private void compareValues(BigDecimal expected) {
96-
if (actual.compareTo(expected) != 0) {
96+
private void compareValues(@Nullable BigDecimal expected) {
97+
if (checkNotNull(actual).compareTo(expected) != 0) {
9798
failWithoutActual(fact("expected", expected), butWas(), simpleFact("(scale is ignored)"));
9899
}
99100
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
* @author Christian Gruber ([email protected])
2626
*/
2727
public final class BooleanSubject extends Subject {
28-
private final Boolean actual;
28+
private final @Nullable Boolean actual;
2929

3030
BooleanSubject(FailureMetadata metadata, @Nullable Boolean actual) {
3131
super(metadata, actual);

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

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

18+
import static com.google.common.base.Preconditions.checkNotNull;
19+
1820
import com.google.common.annotations.GwtIncompatible;
1921
import org.checkerframework.checker.nullness.qual.Nullable;
2022

@@ -25,7 +27,7 @@
2527
*/
2628
@GwtIncompatible("reflection")
2729
public final class ClassSubject extends Subject {
28-
private final Class<?> actual;
30+
private final @Nullable Class<?> actual;
2931

3032
ClassSubject(FailureMetadata metadata, @Nullable Class<?> o) {
3133
super(metadata, o);
@@ -37,7 +39,7 @@ public final class ClassSubject extends Subject {
3739
* class or interface.
3840
*/
3941
public void isAssignableTo(Class<?> clazz) {
40-
if (!clazz.isAssignableFrom(actual)) {
42+
if (!clazz.isAssignableFrom(checkNotNull(actual))) {
4143
failWithActual("expected to be assignable to", clazz.getName());
4244
}
4345
}

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

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

18+
import static com.google.common.base.Preconditions.checkNotNull;
19+
1820
import com.google.common.collect.Range;
1921
import org.checkerframework.checker.nullness.qual.Nullable;
2022

@@ -29,7 +31,7 @@ public abstract class ComparableSubject<T extends Comparable> extends Subject {
2931
* Constructor for use by subclasses. If you want to create an instance of this class itself, call
3032
* {@link Subject#check(String, Object...) check(...)}{@code .that(actual)}.
3133
*/
32-
private final T actual;
34+
private final @Nullable T actual;
3335

3436
protected ComparableSubject(FailureMetadata metadata, @Nullable T actual) {
3537
super(metadata, actual);
@@ -38,14 +40,14 @@ protected ComparableSubject(FailureMetadata metadata, @Nullable T actual) {
3840

3941
/** Checks that the subject is in {@code range}. */
4042
public final void isIn(Range<T> range) {
41-
if (!range.contains(actual)) {
43+
if (!range.contains(checkNotNull(actual))) {
4244
failWithActual("expected to be in range", range);
4345
}
4446
}
4547

4648
/** Checks that the subject is <i>not</i> in {@code range}. */
4749
public final void isNotIn(Range<T> range) {
48-
if (range.contains(actual)) {
50+
if (range.contains(checkNotNull(actual))) {
4951
failWithActual("expected not to be in range", range);
5052
}
5153
}
@@ -57,8 +59,8 @@ public final void isNotIn(Range<T> range) {
5759
* <p><b>Note:</b> Do not use this method for checking object equality. Instead, use {@link
5860
* #isEqualTo(Object)}.
5961
*/
60-
public void isEquivalentAccordingToCompareTo(T expected) {
61-
if (actual.compareTo(expected) != 0) {
62+
public void isEquivalentAccordingToCompareTo(@Nullable T expected) {
63+
if (checkNotNull(actual).compareTo(expected) != 0) {
6264
failWithActual("expected value that sorts equal to", expected);
6365
}
6466
}
@@ -69,8 +71,8 @@ public void isEquivalentAccordingToCompareTo(T expected) {
6971
* <p>To check that the subject is greater than <i>or equal to</i> {@code other}, use {@link
7072
* #isAtLeast}.
7173
*/
72-
public final void isGreaterThan(T other) {
73-
if (actual.compareTo(other) <= 0) {
74+
public final void isGreaterThan(@Nullable T other) {
75+
if (checkNotNull(actual).compareTo(other) <= 0) {
7476
failWithActual("expected to be greater than", other);
7577
}
7678
}
@@ -81,8 +83,8 @@ public final void isGreaterThan(T other) {
8183
* <p>To check that the subject is less than <i>or equal to</i> {@code other}, use {@link
8284
* #isAtMost}.
8385
*/
84-
public final void isLessThan(T other) {
85-
if (actual.compareTo(other) >= 0) {
86+
public final void isLessThan(@Nullable T other) {
87+
if (checkNotNull(actual).compareTo(other) >= 0) {
8688
failWithActual("expected to be less than", other);
8789
}
8890
}
@@ -93,8 +95,8 @@ public final void isLessThan(T other) {
9395
* <p>To check that the subject is <i>strictly</i> less than {@code other}, use {@link
9496
* #isLessThan}.
9597
*/
96-
public final void isAtMost(T other) {
97-
if (actual.compareTo(other) > 0) {
98+
public final void isAtMost(@Nullable T other) {
99+
if (checkNotNull(actual).compareTo(other) > 0) {
98100
failWithActual("expected to be at most", other);
99101
}
100102
}
@@ -105,8 +107,8 @@ public final void isAtMost(T other) {
105107
* <p>To check that the subject is <i>strictly</i> greater than {@code other}, use {@link
106108
* #isGreaterThan}.
107109
*/
108-
public final void isAtLeast(T other) {
109-
if (actual.compareTo(other) < 0) {
110+
public final void isAtLeast(@Nullable T other) {
111+
if (checkNotNull(actual).compareTo(other) < 0) {
110112
failWithActual("expected to be at least", other);
111113
}
112114
}

0 commit comments

Comments
 (0)