Skip to content

Commit

Permalink
Use @NullMarked instead of our custom `@ElementTypesAreNonnullByDef…
Browse files Browse the repository at this point in the history
…ault` annotations.

This is the first step toward [using JSpecify in Guava](jspecify/jspecify#239 (comment)). At the end of that path, we'll be able to [remove our dependency on JSR-305](#2960) (and on the Checker Framework's annotations), and we'll have one less blocker to [providing a `module-info`](#2970).

`@NullMarked` allows tools like kotlinc to produce errors for code like `ImmutableList<String?>`. (Before releasing this change, I'll conduct some further testing to more fully characterize the effects, both under Kotlin 2.1 and prior.) As we make further changes, it will allow kotlinc to detect even more nullness problems. We will make these changes in a series of incremental releases so that users can pick them up gradually, as we did inside Google. In simple cases, users may wish to pick up all the changes at once instead by upgrading straight from Guava 33.4.0 (or an earlier version) to Guava 33.4.4 (or whatever the version to make the final changes ends up being).

RELNOTES=Replaced our custom `@ElementTypesAreNonnullByDefault` annotations with the JSpecify `@NullMarked` annotation.
PiperOrigin-RevId: 707134516
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Dec 18, 2024
1 parent e25ee0c commit ba2bff8
Show file tree
Hide file tree
Showing 1,786 changed files with 3,512 additions and 3,352 deletions.
4 changes: 4 additions & 0 deletions android/guava-testlib/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
unit testing - particularly to assist the tests for Guava itself.
</description>
<dependencies>
<dependency>
<groupId>org.jspecify</groupId>
<artifactId>jspecify</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.common.annotations.GwtCompatible;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.Collection;
import org.jspecify.annotations.NullMarked;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.junit.Ignore;

Expand All @@ -32,7 +33,7 @@
@Ignore("test runners must not instantiate and run this directly, only via suites we build")
// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
@SuppressWarnings("JUnit4ClassUsedInJUnit3")
@ElementTypesAreNonnullByDefault
@NullMarked
public abstract class AbstractCollectionTester<E extends @Nullable Object>
extends AbstractContainerTester<Collection<E>, E> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.jspecify.annotations.NullMarked;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.junit.Ignore;

Expand All @@ -42,7 +43,7 @@
@Ignore("test runners must not instantiate and run this directly, only via suites we build")
// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
@SuppressWarnings("JUnit4ClassUsedInJUnit3")
@ElementTypesAreNonnullByDefault
@NullMarked
public abstract class AbstractContainerTester<C, E extends @Nullable Object>
extends AbstractTester<OneSizeTestContainerGenerator<C, E>> {
protected SampleElements<E> samples;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.Stack;
import org.jspecify.annotations.NullMarked;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
Expand All @@ -45,7 +46,7 @@
* @author Chris Povirk
*/
@GwtCompatible
@ElementTypesAreNonnullByDefault
@NullMarked
abstract class AbstractIteratorTester<E extends @Nullable Object, I extends Iterator<E>> {
private Stimulus<E, ? super I>[] stimuli;
private final Iterator<E> elementsToInsert;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.ListIterator;
import java.util.Map;
import java.util.Map.Entry;
import org.jspecify.annotations.NullMarked;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.junit.Ignore;

Expand All @@ -43,7 +44,7 @@
@Ignore("test runners must not instantiate and run this directly, only via suites we build")
// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
@SuppressWarnings("JUnit4ClassUsedInJUnit3")
@ElementTypesAreNonnullByDefault
@NullMarked
public abstract class AbstractMapTester<K extends @Nullable Object, V extends @Nullable Object>
extends AbstractContainerTester<Map<K, V>, Entry<K, V>> {
protected Map<K, V> getMap() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.J2ktIncompatible;
import junit.framework.TestCase;
import org.jspecify.annotations.NullMarked;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
Expand All @@ -34,7 +35,7 @@
* @author George van den Driessche
*/
@GwtCompatible(emulated = true)
@ElementTypesAreNonnullByDefault
@NullMarked
public class AbstractTester<G> extends TestCase {
private G subjectGenerator;
private String suiteName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import org.jspecify.annotations.NullMarked;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
Expand All @@ -41,7 +42,7 @@
* @author George van den Driessche
*/
@GwtCompatible
@ElementTypesAreNonnullByDefault
@NullMarked
public final class DerivedCollectionGenerators {
public static class MapEntrySetGenerator<K extends @Nullable Object, V extends @Nullable Object>
implements TestSetGenerator<Entry<K, V>>, DerivedGenerator {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.jspecify.annotations.NullMarked;
import org.checkerframework.checker.nullness.qual.Nullable;

@GwtCompatible(emulated = true)
@ElementTypesAreNonnullByDefault
@NullMarked
public class Helpers {
// Clone of Objects.equal
static boolean equal(@Nullable Object a, @Nullable Object b) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
import static java.lang.annotation.ElementType.TYPE;

import java.lang.annotation.Target;
import org.jspecify.annotations.NullMarked;

/**
* Disables Animal Sniffer's checking of compatibility with older versions of Java/Android.
*
* <p>Each package's copy of this annotation needs to be listed in our {@code pom.xml}.
*/
@Target({METHOD, CONSTRUCTOR, TYPE})
@ElementTypesAreNonnullByDefault
@NullMarked
@interface IgnoreJRERequirement {}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.common.annotations.GwtCompatible;
import java.util.Collections;
import java.util.Iterator;
import org.jspecify.annotations.NullMarked;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
Expand Down Expand Up @@ -85,7 +86,7 @@
* @author Chris Povirk
*/
@GwtCompatible
@ElementTypesAreNonnullByDefault
@NullMarked
public abstract class IteratorTester<E extends @Nullable Object>
extends AbstractIteratorTester<E, Iterator<E>> {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import org.jspecify.annotations.NullMarked;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
Expand All @@ -36,7 +37,7 @@
* @author Chris Povirk
*/
@GwtCompatible
@ElementTypesAreNonnullByDefault
@NullMarked
public abstract class ListIteratorTester<E extends @Nullable Object>
extends AbstractIteratorTester<E, ListIterator<E>> {
protected ListIteratorTester(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.Map.Entry;
import java.util.Set;
import junit.framework.TestCase;
import org.jspecify.annotations.NullMarked;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
Expand All @@ -48,7 +49,7 @@
// check the order if so.
// TODO: Refactor to share code with SetTestBuilder etc.
@GwtCompatible
@ElementTypesAreNonnullByDefault
@NullMarked
public abstract class MapInterfaceTest<K extends @Nullable Object, V extends @Nullable Object>
extends TestCase {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Collection;
import java.util.Iterator;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.jspecify.annotations.NullMarked;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
Expand All @@ -33,7 +34,7 @@
* @author Kevin Bourrillion
*/
@GwtCompatible
@ElementTypesAreNonnullByDefault
@NullMarked
public class MinimalCollection<E extends @Nullable Object> extends AbstractCollection<E> {
// TODO: expose allow nulls parameter?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.List;
import java.util.Set;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.jspecify.annotations.NullMarked;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
Expand All @@ -34,7 +35,7 @@
* @author Regina O'Dell
*/
@GwtCompatible
@ElementTypesAreNonnullByDefault
@NullMarked
public class MinimalSet<E extends @Nullable Object> extends MinimalCollection<E> implements Set<E> {

@SuppressWarnings("unchecked") // empty Object[] as E[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.jspecify.annotations.NullMarked;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
Expand All @@ -31,7 +32,7 @@
* @author George van den Driessche
*/
@GwtCompatible
@ElementTypesAreNonnullByDefault
@NullMarked
public final class OneSizeGenerator<T, E extends @Nullable Object>
implements OneSizeTestContainerGenerator<T, E> {
private final TestContainerGenerator<T, E> generator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.common.annotations.GwtCompatible;
import com.google.common.collect.testing.features.CollectionSize;
import java.util.Collection;
import org.jspecify.annotations.NullMarked;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
Expand All @@ -32,7 +33,7 @@
* @author George van den Driessche
*/
@GwtCompatible
@ElementTypesAreNonnullByDefault
@NullMarked
public interface OneSizeTestContainerGenerator<T, E extends @Nullable Object>
extends TestSubjectGenerator<T>, TestContainerGenerator<T, E> {
TestContainerGenerator<T, E> getInnerGenerator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
import junit.framework.AssertionFailedError;
import org.jspecify.annotations.NullMarked;
import org.checkerframework.checker.nullness.qual.Nullable;

/** Replacements for JUnit's {@code assertThrows} that work under GWT/J2CL. */
@GwtCompatible(emulated = true)
@ElementTypesAreNonnullByDefault
@NullMarked
final class ReflectionFreeAssertThrows {
interface ThrowingRunnable {
void run() throws Throwable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import org.jspecify.annotations.NullMarked;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
Expand All @@ -31,7 +32,7 @@
* @author Kevin Bourrillion
*/
@GwtCompatible
@ElementTypesAreNonnullByDefault
@NullMarked
public class SampleElements<E extends @Nullable Object> implements Iterable<E> {
// TODO: rename e3, e4 => missing1, missing2
private final E e0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import org.jspecify.annotations.NullMarked;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
Expand All @@ -50,7 +51,7 @@
* @since 33.4.0 (but since 21.0 in the JRE flavor)
*/
@GwtCompatible
@ElementTypesAreNonnullByDefault
@NullMarked
@SuppressWarnings("Java7ApiChecker")
@IgnoreJRERequirement // Users will use this only if they're already using Spliterator.
public final class SpliteratorTester<E extends @Nullable Object> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.common.annotations.GwtCompatible;
import com.google.common.collect.testing.SampleElements.Chars;
import java.util.List;
import org.jspecify.annotations.NullMarked;

/**
* Generates {@code List<Character>} instances for test suites.
Expand All @@ -27,7 +28,7 @@
* @author Louis Wasserman
*/
@GwtCompatible
@ElementTypesAreNonnullByDefault
@NullMarked
public abstract class TestCharacterListGenerator implements TestListGenerator<Character> {
@Override
public SampleElements<Character> samples() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.google.common.annotations.GwtCompatible;
import java.util.Collection;
import org.jspecify.annotations.NullMarked;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
Expand All @@ -26,6 +27,6 @@
* @author Kevin Bourrillion
*/
@GwtCompatible
@ElementTypesAreNonnullByDefault
@NullMarked
public interface TestCollectionGenerator<E extends @Nullable Object>
extends TestContainerGenerator<Collection<E>, E> {}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@
import com.google.common.annotations.GwtCompatible;
import com.google.common.collect.testing.SampleElements.Colliders;
import java.util.List;
import org.jspecify.annotations.NullMarked;

/**
* A generator using sample elements whose hash codes all collide badly.
*
* @author Kevin Bourrillion
*/
@GwtCompatible
@ElementTypesAreNonnullByDefault
@NullMarked
public abstract class TestCollidingSetGenerator implements TestSetGenerator<Object> {
@Override
public SampleElements<Object> samples() {
Expand Down
Loading

0 comments on commit ba2bff8

Please sign in to comment.