Skip to content

Commit

Permalink
Null-mark Truth.
Browse files Browse the repository at this point in the history
RELNOTES=Null-mark Truth
PiperOrigin-RevId: 516515683
  • Loading branch information
stefanhaustein authored and Google Java Core Libraries committed Mar 14, 2023
1 parent 99529a3 commit 2151add
Show file tree
Hide file tree
Showing 48 changed files with 514 additions and 399 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.google.common.truth;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.truth.Fact.simpleFact;

import java.lang.reflect.Array;
Expand All @@ -27,7 +28,7 @@
* @author Christian Gruber (cgruber@israfil.net)
*/
abstract class AbstractArraySubject extends Subject {
private final Object actual;
private final @Nullable Object actual;

AbstractArraySubject(
FailureMetadata metadata, @Nullable Object actual, @Nullable String typeDescription) {
Expand Down Expand Up @@ -60,6 +61,6 @@ public final void hasLength(int length) {
}

private int length() {
return Array.getLength(actual);
return Array.getLength(checkNotNull(actual));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.Iterables.getOnlyElement;
import static java.lang.Thread.currentThread;
Expand Down Expand Up @@ -974,9 +975,11 @@ private void pushDescriptor(String desc) {
* @param invocation the method invocation being visited, or {@code null} if a non-method
* descriptor is being visited
*/
private void pushDescriptorAndMaybeProcessMethodCall(String desc, Invocation invocation) {
private void pushDescriptorAndMaybeProcessMethodCall(
String desc, @Nullable Invocation invocation) {
if (invocation != null && invocation.isOnSubjectInstance()) {
actualValueAtLocation.put(labelsSeen.build(), invocation.receiver().actualValue());
actualValueAtLocation.put(
labelsSeen.build(), checkNotNull(invocation.receiver()).actualValue());
}

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

private void pushMaybeDescribed(InferredType type, Invocation invocation, boolean hasParams) {
private void pushMaybeDescribed(
InferredType type, @Nullable Invocation invocation, boolean hasParams) {
push(invocation == null ? opaque(type) : invocation.deriveEntry(type, hasParams));
}

Expand All @@ -1032,10 +1036,11 @@ private StackEntry pop(int count) {
operandStack,
methodSignature);
int expectedLastIndex = operandStack.size() - count - 1;
StackEntry lastPopped = null;
for (int i = operandStack.size() - 1; i > expectedLastIndex; --i) {
lastPopped = operandStack.remove(i);
}

StackEntry lastPopped;
do {
lastPopped = operandStack.remove(operandStack.size() - 1);
} while (operandStack.size() - 1 > expectedLastIndex);
return lastPopped;
}

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

private static void closeQuietly(InputStream stream) {
private static void closeQuietly(@Nullable InputStream stream) {
if (stream == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ final class AssertionErrorWithFacts extends AssertionError implements ErrorWithF

@Override
@SuppressWarnings("UnsynchronizedOverridesSynchronized")
public Throwable getCause() {
public @Nullable Throwable getCause() {
return cause;
}

@Override
public String toString() {
return getLocalizedMessage();
return checkNotNull(getLocalizedMessage());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.google.common.truth;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.truth.Fact.fact;
import static com.google.common.truth.Fact.simpleFact;

Expand All @@ -27,7 +28,7 @@
* @author Kurt Alfred Kluever
*/
public final class BigDecimalSubject extends ComparableSubject<BigDecimal> {
private final BigDecimal actual;
private final @Nullable BigDecimal actual;

BigDecimalSubject(FailureMetadata metadata, @Nullable BigDecimal actual) {
super(metadata, actual);
Expand Down Expand Up @@ -88,12 +89,12 @@ public void isEqualTo(@Nullable Object expected) {
* #isEqualTo(Object)}.
*/
@Override
public void isEquivalentAccordingToCompareTo(BigDecimal expected) {
public void isEquivalentAccordingToCompareTo(@Nullable BigDecimal expected) {
compareValues(expected);
}

private void compareValues(BigDecimal expected) {
if (actual.compareTo(expected) != 0) {
private void compareValues(@Nullable BigDecimal expected) {
if (checkNotNull(actual).compareTo(expected) != 0) {
failWithoutActual(fact("expected", expected), butWas(), simpleFact("(scale is ignored)"));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* @author Christian Gruber (cgruber@israfil.net)
*/
public final class BooleanSubject extends Subject {
private final Boolean actual;
private final @Nullable Boolean actual;

BooleanSubject(FailureMetadata metadata, @Nullable Boolean actual) {
super(metadata, actual);
Expand Down
6 changes: 4 additions & 2 deletions core/src/main/java/com/google/common/truth/ClassSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.google.common.truth;

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

import com.google.common.annotations.GwtIncompatible;
import org.checkerframework.checker.nullness.qual.Nullable;

Expand All @@ -25,7 +27,7 @@
*/
@GwtIncompatible("reflection")
public final class ClassSubject extends Subject {
private final Class<?> actual;
private final @Nullable Class<?> actual;

ClassSubject(FailureMetadata metadata, @Nullable Class<?> o) {
super(metadata, o);
Expand All @@ -37,7 +39,7 @@ public final class ClassSubject extends Subject {
* class or interface.
*/
public void isAssignableTo(Class<?> clazz) {
if (!clazz.isAssignableFrom(actual)) {
if (!clazz.isAssignableFrom(checkNotNull(actual))) {
failWithActual("expected to be assignable to", clazz.getName());
}
}
Expand Down
28 changes: 15 additions & 13 deletions core/src/main/java/com/google/common/truth/ComparableSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.google.common.truth;

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

import com.google.common.collect.Range;
import org.checkerframework.checker.nullness.qual.Nullable;

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

protected ComparableSubject(FailureMetadata metadata, @Nullable T actual) {
super(metadata, actual);
Expand All @@ -38,14 +40,14 @@ protected ComparableSubject(FailureMetadata metadata, @Nullable T actual) {

/** Checks that the subject is in {@code range}. */
public final void isIn(Range<T> range) {
if (!range.contains(actual)) {
if (!range.contains(checkNotNull(actual))) {
failWithActual("expected to be in range", range);
}
}

/** Checks that the subject is <i>not</i> in {@code range}. */
public final void isNotIn(Range<T> range) {
if (range.contains(actual)) {
if (range.contains(checkNotNull(actual))) {
failWithActual("expected not to be in range", range);
}
}
Expand All @@ -57,8 +59,8 @@ public final void isNotIn(Range<T> range) {
* <p><b>Note:</b> Do not use this method for checking object equality. Instead, use {@link
* #isEqualTo(Object)}.
*/
public void isEquivalentAccordingToCompareTo(T expected) {
if (actual.compareTo(expected) != 0) {
public void isEquivalentAccordingToCompareTo(@Nullable T expected) {
if (checkNotNull(actual).compareTo(expected) != 0) {
failWithActual("expected value that sorts equal to", expected);
}
}
Expand All @@ -69,8 +71,8 @@ public void isEquivalentAccordingToCompareTo(T expected) {
* <p>To check that the subject is greater than <i>or equal to</i> {@code other}, use {@link
* #isAtLeast}.
*/
public final void isGreaterThan(T other) {
if (actual.compareTo(other) <= 0) {
public final void isGreaterThan(@Nullable T other) {
if (checkNotNull(actual).compareTo(other) <= 0) {
failWithActual("expected to be greater than", other);
}
}
Expand All @@ -81,8 +83,8 @@ public final void isGreaterThan(T other) {
* <p>To check that the subject is less than <i>or equal to</i> {@code other}, use {@link
* #isAtMost}.
*/
public final void isLessThan(T other) {
if (actual.compareTo(other) >= 0) {
public final void isLessThan(@Nullable T other) {
if (checkNotNull(actual).compareTo(other) >= 0) {
failWithActual("expected to be less than", other);
}
}
Expand All @@ -93,8 +95,8 @@ public final void isLessThan(T other) {
* <p>To check that the subject is <i>strictly</i> less than {@code other}, use {@link
* #isLessThan}.
*/
public final void isAtMost(T other) {
if (actual.compareTo(other) > 0) {
public final void isAtMost(@Nullable T other) {
if (checkNotNull(actual).compareTo(other) > 0) {
failWithActual("expected to be at most", other);
}
}
Expand All @@ -105,8 +107,8 @@ public final void isAtMost(T other) {
* <p>To check that the subject is <i>strictly</i> greater than {@code other}, use {@link
* #isGreaterThan}.
*/
public final void isAtLeast(T other) {
if (actual.compareTo(other) < 0) {
public final void isAtLeast(@Nullable T other) {
if (checkNotNull(actual).compareTo(other) < 0) {
failWithActual("expected to be at least", other);
}
}
Expand Down
Loading

0 comments on commit 2151add

Please sign in to comment.