Skip to content

Commit

Permalink
Build Truth with -source 8 -target 8, updating annotations appropri…
Browse files Browse the repository at this point in the history
…ately.

This is a small step in the general direction of #746

While there, improve the documentation of deprecated `LiteProtoSubject.isEqualTo(MessageLite.Builder)` and `isNotEqualTo(MessageLite.Builder)`, and add a TODO about possible future steps.

RELNOTES=Truth is now built with `-source 8 -target 8`. This means that it no longer runs under Java 7 VMs. It continues to run under Android, even old versions, for all apps that have [enabled support for Java 8 language features](https://developer.android.com/studio/write/java8-support#supported_features).
PiperOrigin-RevId: 387845958
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Aug 2, 2021
1 parent 8bd81e1 commit 7f45c0c
Show file tree
Hide file tree
Showing 29 changed files with 121 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Map.Entry;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.Handle;
Expand Down Expand Up @@ -1507,14 +1508,5 @@ private static void closeQuietly(InputStream stream) {
}
}

/*
* TODO(cpovirk): Switch to using Checker Framework @Nullable. The problem I see if I try to
* switch now is that the AutoValue-generated code is `@Nullable ActualValueInference.StackEntry`
* rather than `ActualValueInference.@Nullable StackEntry`. AutoValue normally gets this right
* (b/29530042), so I think the failure here is because we use `-source 7`. That might still be
* fine, except that j2objc compilation appears to then use `-source 8`.
*/
@interface Nullable {}

