Skip to content

Commit

Permalink
Add some nullness annotations to tests.
Browse files Browse the repository at this point in the history
I'm not entirely sure whether these should be necessary, but adding them is the easiest way to resolve some J2KT errors that I'm seeing as I make changes to prepare for using `@NullMarked` in the open-source build.

RELNOTES=n/a
PiperOrigin-RevId: 707934342
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Dec 19, 2024
1 parent e25ee0c commit 15617f5
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Iterator;
import java.util.NoSuchElementException;
import junit.framework.TestCase;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
* Unit test for {@code AbstractIterator}.
Expand All @@ -46,7 +47,7 @@ public void testDefaultBehaviorOfNextAndHasNext() {
private int rep;

@Override
public Integer computeNext() {
public @Nullable Integer computeNext() {
switch (rep++) {
case 0:
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.HashMap;
import java.util.Map;
import junit.framework.TestCase;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
* Tests for {@link MoreObjects#toStringHelper(Object)}.
Expand Down Expand Up @@ -580,7 +581,7 @@ public void testToStringHelperWithArrays() {
String[] strings = {"hello", "world"};
int[] ints = {2, 42};
Object[] objects = {"obj"};
String[] arrayWithNull = {null};
@Nullable String[] arrayWithNull = new @Nullable String[] {null};
Object[] empty = {};
String toTest =
MoreObjects.toStringHelper("TSH")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@ public void onFailure(Throwable t) {
public void testWildcardFuture() {
SettableFuture<String> settable = SettableFuture.create();
ListenableFuture<?> f = settable;
FutureCallback<Object> callback =
new FutureCallback<Object>() {
FutureCallback<@Nullable Object> callback =
new FutureCallback<@Nullable Object>() {
@Override
public void onSuccess(Object result) {}
public void onSuccess(@Nullable Object result) {}

@Override
public void onFailure(Throwable t) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeoutException;
import org.checkerframework.checker.nullness.qual.Nullable;

/** Methods factored out so that they can be emulated differently in GWT. */
@GwtCompatible(emulated = true)
Expand Down Expand Up @@ -66,7 +67,8 @@ static void clearInterrupt() {
* Retrieves the result of a {@code Future} known to be done but uses the {@code get(long,
* TimeUnit)} overload in order to test that method.
*/
static <V> V getDoneFromTimeoutOverload(Future<V> future) throws ExecutionException {
static <V extends @Nullable Object> V getDoneFromTimeoutOverload(Future<V> future)
throws ExecutionException {
checkState(future.isDone(), "Future was expected to be done: %s", future);
try {
return getUninterruptibly(future, 0, SECONDS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeoutException;
import org.checkerframework.checker.nullness.qual.Nullable;

/** Methods factored out so that they can be emulated differently in GWT. */
final class TestPlatform {
Expand Down Expand Up @@ -55,7 +56,8 @@ static void clearInterrupt() {
// There is no thread interruption in GWT, so there's nothing to do.
}

static <V> V getDoneFromTimeoutOverload(Future<V> future) throws ExecutionException {
static <V extends @Nullable Object> V getDoneFromTimeoutOverload(Future<V> future)
throws ExecutionException {
checkState(future.isDone(), "Future was expected to be done: %s", future);
try {
return future.get(0, SECONDS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Iterator;
import java.util.NoSuchElementException;
import junit.framework.TestCase;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
* Unit test for {@code AbstractIterator}.
Expand All @@ -46,7 +47,7 @@ public void testDefaultBehaviorOfNextAndHasNext() {
private int rep;

@Override
public Integer computeNext() {
public @Nullable Integer computeNext() {
switch (rep++) {
case 0:
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.OptionalInt;
import java.util.OptionalLong;
import junit.framework.TestCase;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
* Tests for {@link MoreObjects#toStringHelper(Object)}.
Expand Down Expand Up @@ -591,7 +592,7 @@ public void testToStringHelperWithArrays() {
String[] strings = {"hello", "world"};
int[] ints = {2, 42};
Object[] objects = {"obj"};
String[] arrayWithNull = {null};
@Nullable String[] arrayWithNull = new @Nullable String[] {null};
Object[] empty = {};
String toTest =
MoreObjects.toStringHelper("TSH")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@ public void onFailure(Throwable t) {
public void testWildcardFuture() {
SettableFuture<String> settable = SettableFuture.create();
ListenableFuture<?> f = settable;
FutureCallback<Object> callback =
new FutureCallback<Object>() {
FutureCallback<@Nullable Object> callback =
new FutureCallback<@Nullable Object>() {
@Override
public void onSuccess(Object result) {}
public void onSuccess(@Nullable Object result) {}

@Override
public void onFailure(Throwable t) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeoutException;
import org.checkerframework.checker.nullness.qual.Nullable;

/** Methods factored out so that they can be emulated differently in GWT. */
@GwtCompatible(emulated = true)
Expand Down Expand Up @@ -66,7 +67,8 @@ static void clearInterrupt() {
* Retrieves the result of a {@code Future} known to be done but uses the {@code get(long,
* TimeUnit)} overload in order to test that method.
*/
static <V> V getDoneFromTimeoutOverload(Future<V> future) throws ExecutionException {
static <V extends @Nullable Object> V getDoneFromTimeoutOverload(Future<V> future)
throws ExecutionException {
checkState(future.isDone(), "Future was expected to be done: %s", future);
try {
return getUninterruptibly(future, 0, SECONDS);
Expand Down

0 comments on commit 15617f5

Please sign in to comment.