Add @NullMarked to the testing package, except for some @GwtIncompatible classes with non-obvious adjustment requirements.
RELNOTES=n/a
PiperOrigin-RevId: 526930990
diff --git a/android/guava-testlib/src/com/google/common/testing/ArbitraryInstances.java b/android/guava-testlib/src/com/google/common/testing/ArbitraryInstances.java
index 0f66184..a937d73 100644
--- a/android/guava-testlib/src/com/google/common/testing/ArbitraryInstances.java
+++ b/android/guava-testlib/src/com/google/common/testing/ArbitraryInstances.java
@@ -167,6 +167,7 @@
* @since 12.0
*/
@GwtIncompatible
+@ElementTypesAreNonnullByDefault
public final class ArbitraryInstances {
private static final Ordering<Field> BY_FIELD_NAME =
@@ -347,7 +348,7 @@
}
if (type.isEnum()) {
T[] enumConstants = type.getEnumConstants();
- return (enumConstants.length == 0) ? null : enumConstants[0];
+ return (enumConstants == null || enumConstants.length == 0) ? null : enumConstants[0];
}
if (type.isArray()) {
return createEmptyArray(type);
@@ -494,11 +495,12 @@
}
// Always equal is a valid total ordering. And it works for any Object.
- private static final class AlwaysEqual extends Ordering<Object> implements Serializable {
+ private static final class AlwaysEqual extends Ordering<@Nullable Object>
+ implements Serializable {
private static final AlwaysEqual INSTANCE = new AlwaysEqual();
@Override
- public int compare(Object o1, Object o2) {
+ public int compare(@Nullable Object o1, @Nullable Object o2) {
return 0;
}
diff --git a/android/guava-testlib/src/com/google/common/testing/ClassSanityTester.java b/android/guava-testlib/src/com/google/common/testing/ClassSanityTester.java
index ca8d1b5..11d176f 100644
--- a/android/guava-testlib/src/com/google/common/testing/ClassSanityTester.java
+++ b/android/guava-testlib/src/com/google/common/testing/ClassSanityTester.java
@@ -344,7 +344,7 @@
FactoryMethodReturnsNullException {
if (cls.isEnum()) {
T[] constants = cls.getEnumConstants();
- if (constants.length > 0) {
+ if (constants != null && constants.length > 0) {
return constants[0];
} else {
return null;
@@ -576,7 +576,7 @@
IllegalAccessException, InvocationTargetException, FactoryMethodReturnsNullException {
List<Parameter> params = factory.getParameters();
List<FreshValueGenerator> argGenerators = Lists.newArrayListWithCapacity(params.size());
- List<Object> args = Lists.newArrayListWithCapacity(params.size());
+ List<@Nullable Object> args = Lists.newArrayListWithCapacity(params.size());
for (Parameter param : params) {
FreshValueGenerator generator = newFreshValueGenerator();
argGenerators.add(generator);
@@ -634,7 +634,7 @@
Object shouldBeEqualArg = generateDummyArg(param, newFreshValueGenerator());
if (arg != shouldBeEqualArg
&& Objects.equal(arg, shouldBeEqualArg)
- && hashCodeInsensitiveToArgReference(factory, args, i, shouldBeEqualArg)
+ && hashCodeInsensitiveToArgReference(factory, args, i, checkNotNull(shouldBeEqualArg))
&& hashCodeInsensitiveToArgReference(
factory, args, i, generateDummyArg(param, newFreshValueGenerator()))) {
// If the implementation uses identityHashCode(), referential equality is
@@ -834,7 +834,7 @@
}
@Override
- public boolean equals(Object obj) {
+ public boolean equals(@Nullable Object obj) {
return obj instanceof SerializableDummyProxy;
}
diff --git a/android/guava-testlib/src/com/google/common/testing/ClusterException.java b/android/guava-testlib/src/com/google/common/testing/ClusterException.java
index 30ca12e..d94103d 100644
--- a/android/guava-testlib/src/com/google/common/testing/ClusterException.java
+++ b/android/guava-testlib/src/com/google/common/testing/ClusterException.java
@@ -59,6 +59,7 @@
* @author Luiz-Otavio Zorzella
*/
@GwtCompatible
+@ElementTypesAreNonnullByDefault
final class ClusterException extends RuntimeException {
final Collection<? extends Throwable> exceptions;
diff --git a/android/guava-testlib/src/com/google/common/testing/DummyProxy.java b/android/guava-testlib/src/com/google/common/testing/DummyProxy.java
index 85e229d..ed5af92 100644
--- a/android/guava-testlib/src/com/google/common/testing/DummyProxy.java
+++ b/android/guava-testlib/src/com/google/common/testing/DummyProxy.java
@@ -30,6 +30,7 @@
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Set;
+import org.checkerframework.checker.nullness.qual.Nullable;
/**
* Generates a dummy interface proxy that simply returns a dummy value for each method.
@@ -37,6 +38,7 @@
* @author Ben Yu
*/
@GwtIncompatible
+@ElementTypesAreNonnullByDefault
abstract class DummyProxy {
/**
@@ -59,7 +61,7 @@
}
/** Returns the dummy return value for {@code returnType}. */
- abstract <R> R dummyReturnValue(TypeToken<R> returnType);
+ abstract <R> @Nullable R dummyReturnValue(TypeToken<R> returnType);
private class DummyHandler extends AbstractInvocationHandler implements Serializable {
private final TypeToken<?> interfaceType;
@@ -69,7 +71,7 @@
}
@Override
- protected Object handleInvocation(Object proxy, Method method, Object[] args) {
+ protected @Nullable Object handleInvocation(Object proxy, Method method, Object[] args) {
Invokable<?, ?> invokable = interfaceType.method(method);
ImmutableList<Parameter> params = invokable.getParameters();
for (int i = 0; i < args.length; i++) {
@@ -87,7 +89,7 @@
}
@Override
- public boolean equals(Object obj) {
+ public boolean equals(@Nullable Object obj) {
if (obj instanceof DummyHandler) {
DummyHandler that = (DummyHandler) obj;
return identity().equals(that.identity());
diff --git a/android/guava-testlib/src/com/google/common/testing/ElementTypesAreNonnullByDefault.java b/android/guava-testlib/src/com/google/common/testing/ElementTypesAreNonnullByDefault.java
new file mode 100644
index 0000000..1362176
--- /dev/null
+++ b/android/guava-testlib/src/com/google/common/testing/ElementTypesAreNonnullByDefault.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2021 The Guava Authors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.google.common.testing;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.PARAMETER;
+import static java.lang.annotation.ElementType.TYPE;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import com.google.common.annotations.GwtCompatible;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+import javax.annotation.Nonnull;
+import javax.annotation.meta.TypeQualifierDefault;
+
+/**
+ * Marks all "top-level" types as non-null in a way that is recognized by Kotlin. Note that this
+ * unfortunately includes type-variable usages, so we also provide {@link ParametricNullness} to
+ * "undo" it as best we can.
+ */
+@GwtCompatible
+@Retention(RUNTIME)
+@Target(TYPE)
+@TypeQualifierDefault({FIELD, METHOD, PARAMETER})
+@Nonnull
+@interface ElementTypesAreNonnullByDefault {}
diff --git a/android/guava-testlib/src/com/google/common/testing/EqualsTester.java b/android/guava-testlib/src/com/google/common/testing/EqualsTester.java
index 37472a3..d448470 100644
--- a/android/guava-testlib/src/com/google/common/testing/EqualsTester.java
+++ b/android/guava-testlib/src/com/google/common/testing/EqualsTester.java
@@ -22,11 +22,12 @@
import com.google.common.annotations.GwtCompatible;
import com.google.common.base.Equivalence;
-import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
+import java.util.ArrayList;
import java.util.List;
+import org.checkerframework.checker.nullness.qual.Nullable;
/**
* Tester for equals() and hashCode() methods of a class.
@@ -75,6 +76,7 @@
* @since 10.0
*/
@GwtCompatible
+@ElementTypesAreNonnullByDefault
public final class EqualsTester {
private static final int REPETITIONS = 3;
@@ -95,9 +97,17 @@
* equal to any other equality groups added to this tester.
*/
@CanIgnoreReturnValue
- public EqualsTester addEqualityGroup(Object... equalityGroup) {
+ public EqualsTester addEqualityGroup(@Nullable Object @Nullable ... equalityGroup) {
checkNotNull(equalityGroup);
- equalityGroups.add(ImmutableList.copyOf(equalityGroup));
+ List<Object> list = new ArrayList<>(equalityGroup.length);
+ for (int i = 0; i < equalityGroup.length; i++) {
+ Object element = equalityGroup[i];
+ if (element == null) {
+ throw new NullPointerException("at index " + i);
+ }
+ list.add(element);
+ }
+ equalityGroups.add(list);
return this;
}
diff --git a/android/guava-testlib/src/com/google/common/testing/EquivalenceTester.java b/android/guava-testlib/src/com/google/common/testing/EquivalenceTester.java
index b690729..ec34752 100644
--- a/android/guava-testlib/src/com/google/common/testing/EquivalenceTester.java
+++ b/android/guava-testlib/src/com/google/common/testing/EquivalenceTester.java
@@ -50,6 +50,7 @@
* @since 10.0
*/
@GwtCompatible
+@ElementTypesAreNonnullByDefault
public final class EquivalenceTester<T> {
private static final int REPETITIONS = 3;
diff --git a/android/guava-testlib/src/com/google/common/testing/FakeTicker.java b/android/guava-testlib/src/com/google/common/testing/FakeTicker.java
index 8e3ca4a..8c0e002 100644
--- a/android/guava-testlib/src/com/google/common/testing/FakeTicker.java
+++ b/android/guava-testlib/src/com/google/common/testing/FakeTicker.java
@@ -35,6 +35,7 @@
* @author Jige Yu
* @since 10.0
*/
+@ElementTypesAreNonnullByDefault
@GwtCompatible
public class FakeTicker extends Ticker {
diff --git a/android/guava-testlib/src/com/google/common/testing/ForwardingWrapperTester.java b/android/guava-testlib/src/com/google/common/testing/ForwardingWrapperTester.java
index 9b156c6..eb2949b 100644
--- a/android/guava-testlib/src/com/google/common/testing/ForwardingWrapperTester.java
+++ b/android/guava-testlib/src/com/google/common/testing/ForwardingWrapperTester.java
@@ -35,6 +35,7 @@
import java.lang.reflect.Modifier;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
+import org.checkerframework.checker.nullness.qual.Nullable;
/**
* Tester to ensure forwarding wrapper works by delegating calls to the corresponding method with
@@ -54,6 +55,7 @@
* @since 14.0
*/
@GwtIncompatible
+@ElementTypesAreNonnullByDefault
public final class ForwardingWrapperTester {
private boolean testsEquals = false;
@@ -173,9 +175,9 @@
wrapperFunction.apply(proxy).toString());
}
- private static Object[] getParameterValues(Method method) {
+ private static @Nullable Object[] getParameterValues(Method method) {
FreshValueGenerator paramValues = new FreshValueGenerator();
- List<Object> passedArgs = Lists.newArrayList();
+ List<@Nullable Object> passedArgs = Lists.newArrayList();
for (Class<?> paramType : method.getParameterTypes()) {
passedArgs.add(paramValues.generateFresh(paramType));
}
@@ -187,8 +189,8 @@
private final Class<T> interfaceType;
private final Method method;
- private final Object[] passedArgs;
- private final Object returnValue;
+ private final @Nullable Object[] passedArgs;
+ private final @Nullable Object returnValue;
private final AtomicInteger called = new AtomicInteger();
InteractionTester(Class<T> interfaceType, Method method) {
@@ -199,8 +201,8 @@
}
@Override
- protected Object handleInvocation(Object p, Method calledMethod, Object[] args)
- throws Throwable {
+ protected @Nullable Object handleInvocation(
+ Object p, Method calledMethod, @Nullable Object[] args) throws Throwable {
assertEquals(method, calledMethod);
assertEquals(method + " invoked more than once.", 0, called.get());
for (int i = 0; i < passedArgs.length; i++) {
diff --git a/android/guava-testlib/src/com/google/common/testing/FreshValueGenerator.java b/android/guava-testlib/src/com/google/common/testing/FreshValueGenerator.java
index f2ac359..3c25577 100644
--- a/android/guava-testlib/src/com/google/common/testing/FreshValueGenerator.java
+++ b/android/guava-testlib/src/com/google/common/testing/FreshValueGenerator.java
@@ -197,7 +197,7 @@
* Generates an instance for {@code type} using the current {@link #freshness}. The generated
* instance may or may not be unique across different calls.
*/
- private Object generate(TypeToken<?> type) {
+ private @Nullable Object generate(TypeToken<?> type) {
Class<?> rawType = type.getRawType();
List<Object> samples = sampleInstances.get(rawType);
Object sample = pickInstance(samples, null);
@@ -208,7 +208,7 @@
return pickInstance(rawType.getEnumConstants(), null);
}
if (type.isArray()) {
- TypeToken<?> componentType = type.getComponentType();
+ TypeToken<?> componentType = checkNotNull(type.getComponentType());
Object array = Array.newInstance(componentType.getRawType(), 1);
Array.set(array, 0, generate(componentType));
return array;
@@ -254,7 +254,7 @@
return defaultGenerate(rawType);
}
- private <T> T defaultGenerate(Class<T> rawType) {
+ private <T> @Nullable T defaultGenerate(Class<T> rawType) {
if (rawType.isInterface()) {
// always create a new proxy
return newProxy(rawType);
@@ -287,7 +287,7 @@
}
@Override
- protected Object handleInvocation(Object proxy, Method method, Object[] args) {
+ protected Object handleInvocation(Object proxy, Method method, @Nullable Object[] args) {
return interfaceMethodCalled(interfaceType, method);
}
@@ -618,7 +618,7 @@
}
@Generates
- <T> Ordering<T> generateOrdering() {
+ <T extends @Nullable Object> Ordering<T> generateOrdering() {
return new Ordering<T>() {
@Override
public int compare(T left, T right) {
@@ -901,14 +901,12 @@
}
@Generates
- static <R, C, V> Table<R, C, V> generateTable(
- @Nullable R row, @Nullable C column, @Nullable V value) {
+ static <R, C, V> Table<R, C, V> generateTable(R row, C column, V value) {
return generateHashBasedTable(row, column, value);
}
@Generates
- static <R, C, V> HashBasedTable<R, C, V> generateHashBasedTable(
- @Nullable R row, @Nullable C column, @Nullable V value) {
+ static <R, C, V> HashBasedTable<R, C, V> generateHashBasedTable(R row, C column, V value) {
HashBasedTable<R, C, V> table = HashBasedTable.create();
table.put(row, column, value);
return table;
diff --git a/android/guava-testlib/src/com/google/common/testing/GcFinalization.java b/android/guava-testlib/src/com/google/common/testing/GcFinalization.java
index da6aa22..60b2405 100644
--- a/android/guava-testlib/src/com/google/common/testing/GcFinalization.java
+++ b/android/guava-testlib/src/com/google/common/testing/GcFinalization.java
@@ -104,6 +104,7 @@
*/
@GwtIncompatible
@J2ObjCIncompatible // gc
+@ElementTypesAreNonnullByDefault
public final class GcFinalization {
private GcFinalization() {}
diff --git a/android/guava-testlib/src/com/google/common/testing/NullPointerTester.java b/android/guava-testlib/src/com/google/common/testing/NullPointerTester.java
index 96bcb30..d5d5d5d 100644
--- a/android/guava-testlib/src/com/google/common/testing/NullPointerTester.java
+++ b/android/guava-testlib/src/com/google/common/testing/NullPointerTester.java
@@ -68,6 +68,7 @@
* @since 10.0
*/
@GwtIncompatible
+@ElementTypesAreNonnullByDefault
public final class NullPointerTester {
private final ClassToInstanceMap<Object> defaults = MutableClassToInstanceMap.create();
@@ -324,7 +325,7 @@
}
@Override
- public boolean equals(Object obj) {
+ public boolean equals(@Nullable Object obj) {
if (obj instanceof Signature) {
Signature that = (Signature) obj;
return name.equals(that.name) && parameterTypes.equals(that.parameterTypes);
@@ -347,7 +348,7 @@
* static
*/
private void testParameter(
- Object instance, Invokable<?, ?> invokable, int paramIndex, Class<?> testedClass) {
+ @Nullable Object instance, Invokable<?, ?> invokable, int paramIndex, Class<?> testedClass) {
/*
* com.google.common is starting to rely on type-use annotations, which aren't visible under
* Android VMs. So we skip testing there.
@@ -358,7 +359,7 @@
if (isPrimitiveOrNullable(invokable.getParameters().get(paramIndex))) {
return; // there's nothing to test
}
- Object[] params = buildParamList(invokable, paramIndex);
+ @Nullable Object[] params = buildParamList(invokable, paramIndex);
try {
@SuppressWarnings("unchecked") // We'll get a runtime exception if the type is wrong.
Invokable<Object, ?> unsafe = (Invokable<Object, ?>) invokable;
@@ -394,9 +395,10 @@
}
}
- private Object[] buildParamList(Invokable<?, ?> invokable, int indexOfParamToSetToNull) {
+ private @Nullable Object[] buildParamList(
+ Invokable<?, ?> invokable, int indexOfParamToSetToNull) {
ImmutableList<Parameter> params = invokable.getParameters();
- Object[] args = new Object[params.size()];
+ @Nullable Object[] args = new Object[params.size()];
for (int i = 0; i < args.length; i++) {
Parameter param = params.get(i);
@@ -412,7 +414,7 @@
return args;
}
- private <T> T getDefaultValue(TypeToken<T> type) {
+ private <T> @Nullable T getDefaultValue(TypeToken<T> type) {
// We assume that all defaults are generics-safe, even if they aren't,
// we take the risk.
@SuppressWarnings("unchecked")
@@ -480,7 +482,7 @@
private <T> T newDefaultReturningProxy(final TypeToken<T> type) {
return new DummyProxy() {
@Override
- <R> R dummyReturnValue(TypeToken<R> returnType) {
+ <R> @Nullable R dummyReturnValue(TypeToken<R> returnType) {
return getDefaultValue(returnType);
}
}.newProxy(type);
@@ -663,6 +665,6 @@
private static boolean isAndroid() {
// Arguably it would make more sense to test "can we see type-use annotations" directly....
- return System.getProperty("java.runtime.name").contains("Android");
+ return checkNotNull(System.getProperty("java.runtime.name", "")).contains("Android");
}
}
diff --git a/android/guava-testlib/src/com/google/common/testing/Platform.java b/android/guava-testlib/src/com/google/common/testing/Platform.java
index b107966..bbad559 100644
--- a/android/guava-testlib/src/com/google/common/testing/Platform.java
+++ b/android/guava-testlib/src/com/google/common/testing/Platform.java
@@ -31,6 +31,7 @@
* @author Chris Povirk
*/
@GwtCompatible(emulated = true)
+@ElementTypesAreNonnullByDefault
final class Platform {
/** Serializes and deserializes the specified object. */
@SuppressWarnings("unchecked")
diff --git a/android/guava-testlib/src/com/google/common/testing/RelationshipTester.java b/android/guava-testlib/src/com/google/common/testing/RelationshipTester.java
index 1ffed6b..d1caedd 100644
--- a/android/guava-testlib/src/com/google/common/testing/RelationshipTester.java
+++ b/android/guava-testlib/src/com/google/common/testing/RelationshipTester.java
@@ -33,6 +33,7 @@
* @author Gregory Kick
*/
@GwtCompatible
+@ElementTypesAreNonnullByDefault
final class RelationshipTester<T> {
static class ItemReporter {
diff --git a/android/guava-testlib/src/com/google/common/testing/SerializableTester.java b/android/guava-testlib/src/com/google/common/testing/SerializableTester.java
index 72d3ea4..c2f8cc1 100644
--- a/android/guava-testlib/src/com/google/common/testing/SerializableTester.java
+++ b/android/guava-testlib/src/com/google/common/testing/SerializableTester.java
@@ -33,6 +33,7 @@
* @since 10.0
*/
@GwtCompatible // but no-op!
+@ElementTypesAreNonnullByDefault
public final class SerializableTester {
private SerializableTester() {}
diff --git a/android/guava-testlib/src/com/google/common/testing/SloppyTearDown.java b/android/guava-testlib/src/com/google/common/testing/SloppyTearDown.java
index 95ff34e..478e8b7 100644
--- a/android/guava-testlib/src/com/google/common/testing/SloppyTearDown.java
+++ b/android/guava-testlib/src/com/google/common/testing/SloppyTearDown.java
@@ -32,6 +32,7 @@
*/
@Beta
@GwtCompatible
+@ElementTypesAreNonnullByDefault
public abstract class SloppyTearDown implements TearDown {
private static final Logger logger = Logger.getLogger(SloppyTearDown.class.getName());
diff --git a/android/guava-testlib/src/com/google/common/testing/TearDown.java b/android/guava-testlib/src/com/google/common/testing/TearDown.java
index 5048534..9a90776 100644
--- a/android/guava-testlib/src/com/google/common/testing/TearDown.java
+++ b/android/guava-testlib/src/com/google/common/testing/TearDown.java
@@ -27,6 +27,7 @@
*/
@Beta
@GwtCompatible
+@ElementTypesAreNonnullByDefault
public interface TearDown {
/**
* Performs a <b>single</b> tear-down operation. See test-libraries-for-java's {@code
diff --git a/android/guava-testlib/src/com/google/common/testing/TearDownAccepter.java b/android/guava-testlib/src/com/google/common/testing/TearDownAccepter.java
index bad1f19..5213425 100644
--- a/android/guava-testlib/src/com/google/common/testing/TearDownAccepter.java
+++ b/android/guava-testlib/src/com/google/common/testing/TearDownAccepter.java
@@ -17,8 +17,8 @@
package com.google.common.testing;
import com.google.common.annotations.Beta;
-import com.google.errorprone.annotations.DoNotMock;
import com.google.common.annotations.GwtCompatible;
+import com.google.errorprone.annotations.DoNotMock;
/**
* Any object which can accept registrations of {@link TearDown} instances.
@@ -29,6 +29,7 @@
@Beta
@DoNotMock("Implement with a lambda")
@GwtCompatible
+@ElementTypesAreNonnullByDefault
public interface TearDownAccepter {
/**
* Registers a TearDown implementor which will be run after the test proper.
diff --git a/android/guava-testlib/src/com/google/common/testing/TearDownStack.java b/android/guava-testlib/src/com/google/common/testing/TearDownStack.java
index bab025a..1f7e855 100644
--- a/android/guava-testlib/src/com/google/common/testing/TearDownStack.java
+++ b/android/guava-testlib/src/com/google/common/testing/TearDownStack.java
@@ -38,6 +38,7 @@
*/
@Beta
@GwtCompatible
+@ElementTypesAreNonnullByDefault
public class TearDownStack implements TearDownAccepter {
private static final Logger logger = Logger.getLogger(TearDownStack.class.getName());
diff --git a/android/guava-testlib/src/com/google/common/testing/TestLogHandler.java b/android/guava-testlib/src/com/google/common/testing/TestLogHandler.java
index 41dca2e..35982ce 100644
--- a/android/guava-testlib/src/com/google/common/testing/TestLogHandler.java
+++ b/android/guava-testlib/src/com/google/common/testing/TestLogHandler.java
@@ -16,6 +16,8 @@
package com.google.common.testing;
+import static com.google.common.base.Preconditions.checkNotNull;
+
import com.google.common.annotations.GwtCompatible;
import java.util.ArrayList;
import java.util.Collections;
@@ -52,6 +54,7 @@
* @since 10.0
*/
@GwtCompatible
+@ElementTypesAreNonnullByDefault
public class TestLogHandler extends Handler {
/** We will keep a private list of all logged records */
private final List<LogRecord> list = new ArrayList<>();
@@ -59,7 +62,7 @@
/** Adds the most recently logged record to our list. */
@Override
public synchronized void publish(@Nullable LogRecord record) {
- list.add(record);
+ list.add(checkNotNull(record));
}
@Override
diff --git a/guava-testlib/src/com/google/common/testing/AbstractPackageSanityTests.java b/guava-testlib/src/com/google/common/testing/AbstractPackageSanityTests.java
index 176d306..26ef6f5 100644
--- a/guava-testlib/src/com/google/common/testing/AbstractPackageSanityTests.java
+++ b/guava-testlib/src/com/google/common/testing/AbstractPackageSanityTests.java
@@ -106,6 +106,7 @@
// Note: @Test annotations are deliberate, as some subclasses specify @RunWith(JUnit4).
@GwtIncompatible
@J2ObjCIncompatible // com.google.common.reflect.ClassPath
+@ElementTypesAreNonnullByDefault
public abstract class AbstractPackageSanityTests extends TestCase {
/**
diff --git a/guava-testlib/src/com/google/common/testing/ArbitraryInstances.java b/guava-testlib/src/com/google/common/testing/ArbitraryInstances.java
index 1325d7f..734199c 100644
--- a/guava-testlib/src/com/google/common/testing/ArbitraryInstances.java
+++ b/guava-testlib/src/com/google/common/testing/ArbitraryInstances.java
@@ -172,6 +172,7 @@
* @since 12.0
*/
@GwtIncompatible
+@ElementTypesAreNonnullByDefault
public final class ArbitraryInstances {
private static final Ordering<Field> BY_FIELD_NAME =
@@ -359,7 +360,7 @@
}
if (type.isEnum()) {
T[] enumConstants = type.getEnumConstants();
- return (enumConstants.length == 0) ? null : enumConstants[0];
+ return (enumConstants == null || enumConstants.length == 0) ? null : enumConstants[0];
}
if (type.isArray()) {
return createEmptyArray(type);
@@ -506,11 +507,12 @@
}
// Always equal is a valid total ordering. And it works for any Object.
- private static final class AlwaysEqual extends Ordering<Object> implements Serializable {
+ private static final class AlwaysEqual extends Ordering<@Nullable Object>
+ implements Serializable {
private static final AlwaysEqual INSTANCE = new AlwaysEqual();
@Override
- public int compare(Object o1, Object o2) {
+ public int compare(@Nullable Object o1, @Nullable Object o2) {
return 0;
}
diff --git a/guava-testlib/src/com/google/common/testing/ClassSanityTester.java b/guava-testlib/src/com/google/common/testing/ClassSanityTester.java
index ca8d1b5..0faab80 100644
--- a/guava-testlib/src/com/google/common/testing/ClassSanityTester.java
+++ b/guava-testlib/src/com/google/common/testing/ClassSanityTester.java
@@ -344,7 +344,7 @@
FactoryMethodReturnsNullException {
if (cls.isEnum()) {
T[] constants = cls.getEnumConstants();
- if (constants.length > 0) {
+ if (constants != null && constants.length > 0) {
return constants[0];
} else {
return null;
@@ -576,7 +576,7 @@
IllegalAccessException, InvocationTargetException, FactoryMethodReturnsNullException {
List<Parameter> params = factory.getParameters();
List<FreshValueGenerator> argGenerators = Lists.newArrayListWithCapacity(params.size());
- List<Object> args = Lists.newArrayListWithCapacity(params.size());
+ List<@Nullable Object> args = Lists.newArrayListWithCapacity(params.size());
for (Parameter param : params) {
FreshValueGenerator generator = newFreshValueGenerator();
argGenerators.add(generator);
@@ -834,7 +834,7 @@
}
@Override
- public boolean equals(Object obj) {
+ public boolean equals(@Nullable Object obj) {
return obj instanceof SerializableDummyProxy;
}
diff --git a/guava-testlib/src/com/google/common/testing/ClusterException.java b/guava-testlib/src/com/google/common/testing/ClusterException.java
index 30ca12e..d94103d 100644
--- a/guava-testlib/src/com/google/common/testing/ClusterException.java
+++ b/guava-testlib/src/com/google/common/testing/ClusterException.java
@@ -59,6 +59,7 @@
* @author Luiz-Otavio Zorzella
*/
@GwtCompatible
+@ElementTypesAreNonnullByDefault
final class ClusterException extends RuntimeException {
final Collection<? extends Throwable> exceptions;
diff --git a/guava-testlib/src/com/google/common/testing/CollectorTester.java b/guava-testlib/src/com/google/common/testing/CollectorTester.java
index e1dd347..b62b83e 100644
--- a/guava-testlib/src/com/google/common/testing/CollectorTester.java
+++ b/guava-testlib/src/com/google/common/testing/CollectorTester.java
@@ -47,12 +47,15 @@
* @since 21.0
*/
@GwtCompatible
-public final class CollectorTester<T, A, R> {
+@ElementTypesAreNonnullByDefault
+public final class CollectorTester<
+ T extends @Nullable Object, A extends @Nullable Object, R extends @Nullable Object> {
/**
* Creates a {@code CollectorTester} for the specified {@code Collector}. The result of the {@code
* Collector} will be compared to the expected value using {@link Object#equals}.
*/
- public static <T, A, R> CollectorTester<T, A, R> of(Collector<T, A, R> collector) {
+ public static <T extends @Nullable Object, A extends @Nullable Object, R extends @Nullable Object>
+ CollectorTester<T, A, R> of(Collector<T, A, R> collector) {
return of(collector, Objects::equals);
}
@@ -60,8 +63,9 @@
* Creates a {@code CollectorTester} for the specified {@code Collector}. The result of the {@code
* Collector} will be compared to the expected value using the specified {@code equivalence}.
*/
- public static <T, A, R> CollectorTester<T, A, R> of(
- Collector<T, A, R> collector, BiPredicate<? super R, ? super R> equivalence) {
+ public static <T extends @Nullable Object, A extends @Nullable Object, R extends @Nullable Object>
+ CollectorTester<T, A, R> of(
+ Collector<T, A, R> collector, BiPredicate<? super R, ? super R> equivalence) {
return new CollectorTester<>(collector, equivalence);
}
@@ -82,7 +86,8 @@
/** Get one accumulator and accumulate the elements into it sequentially. */
SEQUENTIAL {
@Override
- final <T, A, R> A result(Collector<T, A, R> collector, Iterable<T> inputs) {
+ final <T extends @Nullable Object, A extends @Nullable Object, R extends @Nullable Object>
+ A result(Collector<T, A, R> collector, Iterable<T> inputs) {
A accum = collector.supplier().get();
for (T input : inputs) {
collector.accumulator().accept(accum, input);
@@ -93,7 +98,8 @@
/** Get one accumulator for each element and merge the accumulators left-to-right. */
MERGE_LEFT_ASSOCIATIVE {
@Override
- final <T, A, R> A result(Collector<T, A, R> collector, Iterable<T> inputs) {
+ final <T extends @Nullable Object, A extends @Nullable Object, R extends @Nullable Object>
+ A result(Collector<T, A, R> collector, Iterable<T> inputs) {
A accum = collector.supplier().get();
for (T input : inputs) {
A newAccum = collector.supplier().get();
@@ -106,7 +112,8 @@
/** Get one accumulator for each element and merge the accumulators right-to-left. */
MERGE_RIGHT_ASSOCIATIVE {
@Override
- final <T, A, R> A result(Collector<T, A, R> collector, Iterable<T> inputs) {
+ final <T extends @Nullable Object, A extends @Nullable Object, R extends @Nullable Object>
+ A result(Collector<T, A, R> collector, Iterable<T> inputs) {
List<A> stack = new ArrayList<>();
for (T input : inputs) {
A newAccum = collector.supplier().get();
@@ -122,16 +129,17 @@
return pop(stack);
}
- <E> void push(List<E> stack, E value) {
+ <E extends @Nullable Object> void push(List<E> stack, E value) {
stack.add(value);
}
- <E> E pop(List<E> stack) {
+ <E extends @Nullable Object> E pop(List<E> stack) {
return stack.remove(stack.size() - 1);
}
};
- abstract <T, A, R> A result(Collector<T, A, R> collector, Iterable<T> inputs);
+ abstract <T extends @Nullable Object, A extends @Nullable Object, R extends @Nullable Object>
+ A result(Collector<T, A, R> collector, Iterable<T> inputs);
}
/**
@@ -140,7 +148,7 @@
*/
@SafeVarargs
@CanIgnoreReturnValue
- public final CollectorTester<T, A, R> expectCollects(@Nullable R expectedResult, T... inputs) {
+ public final CollectorTester<T, A, R> expectCollects(R expectedResult, T... inputs) {
List<T> list = Arrays.asList(inputs);
doExpectCollects(expectedResult, list);
if (collector.characteristics().contains(Collector.Characteristics.UNORDERED)) {
@@ -150,7 +158,7 @@
return this;
}
- private void doExpectCollects(@Nullable R expectedResult, List<T> inputs) {
+ private void doExpectCollects(R expectedResult, List<T> inputs) {
for (CollectStrategy scheme : EnumSet.allOf(CollectStrategy.class)) {
A finalAccum = scheme.result(collector, inputs);
if (collector.characteristics().contains(Collector.Characteristics.IDENTITY_FINISH)) {
@@ -160,7 +168,7 @@
}
}
- private void assertEquivalent(@Nullable R expected, @Nullable R actual) {
+ private void assertEquivalent(R expected, R actual) {
assertTrue(
"Expected " + expected + " got " + actual + " modulo equivalence " + equivalence,
equivalence.test(expected, actual));
diff --git a/guava-testlib/src/com/google/common/testing/DummyProxy.java b/guava-testlib/src/com/google/common/testing/DummyProxy.java
index 85e229d..ed5af92 100644
--- a/guava-testlib/src/com/google/common/testing/DummyProxy.java
+++ b/guava-testlib/src/com/google/common/testing/DummyProxy.java
@@ -30,6 +30,7 @@
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Set;
+import org.checkerframework.checker.nullness.qual.Nullable;
/**
* Generates a dummy interface proxy that simply returns a dummy value for each method.
@@ -37,6 +38,7 @@
* @author Ben Yu
*/
@GwtIncompatible
+@ElementTypesAreNonnullByDefault
abstract class DummyProxy {
/**
@@ -59,7 +61,7 @@
}
/** Returns the dummy return value for {@code returnType}. */
- abstract <R> R dummyReturnValue(TypeToken<R> returnType);
+ abstract <R> @Nullable R dummyReturnValue(TypeToken<R> returnType);
private class DummyHandler extends AbstractInvocationHandler implements Serializable {
private final TypeToken<?> interfaceType;
@@ -69,7 +71,7 @@
}
@Override
- protected Object handleInvocation(Object proxy, Method method, Object[] args) {
+ protected @Nullable Object handleInvocation(Object proxy, Method method, Object[] args) {
Invokable<?, ?> invokable = interfaceType.method(method);
ImmutableList<Parameter> params = invokable.getParameters();
for (int i = 0; i < args.length; i++) {
@@ -87,7 +89,7 @@
}
@Override
- public boolean equals(Object obj) {
+ public boolean equals(@Nullable Object obj) {
if (obj instanceof DummyHandler) {
DummyHandler that = (DummyHandler) obj;
return identity().equals(that.identity());
diff --git a/guava-testlib/src/com/google/common/testing/ElementTypesAreNonnullByDefault.java b/guava-testlib/src/com/google/common/testing/ElementTypesAreNonnullByDefault.java
new file mode 100644
index 0000000..1362176
--- /dev/null
+++ b/guava-testlib/src/com/google/common/testing/ElementTypesAreNonnullByDefault.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2021 The Guava Authors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.google.common.testing;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.PARAMETER;
+import static java.lang.annotation.ElementType.TYPE;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import com.google.common.annotations.GwtCompatible;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+import javax.annotation.Nonnull;
+import javax.annotation.meta.TypeQualifierDefault;
+
+/**
+ * Marks all "top-level" types as non-null in a way that is recognized by Kotlin. Note that this
+ * unfortunately includes type-variable usages, so we also provide {@link ParametricNullness} to
+ * "undo" it as best we can.
+ */
+@GwtCompatible
+@Retention(RUNTIME)
+@Target(TYPE)
+@TypeQualifierDefault({FIELD, METHOD, PARAMETER})
+@Nonnull
+@interface ElementTypesAreNonnullByDefault {}
diff --git a/guava-testlib/src/com/google/common/testing/EqualsTester.java b/guava-testlib/src/com/google/common/testing/EqualsTester.java
index 37472a3..d448470 100644
--- a/guava-testlib/src/com/google/common/testing/EqualsTester.java
+++ b/guava-testlib/src/com/google/common/testing/EqualsTester.java
@@ -22,11 +22,12 @@
import com.google.common.annotations.GwtCompatible;
import com.google.common.base.Equivalence;
-import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
+import java.util.ArrayList;
import java.util.List;
+import org.checkerframework.checker.nullness.qual.Nullable;
/**
* Tester for equals() and hashCode() methods of a class.
@@ -75,6 +76,7 @@
* @since 10.0
*/
@GwtCompatible
+@ElementTypesAreNonnullByDefault
public final class EqualsTester {
private static final int REPETITIONS = 3;
@@ -95,9 +97,17 @@
* equal to any other equality groups added to this tester.
*/
@CanIgnoreReturnValue
- public EqualsTester addEqualityGroup(Object... equalityGroup) {
+ public EqualsTester addEqualityGroup(@Nullable Object @Nullable ... equalityGroup) {
checkNotNull(equalityGroup);
- equalityGroups.add(ImmutableList.copyOf(equalityGroup));
+ List<Object> list = new ArrayList<>(equalityGroup.length);
+ for (int i = 0; i < equalityGroup.length; i++) {
+ Object element = equalityGroup[i];
+ if (element == null) {
+ throw new NullPointerException("at index " + i);
+ }
+ list.add(element);
+ }
+ equalityGroups.add(list);
return this;
}
diff --git a/guava-testlib/src/com/google/common/testing/EquivalenceTester.java b/guava-testlib/src/com/google/common/testing/EquivalenceTester.java
index b690729..ec34752 100644
--- a/guava-testlib/src/com/google/common/testing/EquivalenceTester.java
+++ b/guava-testlib/src/com/google/common/testing/EquivalenceTester.java
@@ -50,6 +50,7 @@
* @since 10.0
*/
@GwtCompatible
+@ElementTypesAreNonnullByDefault
public final class EquivalenceTester<T> {
private static final int REPETITIONS = 3;
diff --git a/guava-testlib/src/com/google/common/testing/FakeTicker.java b/guava-testlib/src/com/google/common/testing/FakeTicker.java
index afce1e8..a8a1c4d 100644
--- a/guava-testlib/src/com/google/common/testing/FakeTicker.java
+++ b/guava-testlib/src/com/google/common/testing/FakeTicker.java
@@ -36,6 +36,7 @@
* @author Jige Yu
* @since 10.0
*/
+@ElementTypesAreNonnullByDefault
@GwtCompatible
public class FakeTicker extends Ticker {
diff --git a/guava-testlib/src/com/google/common/testing/ForwardingWrapperTester.java b/guava-testlib/src/com/google/common/testing/ForwardingWrapperTester.java
index 9b156c6..eb2949b 100644
--- a/guava-testlib/src/com/google/common/testing/ForwardingWrapperTester.java
+++ b/guava-testlib/src/com/google/common/testing/ForwardingWrapperTester.java
@@ -35,6 +35,7 @@
import java.lang.reflect.Modifier;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
+import org.checkerframework.checker.nullness.qual.Nullable;
/**
* Tester to ensure forwarding wrapper works by delegating calls to the corresponding method with
@@ -54,6 +55,7 @@
* @since 14.0
*/
@GwtIncompatible
+@ElementTypesAreNonnullByDefault
public final class ForwardingWrapperTester {
private boolean testsEquals = false;
@@ -173,9 +175,9 @@
wrapperFunction.apply(proxy).toString());
}
- private static Object[] getParameterValues(Method method) {
+ private static @Nullable Object[] getParameterValues(Method method) {
FreshValueGenerator paramValues = new FreshValueGenerator();
- List<Object> passedArgs = Lists.newArrayList();
+ List<@Nullable Object> passedArgs = Lists.newArrayList();
for (Class<?> paramType : method.getParameterTypes()) {
passedArgs.add(paramValues.generateFresh(paramType));
}
@@ -187,8 +189,8 @@
private final Class<T> interfaceType;
private final Method method;
- private final Object[] passedArgs;
- private final Object returnValue;
+ private final @Nullable Object[] passedArgs;
+ private final @Nullable Object returnValue;
private final AtomicInteger called = new AtomicInteger();
InteractionTester(Class<T> interfaceType, Method method) {
@@ -199,8 +201,8 @@
}
@Override
- protected Object handleInvocation(Object p, Method calledMethod, Object[] args)
- throws Throwable {
+ protected @Nullable Object handleInvocation(
+ Object p, Method calledMethod, @Nullable Object[] args) throws Throwable {
assertEquals(method, calledMethod);
assertEquals(method + " invoked more than once.", 0, called.get());
for (int i = 0; i < passedArgs.length; i++) {
diff --git a/guava-testlib/src/com/google/common/testing/FreshValueGenerator.java b/guava-testlib/src/com/google/common/testing/FreshValueGenerator.java
index bb51e40..3641975 100644
--- a/guava-testlib/src/com/google/common/testing/FreshValueGenerator.java
+++ b/guava-testlib/src/com/google/common/testing/FreshValueGenerator.java
@@ -201,7 +201,7 @@
* Generates an instance for {@code type} using the current {@link #freshness}. The generated
* instance may or may not be unique across different calls.
*/
- private Object generate(TypeToken<?> type) {
+ private @Nullable Object generate(TypeToken<?> type) {
Class<?> rawType = type.getRawType();
List<Object> samples = sampleInstances.get(rawType);
Object sample = pickInstance(samples, null);
@@ -212,7 +212,7 @@
return pickInstance(rawType.getEnumConstants(), null);
}
if (type.isArray()) {
- TypeToken<?> componentType = type.getComponentType();
+ TypeToken<?> componentType = checkNotNull(type.getComponentType());
Object array = Array.newInstance(componentType.getRawType(), 1);
Array.set(array, 0, generate(componentType));
return array;
@@ -258,7 +258,7 @@
return defaultGenerate(rawType);
}
- private <T> T defaultGenerate(Class<T> rawType) {
+ private <T> @Nullable T defaultGenerate(Class<T> rawType) {
if (rawType.isInterface()) {
// always create a new proxy
return newProxy(rawType);
@@ -291,7 +291,7 @@
}
@Override
- protected Object handleInvocation(Object proxy, Method method, Object[] args) {
+ protected Object handleInvocation(Object proxy, Method method, @Nullable Object[] args) {
return interfaceMethodCalled(interfaceType, method);
}
@@ -647,7 +647,7 @@
}
@Generates
- <T> Ordering<T> generateOrdering() {
+ <T extends @Nullable Object> Ordering<T> generateOrdering() {
return new Ordering<T>() {
@Override
public int compare(T left, T right) {
@@ -930,14 +930,12 @@
}
@Generates
- static <R, C, V> Table<R, C, V> generateTable(
- @Nullable R row, @Nullable C column, @Nullable V value) {
+ static <R, C, V> Table<R, C, V> generateTable(R row, C column, V value) {
return generateHashBasedTable(row, column, value);
}
@Generates
- static <R, C, V> HashBasedTable<R, C, V> generateHashBasedTable(
- @Nullable R row, @Nullable C column, @Nullable V value) {
+ static <R, C, V> HashBasedTable<R, C, V> generateHashBasedTable(R row, C column, V value) {
HashBasedTable<R, C, V> table = HashBasedTable.create();
table.put(row, column, value);
return table;
diff --git a/guava-testlib/src/com/google/common/testing/GcFinalization.java b/guava-testlib/src/com/google/common/testing/GcFinalization.java
index da6aa22..60b2405 100644
--- a/guava-testlib/src/com/google/common/testing/GcFinalization.java
+++ b/guava-testlib/src/com/google/common/testing/GcFinalization.java
@@ -104,6 +104,7 @@
*/
@GwtIncompatible
@J2ObjCIncompatible // gc
+@ElementTypesAreNonnullByDefault
public final class GcFinalization {
private GcFinalization() {}
diff --git a/guava-testlib/src/com/google/common/testing/NullPointerTester.java b/guava-testlib/src/com/google/common/testing/NullPointerTester.java
index 87349a8..836efee 100644
--- a/guava-testlib/src/com/google/common/testing/NullPointerTester.java
+++ b/guava-testlib/src/com/google/common/testing/NullPointerTester.java
@@ -68,6 +68,7 @@
* @since 10.0
*/
@GwtIncompatible
+@ElementTypesAreNonnullByDefault
public final class NullPointerTester {
private final ClassToInstanceMap<Object> defaults = MutableClassToInstanceMap.create();
@@ -325,7 +326,7 @@
}
@Override
- public boolean equals(Object obj) {
+ public boolean equals(@Nullable Object obj) {
if (obj instanceof Signature) {
Signature that = (Signature) obj;
return name.equals(that.name) && parameterTypes.equals(that.parameterTypes);
@@ -348,7 +349,7 @@
* static
*/
private void testParameter(
- Object instance, Invokable<?, ?> invokable, int paramIndex, Class<?> testedClass) {
+ @Nullable Object instance, Invokable<?, ?> invokable, int paramIndex, Class<?> testedClass) {
/*
* com.google.common is starting to rely on type-use annotations, which aren't visible under
* Android VMs. So we skip testing there.
@@ -359,7 +360,7 @@
if (isPrimitiveOrNullable(invokable.getParameters().get(paramIndex))) {
return; // there's nothing to test
}
- Object[] params = buildParamList(invokable, paramIndex);
+ @Nullable Object[] params = buildParamList(invokable, paramIndex);
try {
@SuppressWarnings("unchecked") // We'll get a runtime exception if the type is wrong.
Invokable<Object, ?> unsafe = (Invokable<Object, ?>) invokable;
@@ -395,9 +396,10 @@
}
}
- private Object[] buildParamList(Invokable<?, ?> invokable, int indexOfParamToSetToNull) {
+ private @Nullable Object[] buildParamList(
+ Invokable<?, ?> invokable, int indexOfParamToSetToNull) {
ImmutableList<Parameter> params = invokable.getParameters();
- Object[] args = new Object[params.size()];
+ @Nullable Object[] args = new Object[params.size()];
for (int i = 0; i < args.length; i++) {
Parameter param = params.get(i);
@@ -413,7 +415,7 @@
return args;
}
- private <T> T getDefaultValue(TypeToken<T> type) {
+ private <T> @Nullable T getDefaultValue(TypeToken<T> type) {
// We assume that all defaults are generics-safe, even if they aren't,
// we take the risk.
@SuppressWarnings("unchecked")
@@ -481,7 +483,7 @@
private <T> T newDefaultReturningProxy(final TypeToken<T> type) {
return new DummyProxy() {
@Override
- <R> R dummyReturnValue(TypeToken<R> returnType) {
+ <R> @Nullable R dummyReturnValue(TypeToken<R> returnType) {
return getDefaultValue(returnType);
}
}.newProxy(type);
@@ -664,6 +666,6 @@
private static boolean isAndroid() {
// Arguably it would make more sense to test "can we see type-use annotations" directly....
- return System.getProperty("java.runtime.name").contains("Android");
+ return checkNotNull(System.getProperty("java.runtime.name", "")).contains("Android");
}
}
diff --git a/guava-testlib/src/com/google/common/testing/Platform.java b/guava-testlib/src/com/google/common/testing/Platform.java
index b107966..bbad559 100644
--- a/guava-testlib/src/com/google/common/testing/Platform.java
+++ b/guava-testlib/src/com/google/common/testing/Platform.java
@@ -31,6 +31,7 @@
* @author Chris Povirk
*/
@GwtCompatible(emulated = true)
+@ElementTypesAreNonnullByDefault
final class Platform {
/** Serializes and deserializes the specified object. */
@SuppressWarnings("unchecked")
diff --git a/guava-testlib/src/com/google/common/testing/RelationshipTester.java b/guava-testlib/src/com/google/common/testing/RelationshipTester.java
index 1ffed6b..d1caedd 100644
--- a/guava-testlib/src/com/google/common/testing/RelationshipTester.java
+++ b/guava-testlib/src/com/google/common/testing/RelationshipTester.java
@@ -33,6 +33,7 @@
* @author Gregory Kick
*/
@GwtCompatible
+@ElementTypesAreNonnullByDefault
final class RelationshipTester<T> {
static class ItemReporter {
diff --git a/guava-testlib/src/com/google/common/testing/SerializableTester.java b/guava-testlib/src/com/google/common/testing/SerializableTester.java
index 72d3ea4..c2f8cc1 100644
--- a/guava-testlib/src/com/google/common/testing/SerializableTester.java
+++ b/guava-testlib/src/com/google/common/testing/SerializableTester.java
@@ -33,6 +33,7 @@
* @since 10.0
*/
@GwtCompatible // but no-op!
+@ElementTypesAreNonnullByDefault
public final class SerializableTester {
private SerializableTester() {}
diff --git a/guava-testlib/src/com/google/common/testing/SloppyTearDown.java b/guava-testlib/src/com/google/common/testing/SloppyTearDown.java
index 95ff34e..478e8b7 100644
--- a/guava-testlib/src/com/google/common/testing/SloppyTearDown.java
+++ b/guava-testlib/src/com/google/common/testing/SloppyTearDown.java
@@ -32,6 +32,7 @@
*/
@Beta
@GwtCompatible
+@ElementTypesAreNonnullByDefault
public abstract class SloppyTearDown implements TearDown {
private static final Logger logger = Logger.getLogger(SloppyTearDown.class.getName());
diff --git a/guava-testlib/src/com/google/common/testing/TearDown.java b/guava-testlib/src/com/google/common/testing/TearDown.java
index 9582525..877e5c2 100644
--- a/guava-testlib/src/com/google/common/testing/TearDown.java
+++ b/guava-testlib/src/com/google/common/testing/TearDown.java
@@ -28,6 +28,7 @@
@Beta
@FunctionalInterface
@GwtCompatible
+@ElementTypesAreNonnullByDefault
public interface TearDown {
/**
* Performs a <b>single</b> tear-down operation. See test-libraries-for-java's {@code
diff --git a/guava-testlib/src/com/google/common/testing/TearDownAccepter.java b/guava-testlib/src/com/google/common/testing/TearDownAccepter.java
index bad1f19..5213425 100644
--- a/guava-testlib/src/com/google/common/testing/TearDownAccepter.java
+++ b/guava-testlib/src/com/google/common/testing/TearDownAccepter.java
@@ -17,8 +17,8 @@
package com.google.common.testing;
import com.google.common.annotations.Beta;
-import com.google.errorprone.annotations.DoNotMock;
import com.google.common.annotations.GwtCompatible;
+import com.google.errorprone.annotations.DoNotMock;
/**
* Any object which can accept registrations of {@link TearDown} instances.
@@ -29,6 +29,7 @@
@Beta
@DoNotMock("Implement with a lambda")
@GwtCompatible
+@ElementTypesAreNonnullByDefault
public interface TearDownAccepter {
/**
* Registers a TearDown implementor which will be run after the test proper.
diff --git a/guava-testlib/src/com/google/common/testing/TearDownStack.java b/guava-testlib/src/com/google/common/testing/TearDownStack.java
index bab025a..1f7e855 100644
--- a/guava-testlib/src/com/google/common/testing/TearDownStack.java
+++ b/guava-testlib/src/com/google/common/testing/TearDownStack.java
@@ -38,6 +38,7 @@
*/
@Beta
@GwtCompatible
+@ElementTypesAreNonnullByDefault
public class TearDownStack implements TearDownAccepter {
private static final Logger logger = Logger.getLogger(TearDownStack.class.getName());
diff --git a/guava-testlib/src/com/google/common/testing/TestLogHandler.java b/guava-testlib/src/com/google/common/testing/TestLogHandler.java
index 41dca2e..35982ce 100644
--- a/guava-testlib/src/com/google/common/testing/TestLogHandler.java
+++ b/guava-testlib/src/com/google/common/testing/TestLogHandler.java
@@ -16,6 +16,8 @@
package com.google.common.testing;
+import static com.google.common.base.Preconditions.checkNotNull;
+
import com.google.common.annotations.GwtCompatible;
import java.util.ArrayList;
import java.util.Collections;
@@ -52,6 +54,7 @@
* @since 10.0
*/
@GwtCompatible
+@ElementTypesAreNonnullByDefault
public class TestLogHandler extends Handler {
/** We will keep a private list of all logged records */
private final List<LogRecord> list = new ArrayList<>();
@@ -59,7 +62,7 @@
/** Adds the most recently logged record to our list. */
@Override
public synchronized void publish(@Nullable LogRecord record) {
- list.add(record);
+ list.add(checkNotNull(record));
}
@Override