private ActualValueInference() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ enum OldAndNewValuesAreSimilar {
* to set a message is {@code check(...).withMessage(...).that(...)} (for calls from within a
* {@code Subject}) or {@link Truth#assertWithMessage} (for most other calls).
*/
FailureMetadata withMessage(String format, /*@Nullable*/ Object[] args) {
FailureMetadata withMessage(String format, @Nullable Object[] args) {
ImmutableList<LazyMessage> messages = append(this.messages, new LazyMessage(format, args));
return derive(messages, steps);
}
Expand Down
23 changes: 14 additions & 9 deletions core/src/main/java/com/google/common/truth/IterableSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public final void containsNoDuplicates() {

/** Checks that the subject contains at least one of the provided objects or fails. */
public final void containsAnyOf(
@Nullable Object first, @Nullable Object second, @Nullable Object /*@Nullable*/... rest) {
@Nullable Object first, @Nullable Object second, @Nullable Object @Nullable ... rest) {
containsAnyIn(accumulate(first, second, rest));
}

Expand Down Expand Up @@ -255,7 +255,7 @@ public final void containsAnyIn(Object[] expected) {
public final Ordered containsAtLeast(
@Nullable Object firstExpected,
@Nullable Object secondExpected,
@Nullable Object /*@Nullable*/... restOfExpected) {
@Nullable Object @Nullable ... restOfExpected) {
return containsAtLeastElementsIn(accumulate(firstExpected, secondExpected, restOfExpected));
}

Expand Down Expand Up @@ -379,7 +379,7 @@ private static void moveElements(List<?> input, Collection<Object> output, int m
* elements, not an element itself. This helps human readers and avoids a compiler warning.
*/
@CanIgnoreReturnValue
public final Ordered containsExactly(@Nullable Object /*@Nullable*/... varargs) {
public final Ordered containsExactly(@Nullable Object @Nullable ... varargs) {
List<Object> expected = (varargs == null) ? newArrayList((Object) null) : asList(varargs);
return containsExactlyElementsIn(
expected, varargs != null && varargs.length == 1 && varargs[0] instanceof Iterable);
Expand Down Expand Up @@ -694,7 +694,7 @@ enum ElementFactGrouping {
public final void containsNoneOf(
@Nullable Object firstExcluded,
@Nullable Object secondExcluded,
@Nullable Object /*@Nullable*/... restOfExcluded) {
@Nullable Object @Nullable ... restOfExcluded) {
containsNoneIn(accumulate(firstExcluded, secondExcluded, restOfExcluded));
}

Expand Down Expand Up @@ -845,7 +845,7 @@ private void pairwiseCheck(String expectedFact, PairwiseChecker checker) {
@Override
@Deprecated
public void isNoneOf(
@Nullable Object first, @Nullable Object second, @Nullable Object /*@Nullable*/... rest) {
@Nullable Object first, @Nullable Object second, @Nullable Object @Nullable ... rest) {
super.isNoneOf(first, second, rest);
}

Expand Down Expand Up @@ -1056,6 +1056,11 @@ public UsingCorrespondence<A, E> displayingDiffsPairedBy(
* Checks that the subject contains at least one element that corresponds to the given expected
* element.
*/
/*
* TODO(cpovirk): Do we want @Nullable on usages of E? Probably not, since it could throw errors
* during comparisons? Or maybe we should take the risk for user convenience? If we make
* changes, also make them in MapSubject, MultimapSubject, and possibly others.
*/
public void contains(@Nullable E expected) {
Correspondence.ExceptionStore exceptions = Correspondence.ExceptionStore.forIterable();
for (A actual : getCastActual()) {
Expand Down Expand Up @@ -1153,7 +1158,7 @@ public void doesNotContain(@Nullable E excluded) {
*/
@SafeVarargs
@CanIgnoreReturnValue
public final Ordered containsExactly(@Nullable E /*@Nullable*/... expected) {
public final Ordered containsExactly(@Nullable E @Nullable ... expected) {
return containsExactlyElementsIn(
(expected == null) ? newArrayList((E) null) : asList(expected));
}
Expand Down Expand Up @@ -1504,7 +1509,7 @@ private boolean failIfOneToOneMappingHasMissingOrExtra(
@SafeVarargs
@CanIgnoreReturnValue
public final Ordered containsAtLeast(
@Nullable E first, @Nullable E second, @Nullable E /*@Nullable*/... rest) {
@Nullable E first, @Nullable E second, @Nullable E @Nullable ... rest) {
return containsAtLeastElementsIn(accumulate(first, second, rest));
}

Expand Down Expand Up @@ -1753,7 +1758,7 @@ private boolean failIfOneToOneMappingHasMissing(
*/
@SafeVarargs
public final void containsAnyOf(
@Nullable E first, @Nullable E second, @Nullable E /*@Nullable*/... rest) {
@Nullable E first, @Nullable E second, @Nullable E @Nullable ... rest) {
containsAnyIn(accumulate(first, second, rest));
}

Expand Down Expand Up @@ -1861,7 +1866,7 @@ private ImmutableList<Fact> describeAnyMatchesByKey(
public final void containsNoneOf(
@Nullable E firstExcluded,
@Nullable E secondExcluded,
@Nullable E /*@Nullable*/... restOfExcluded) {
@Nullable E @Nullable ... restOfExcluded) {
containsNoneIn(accumulate(firstExcluded, secondExcluded, restOfExcluded));
}

Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/com/google/common/truth/LazyMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import org.checkerframework.checker.nullness.qual.Nullable;

final class LazyMessage {
private static final String PLACEHOLDER_ERR =
Expand All @@ -28,7 +29,7 @@ final class LazyMessage {
private final String format;
private final Object[] args;

LazyMessage(String format, /*@Nullable*/ Object... args) {
LazyMessage(String format, @Nullable Object... args) {
this.format = format;
this.args = args;
int placeholders = countPlaceholders(format);
Expand Down
12 changes: 5 additions & 7 deletions core/src/main/java/com/google/common/truth/MapSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,18 +188,18 @@ public final Ordered containsExactly() {
*/
@CanIgnoreReturnValue
public final Ordered containsExactly(
@Nullable Object k0, @Nullable Object v0, /*@Nullable*/ Object... rest) {
@Nullable Object k0, @Nullable Object v0, @Nullable Object... rest) {
return containsExactlyEntriesIn(accumulateMap("containsExactly", k0, v0, rest));
}

@CanIgnoreReturnValue
public final Ordered containsAtLeast(
@Nullable Object k0, @Nullable Object v0, /*@Nullable*/ Object... rest) {
@Nullable Object k0, @Nullable Object v0, @Nullable Object... rest) {
return containsAtLeastEntriesIn(accumulateMap("containsAtLeast", k0, v0, rest));
}

private static Map<Object, Object> accumulateMap(
String functionName, @Nullable Object k0, @Nullable Object v0, /*@Nullable*/ Object... rest) {
String functionName, @Nullable Object k0, @Nullable Object v0, @Nullable Object... rest) {
checkArgument(
rest.length % 2 == 0,
"There must be an equal number of key/value pairs "
Expand Down Expand Up @@ -682,8 +682,7 @@ public void doesNotContainEntry(@Nullable Object excludedKey, @Nullable E exclud
// TODO(b/25744307): Can we add an error-prone check that rest.length % 2 == 0?
// For bonus points, checking that the even-numbered values are of type E would be sweet.
@CanIgnoreReturnValue
public Ordered containsExactly(
@Nullable Object k0, @Nullable E v0, /*@Nullable*/ Object... rest) {
public Ordered containsExactly(@Nullable Object k0, @Nullable E v0, @Nullable Object... rest) {
@SuppressWarnings("unchecked") // throwing ClassCastException is the correct behaviour
Map<Object, E> expectedMap = (Map<Object, E>) accumulateMap("containsExactly", k0, v0, rest);
return containsExactlyEntriesIn(expectedMap);
Expand All @@ -702,8 +701,7 @@ public Ordered containsExactly(
// TODO(b/25744307): Can we add an error-prone check that rest.length % 2 == 0?
// For bonus points, checking that the even-numbered values are of type E would be sweet.
@CanIgnoreReturnValue
public Ordered containsAtLeast(
@Nullable Object k0, @Nullable E v0, /*@Nullable*/ Object... rest) {
public Ordered containsAtLeast(@Nullable Object k0, @Nullable E v0, @Nullable Object... rest) {
@SuppressWarnings("unchecked") // throwing ClassCastException is the correct behaviour
Map<Object, E> expectedMap = (Map<Object, E>) accumulateMap("containsAtLeast", k0, v0, rest);
return containsAtLeastEntriesIn(expectedMap);
Expand Down
12 changes: 5 additions & 7 deletions core/src/main/java/com/google/common/truth/MultimapSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ public final Ordered containsExactly() {
*/
@CanIgnoreReturnValue
public final Ordered containsExactly(
@Nullable Object k0, @Nullable Object v0, /*@Nullable*/ Object... rest) {
@Nullable Object k0, @Nullable Object v0, @Nullable Object... rest) {
return containsExactlyEntriesIn(accumulateMultimap(k0, v0, rest));
}

Expand All @@ -317,12 +317,12 @@ public final Ordered containsExactly(
*/
@CanIgnoreReturnValue
public final Ordered containsAtLeast(
@Nullable Object k0, @Nullable Object v0, /*@Nullable*/ Object... rest) {
@Nullable Object k0, @Nullable Object v0, @Nullable Object... rest) {
return containsAtLeastEntriesIn(accumulateMultimap(k0, v0, rest));
}

private static Multimap<Object, Object> accumulateMultimap(
@Nullable Object k0, @Nullable Object v0, /*@Nullable*/ Object... rest) {
@Nullable Object k0, @Nullable Object v0, @Nullable Object... rest) {
checkArgument(
rest.length % 2 == 0,
"There must be an equal number of key/value pairs "
Expand Down Expand Up @@ -770,8 +770,7 @@ private <K, V extends E> Ordered internalContainsAtLeastEntriesIn(
* key/value pairs at compile time. Please make sure you provide varargs in key/value pairs!
*/
@CanIgnoreReturnValue
public Ordered containsExactly(
@Nullable Object k0, @Nullable E v0, /*@Nullable*/ Object... rest) {
public Ordered containsExactly(@Nullable Object k0, @Nullable E v0, @Nullable Object... rest) {
@SuppressWarnings("unchecked")
Multimap<?, E> expectedMultimap = (Multimap<?, E>) accumulateMultimap(k0, v0, rest);
return containsExactlyEntriesIn(expectedMultimap);
Expand All @@ -790,8 +789,7 @@ public Ordered containsExactly() {
* key/value pairs at compile time. Please make sure you provide varargs in key/value pairs!
*/
@CanIgnoreReturnValue
public Ordered containsAtLeast(
@Nullable Object k0, @Nullable E v0, /*@Nullable*/ Object... rest) {
public Ordered containsAtLeast(@Nullable Object k0, @Nullable E v0, @Nullable Object... rest) {
@SuppressWarnings("unchecked")
Multimap<?, E> expectedMultimap = (Multimap<?, E>) accumulateMultimap(k0, v0, rest);
return containsAtLeastEntriesIn(expectedMultimap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public final class ObjectArraySubject<T> extends AbstractArraySubject {
private final T[] actual;

ObjectArraySubject(
FailureMetadata metadata, @Nullable T /*@Nullable*/[] o, @Nullable String typeDescription) {
FailureMetadata metadata, @Nullable T @Nullable [] o, @Nullable String typeDescription) {
super(metadata, o, typeDescription);
this.actual = o;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public final class PrimitiveBooleanArraySubject extends AbstractArraySubject {
private final boolean[] actual;

PrimitiveBooleanArraySubject(
FailureMetadata metadata, boolean /*@Nullable*/[] o, @Nullable String typeDescription) {
FailureMetadata metadata, boolean @Nullable [] o, @Nullable String typeDescription) {
super(metadata, o, typeDescription);
this.actual = o;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public final class PrimitiveByteArraySubject extends AbstractArraySubject {
private final byte[] actual;

PrimitiveByteArraySubject(
FailureMetadata metadata, byte /*@Nullable*/[] o, @Nullable String typeDescription) {
FailureMetadata metadata, byte @Nullable [] o, @Nullable String typeDescription) {
super(metadata, o, typeDescription);
this.actual = o;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public final class PrimitiveCharArraySubject extends AbstractArraySubject {
private final char[] actual;

PrimitiveCharArraySubject(
FailureMetadata metadata, char /*@Nullable*/[] o, @Nullable String typeDescription) {
FailureMetadata metadata, char @Nullable [] o, @Nullable String typeDescription) {
super(metadata, o, typeDescription);
this.actual = o;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public final class PrimitiveDoubleArraySubject extends AbstractArraySubject {
private final double[] actual;

PrimitiveDoubleArraySubject(
FailureMetadata metadata, double /*@Nullable*/[] o, @Nullable String typeDescription) {
FailureMetadata metadata, double @Nullable [] o, @Nullable String typeDescription) {
super(metadata, o, typeDescription);
this.actual = o;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public final class PrimitiveFloatArraySubject extends AbstractArraySubject {
private final float[] actual;

PrimitiveFloatArraySubject(
FailureMetadata metadata, float /*@Nullable*/[] o, @Nullable String typeDescription) {
FailureMetadata metadata, float @Nullable [] o, @Nullable String typeDescription) {
super(metadata, o, typeDescription);
this.actual = o;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public final class PrimitiveIntArraySubject extends AbstractArraySubject {
private final int[] actual;

PrimitiveIntArraySubject(
FailureMetadata metadata, int /*@Nullable*/[] o, @Nullable String typeDescription) {
FailureMetadata metadata, int @Nullable [] o, @Nullable String typeDescription) {
super(metadata, o, typeDescription);
this.actual = o;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public final class PrimitiveLongArraySubject extends AbstractArraySubject {
private final long[] actual;

PrimitiveLongArraySubject(
FailureMetadata metadata, long /*@Nullable*/[] o, @Nullable String typeDescription) {
FailureMetadata metadata, long @Nullable [] o, @Nullable String typeDescription) {
super(metadata, o, typeDescription);
this.actual = o;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public final class PrimitiveShortArraySubject extends AbstractArraySubject {
private final short[] actual;

PrimitiveShortArraySubject(
FailureMetadata metadata, short /*@Nullable*/[] o, @Nullable String typeDescription) {
FailureMetadata metadata, short @Nullable [] o, @Nullable String typeDescription) {
super(metadata, o, typeDescription);
this.actual = o;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,39 +112,39 @@ public final IterableSubject that(@Nullable Iterable<?> actual) {
return new IterableSubject(metadata(), actual);
}

public final <T> ObjectArraySubject<T> that(@Nullable T /*@Nullable*/[] actual) {
public final <T> ObjectArraySubject<T> that(@Nullable T @Nullable [] actual) {
return new ObjectArraySubject<>(metadata(), actual, "array");
}

public final PrimitiveBooleanArraySubject that(boolean /*@Nullable*/[] actual) {
public final PrimitiveBooleanArraySubject that(boolean @Nullable [] actual) {
return new PrimitiveBooleanArraySubject(metadata(), actual, "array");
}

public final PrimitiveShortArraySubject that(short /*@Nullable*/[] actual) {
public final PrimitiveShortArraySubject that(short @Nullable [] actual) {
return new PrimitiveShortArraySubject(metadata(), actual, "array");
}

public final PrimitiveIntArraySubject that(int /*@Nullable*/[] actual) {
public final PrimitiveIntArraySubject that(int @Nullable [] actual) {
return new PrimitiveIntArraySubject(metadata(), actual, "array");
}

public final PrimitiveLongArraySubject that(long /*@Nullable*/[] actual) {
public final PrimitiveLongArraySubject that(long @Nullable [] actual) {
return new PrimitiveLongArraySubject(metadata(), actual, "array");
}

public final PrimitiveCharArraySubject that(char /*@Nullable*/[] actual) {
public final PrimitiveCharArraySubject that(char @Nullable [] actual) {
return new PrimitiveCharArraySubject(metadata(), actual, "array");
}

public final PrimitiveByteArraySubject that(byte /*@Nullable*/[] actual) {
public final PrimitiveByteArraySubject that(byte @Nullable [] actual) {
return new PrimitiveByteArraySubject(metadata(), actual, "array");
}

public final PrimitiveFloatArraySubject that(float /*@Nullable*/[] actual) {
public final PrimitiveFloatArraySubject that(float @Nullable [] actual) {
return new PrimitiveFloatArraySubject(metadata(), actual, "array");
}

public final PrimitiveDoubleArraySubject that(double /*@Nullable*/[] actual) {
public final PrimitiveDoubleArraySubject that(double @Nullable [] actual) {
return new PrimitiveDoubleArraySubject(metadata(), actual, "array");
}

Expand Down Expand Up @@ -189,7 +189,7 @@ public final StandardSubjectBuilder withMessage(@Nullable String messageToPrepen
* @throws IllegalArgumentException if the number of placeholders in the format string does not
* equal the number of given arguments
*/
public final StandardSubjectBuilder withMessage(String format, /*@Nullable*/ Object... args) {
public final StandardSubjectBuilder withMessage(String format, @Nullable Object... args) {
return new StandardSubjectBuilder(metadata().withMessage(format, args));
}

Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/com/google/common/truth/Subject.java
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ public void isIn(Iterable<?> iterable) {

/** Fails unless the subject is equal to any of the given elements. */
public void isAnyOf(
@Nullable Object first, @Nullable Object second, @Nullable Object /*@Nullable*/... rest) {
@Nullable Object first, @Nullable Object second, @Nullable Object @Nullable ... rest) {
isIn(accumulate(first, second, rest));
}

Expand All @@ -359,7 +359,7 @@ public void isNotIn(Iterable<?> iterable) {

/** Fails if the subject is equal to any of the given elements. */
public void isNoneOf(
@Nullable Object first, @Nullable Object second, @Nullable Object /*@Nullable*/... rest) {
@Nullable Object first, @Nullable Object second, @Nullable Object @Nullable ... rest) {
isNotIn(accumulate(first, second, rest));
}

Expand Down
Loading

0 comments on commit 7f45c0c

Please sign in to comment.