Skip to content

Commit 8ebb375

Browse files
cpovirkGoogle Java Core Libraries
authored and
Google Java Core Libraries
committed
Use @NullMarked instead of our custom @ElementTypesAreNonnullByDefault annotations.
This is the next 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: 708598410
1 parent ce43463 commit 8ebb375

File tree

595 files changed

+1144
-1855
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

595 files changed

+1144
-1855
lines changed

android/guava-testlib/src/com/google/common/collect/testing/AbstractCollectionTester.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.google.common.annotations.GwtCompatible;
2020
import com.google.errorprone.annotations.CanIgnoreReturnValue;
2121
import java.util.Collection;
22+
import org.jspecify.annotations.NullMarked;
2223
import org.checkerframework.checker.nullness.qual.Nullable;
2324
import org.junit.Ignore;
2425

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

android/guava-testlib/src/com/google/common/collect/testing/AbstractContainerTester.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.ArrayList;
2828
import java.util.Collection;
2929
import java.util.List;
30+
import org.jspecify.annotations.NullMarked;
3031
import org.checkerframework.checker.nullness.qual.Nullable;
3132
import org.junit.Ignore;
3233

@@ -42,7 +43,7 @@
4243
@Ignore("test runners must not instantiate and run this directly, only via suites we build")
4344
// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
4445
@SuppressWarnings("JUnit4ClassUsedInJUnit3")
45-
@ElementTypesAreNonnullByDefault
46+
@NullMarked
4647
public abstract class AbstractContainerTester<C, E extends @Nullable Object>
4748
extends AbstractTester<OneSizeTestContainerGenerator<C, E>> {
4849
protected SampleElements<E> samples;

android/guava-testlib/src/com/google/common/collect/testing/AbstractIteratorTester.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.util.NoSuchElementException;
3535
import java.util.Set;
3636
import java.util.Stack;
37+
import org.jspecify.annotations.NullMarked;
3738
import org.checkerframework.checker.nullness.qual.Nullable;
3839

3940
/**
@@ -45,7 +46,7 @@
4546
* @author Chris Povirk
4647
*/
4748
@GwtCompatible
48-
@ElementTypesAreNonnullByDefault
49+
@NullMarked
4950
abstract class AbstractIteratorTester<E extends @Nullable Object, I extends Iterator<E>> {
5051
private Stimulus<E, ? super I>[] stimuli;
5152
private final Iterator<E> elementsToInsert;

android/guava-testlib/src/com/google/common/collect/testing/AbstractMapTester.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.ListIterator;
2727
import java.util.Map;
2828
import java.util.Map.Entry;
29+
import org.jspecify.annotations.NullMarked;
2930
import org.checkerframework.checker.nullness.qual.Nullable;
3031
import org.junit.Ignore;
3132

@@ -43,7 +44,7 @@
4344
@Ignore("test runners must not instantiate and run this directly, only via suites we build")
4445
// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
4546
@SuppressWarnings("JUnit4ClassUsedInJUnit3")
46-
@ElementTypesAreNonnullByDefault
47+
@NullMarked
4748
public abstract class AbstractMapTester<K extends @Nullable Object, V extends @Nullable Object>
4849
extends AbstractContainerTester<Map<K, V>, Entry<K, V>> {
4950
protected Map<K, V> getMap() {

android/guava-testlib/src/com/google/common/collect/testing/AbstractTester.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.google.common.annotations.GwtIncompatible;
2121
import com.google.common.annotations.J2ktIncompatible;
2222
import junit.framework.TestCase;
23+
import org.jspecify.annotations.NullMarked;
2324
import org.checkerframework.checker.nullness.qual.Nullable;
2425

2526
/**
@@ -34,7 +35,7 @@
3435
* @author George van den Driessche
3536
*/
3637
@GwtCompatible(emulated = true)
37-
@ElementTypesAreNonnullByDefault
38+
@NullMarked
3839
public class AbstractTester<G> extends TestCase {
3940
private G subjectGenerator;
4041
private String suiteName;

android/guava-testlib/src/com/google/common/collect/testing/DerivedCollectionGenerators.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.Set;
3434
import java.util.SortedMap;
3535
import java.util.SortedSet;
36+
import org.jspecify.annotations.NullMarked;
3637
import org.checkerframework.checker.nullness.qual.Nullable;
3738

3839
/**
@@ -41,7 +42,7 @@
4142
* @author George van den Driessche
4243
*/
4344
@GwtCompatible
44-
@ElementTypesAreNonnullByDefault
45+
@NullMarked
4546
public final class DerivedCollectionGenerators {
4647
public static class MapEntrySetGenerator<K extends @Nullable Object, V extends @Nullable Object>
4748
implements TestSetGenerator<Entry<K, V>>, DerivedGenerator {

android/guava-testlib/src/com/google/common/collect/testing/Helpers.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,11 @@
4444
import java.util.Map;
4545
import java.util.Map.Entry;
4646
import java.util.Set;
47+
import org.jspecify.annotations.NullMarked;
4748
import org.checkerframework.checker.nullness.qual.Nullable;
4849

4950
@GwtCompatible(emulated = true)
50-
@ElementTypesAreNonnullByDefault
51+
@NullMarked
5152
public class Helpers {
5253
// Clone of Objects.equal
5354
static boolean equal(@Nullable Object a, @Nullable Object b) {

android/guava-testlib/src/com/google/common/collect/testing/IgnoreJRERequirement.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@
1919
import static java.lang.annotation.ElementType.TYPE;
2020

2121
import java.lang.annotation.Target;
22+
import org.jspecify.annotations.NullMarked;
2223

2324
/**
2425
* Disables Animal Sniffer's checking of compatibility with older versions of Java/Android.
2526
*
2627
* <p>Each package's copy of this annotation needs to be listed in our {@code pom.xml}.
2728
*/
2829
@Target({METHOD, CONSTRUCTOR, TYPE})
29-
@ElementTypesAreNonnullByDefault
30+
@NullMarked
3031
@interface IgnoreJRERequirement {}

android/guava-testlib/src/com/google/common/collect/testing/IteratorTester.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.google.common.annotations.GwtCompatible;
2020
import java.util.Collections;
2121
import java.util.Iterator;
22+
import org.jspecify.annotations.NullMarked;
2223
import org.checkerframework.checker.nullness.qual.Nullable;
2324

2425
/**
@@ -85,7 +86,7 @@
8586
* @author Chris Povirk
8687
*/
8788
@GwtCompatible
88-
@ElementTypesAreNonnullByDefault
89+
@NullMarked
8990
public abstract class IteratorTester<E extends @Nullable Object>
9091
extends AbstractIteratorTester<E, Iterator<E>> {
9192
/**

android/guava-testlib/src/com/google/common/collect/testing/ListIteratorTester.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.ArrayList;
2121
import java.util.List;
2222
import java.util.ListIterator;
23+
import org.jspecify.annotations.NullMarked;
2324
import org.checkerframework.checker.nullness.qual.Nullable;
2425

2526
/**
@@ -36,7 +37,7 @@
3637
* @author Chris Povirk
3738
*/
3839
@GwtCompatible
39-
@ElementTypesAreNonnullByDefault
40+
@NullMarked
4041
public abstract class ListIteratorTester<E extends @Nullable Object>
4142
extends AbstractIteratorTester<E, ListIterator<E>> {
4243
protected ListIteratorTester(

android/guava-testlib/src/com/google/common/collect/testing/MapInterfaceTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.Map.Entry;
3434
import java.util.Set;
3535
import junit.framework.TestCase;
36+
import org.jspecify.annotations.NullMarked;
3637
import org.checkerframework.checker.nullness.qual.Nullable;
3738

3839
/**
@@ -48,7 +49,7 @@
4849
// check the order if so.
4950
// TODO: Refactor to share code with SetTestBuilder etc.
5051
@GwtCompatible
51-
@ElementTypesAreNonnullByDefault
52+
@NullMarked
5253
public abstract class MapInterfaceTest<K extends @Nullable Object, V extends @Nullable Object>
5354
extends TestCase {
5455

android/guava-testlib/src/com/google/common/collect/testing/MinimalCollection.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.Collection;
2525
import java.util.Iterator;
2626
import org.checkerframework.checker.nullness.qual.NonNull;
27+
import org.jspecify.annotations.NullMarked;
2728
import org.checkerframework.checker.nullness.qual.Nullable;
2829

2930
/**
@@ -33,7 +34,7 @@
3334
* @author Kevin Bourrillion
3435
*/
3536
@GwtCompatible
36-
@ElementTypesAreNonnullByDefault
37+
@NullMarked
3738
public class MinimalCollection<E extends @Nullable Object> extends AbstractCollection<E> {
3839
// TODO: expose allow nulls parameter?
3940

android/guava-testlib/src/com/google/common/collect/testing/MinimalSet.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.List;
2525
import java.util.Set;
2626
import org.checkerframework.checker.nullness.qual.NonNull;
27+
import org.jspecify.annotations.NullMarked;
2728
import org.checkerframework.checker.nullness.qual.Nullable;
2829

2930
/**
@@ -34,7 +35,7 @@
3435
* @author Regina O'Dell
3536
*/
3637
@GwtCompatible
37-
@ElementTypesAreNonnullByDefault
38+
@NullMarked
3839
public class MinimalSet<E extends @Nullable Object> extends MinimalCollection<E> implements Set<E> {
3940

4041
@SuppressWarnings("unchecked") // empty Object[] as E[]

android/guava-testlib/src/com/google/common/collect/testing/OneSizeGenerator.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.ArrayList;
2424
import java.util.Collection;
2525
import java.util.List;
26+
import org.jspecify.annotations.NullMarked;
2627
import org.checkerframework.checker.nullness.qual.Nullable;
2728

2829
/**
@@ -31,7 +32,7 @@
3132
* @author George van den Driessche
3233
*/
3334
@GwtCompatible
34-
@ElementTypesAreNonnullByDefault
35+
@NullMarked
3536
public final class OneSizeGenerator<T, E extends @Nullable Object>
3637
implements OneSizeTestContainerGenerator<T, E> {
3738
private final TestContainerGenerator<T, E> generator;

android/guava-testlib/src/com/google/common/collect/testing/OneSizeTestContainerGenerator.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.google.common.annotations.GwtCompatible;
2020
import com.google.common.collect.testing.features.CollectionSize;
2121
import java.util.Collection;
22+
import org.jspecify.annotations.NullMarked;
2223
import org.checkerframework.checker.nullness.qual.Nullable;
2324

2425
/**
@@ -32,7 +33,7 @@
3233
* @author George van den Driessche
3334
*/
3435
@GwtCompatible
35-
@ElementTypesAreNonnullByDefault
36+
@NullMarked
3637
public interface OneSizeTestContainerGenerator<T, E extends @Nullable Object>
3738
extends TestSubjectGenerator<T>, TestContainerGenerator<T, E> {
3839
TestContainerGenerator<T, E> getInnerGenerator();

android/guava-testlib/src/com/google/common/collect/testing/ReflectionFreeAssertThrows.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@
3131
import java.util.concurrent.ExecutionException;
3232
import java.util.concurrent.TimeoutException;
3333
import junit.framework.AssertionFailedError;
34+
import org.jspecify.annotations.NullMarked;
3435
import org.checkerframework.checker.nullness.qual.Nullable;
3536

3637
/** Replacements for JUnit's {@code assertThrows} that work under GWT/J2CL. */
3738
@GwtCompatible(emulated = true)
38-
@ElementTypesAreNonnullByDefault
39+
@NullMarked
3940
final class ReflectionFreeAssertThrows {
4041
interface ThrowingRunnable {
4142
void run() throws Throwable;

android/guava-testlib/src/com/google/common/collect/testing/SampleElements.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.Iterator;
2424
import java.util.List;
2525
import java.util.Map.Entry;
26+
import org.jspecify.annotations.NullMarked;
2627
import org.checkerframework.checker.nullness.qual.Nullable;
2728

2829
/**
@@ -31,7 +32,7 @@
3132
* @author Kevin Bourrillion
3233
*/
3334
@GwtCompatible
34-
@ElementTypesAreNonnullByDefault
35+
@NullMarked
3536
public class SampleElements<E extends @Nullable Object> implements Iterable<E> {
3637
// TODO: rename e3, e4 => missing1, missing2
3738
private final E e0;

android/guava-testlib/src/com/google/common/collect/testing/SpliteratorTester.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import java.util.function.Consumer;
4343
import java.util.function.Function;
4444
import java.util.function.Supplier;
45+
import org.jspecify.annotations.NullMarked;
4546
import org.checkerframework.checker.nullness.qual.Nullable;
4647

4748
/**
@@ -50,7 +51,7 @@
5051
* @since 33.4.0 (but since 21.0 in the JRE flavor)
5152
*/
5253
@GwtCompatible
53-
@ElementTypesAreNonnullByDefault
54+
@NullMarked
5455
@SuppressWarnings("Java7ApiChecker")
5556
@IgnoreJRERequirement // Users will use this only if they're already using Spliterator.
5657
public final class SpliteratorTester<E extends @Nullable Object> {

android/guava-testlib/src/com/google/common/collect/testing/TestCharacterListGenerator.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.google.common.annotations.GwtCompatible;
2020
import com.google.common.collect.testing.SampleElements.Chars;
2121
import java.util.List;
22+
import org.jspecify.annotations.NullMarked;
2223

2324
/**
2425
* Generates {@code List<Character>} instances for test suites.
@@ -27,7 +28,7 @@
2728
* @author Louis Wasserman
2829
*/
2930
@GwtCompatible
30-
@ElementTypesAreNonnullByDefault
31+
@NullMarked
3132
public abstract class TestCharacterListGenerator implements TestListGenerator<Character> {
3233
@Override
3334
public SampleElements<Character> samples() {

android/guava-testlib/src/com/google/common/collect/testing/TestCollectionGenerator.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.google.common.annotations.GwtCompatible;
2020
import java.util.Collection;
21+
import org.jspecify.annotations.NullMarked;
2122
import org.checkerframework.checker.nullness.qual.Nullable;
2223

2324
/**
@@ -26,6 +27,6 @@
2627
* @author Kevin Bourrillion
2728
*/
2829
@GwtCompatible
29-
@ElementTypesAreNonnullByDefault
30+
@NullMarked
3031
public interface TestCollectionGenerator<E extends @Nullable Object>
3132
extends TestContainerGenerator<Collection<E>, E> {}

android/guava-testlib/src/com/google/common/collect/testing/TestCollidingSetGenerator.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@
1919
import com.google.common.annotations.GwtCompatible;
2020
import com.google.common.collect.testing.SampleElements.Colliders;
2121
import java.util.List;
22+
import org.jspecify.annotations.NullMarked;
2223

2324
/**
2425
* A generator using sample elements whose hash codes all collide badly.
2526
*
2627
* @author Kevin Bourrillion
2728
*/
2829
@GwtCompatible
29-
@ElementTypesAreNonnullByDefault
30+
@NullMarked
3031
public abstract class TestCollidingSetGenerator implements TestSetGenerator<Object> {
3132
@Override
3233
public SampleElements<Object> samples() {

android/guava-testlib/src/com/google/common/collect/testing/TestContainerGenerator.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Collection;
2121
import java.util.List;
2222
import java.util.Map;
23+
import org.jspecify.annotations.NullMarked;
2324
import org.checkerframework.checker.nullness.qual.Nullable;
2425

2526
/**
@@ -30,7 +31,7 @@
3031
* @author George van den Driessche
3132
*/
3233
@GwtCompatible
33-
@ElementTypesAreNonnullByDefault
34+
@NullMarked
3435
public interface TestContainerGenerator<T, E extends @Nullable Object> {
3536
/** Returns the sample elements that this generate populates its container with. */
3637
SampleElements<E> samples();

android/guava-testlib/src/com/google/common/collect/testing/TestEnumMapGenerator.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@
2323
import java.util.List;
2424
import java.util.Map;
2525
import java.util.Map.Entry;
26+
import org.jspecify.annotations.NullMarked;
2627

2728
/**
2829
* Implementation helper for {@link TestMapGenerator} for use with enum maps.
2930
*
3031
* @author Kevin Bourrillion
3132
*/
3233
@GwtCompatible
33-
@ElementTypesAreNonnullByDefault
34+
@NullMarked
3435
public abstract class TestEnumMapGenerator implements TestMapGenerator<AnEnum, String> {
3536

3637
@Override

android/guava-testlib/src/com/google/common/collect/testing/TestEnumSetGenerator.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@
2222
import com.google.common.collect.testing.SampleElements.Enums;
2323
import java.util.List;
2424
import java.util.Set;
25+
import org.jspecify.annotations.NullMarked;
2526

2627
/**
2728
* An abstract TestSetGenerator for generating sets containing enum values.
2829
*
2930
* @author Kevin Bourrillion
3031
*/
3132
@GwtCompatible
32-
@ElementTypesAreNonnullByDefault
33+
@NullMarked
3334
public abstract class TestEnumSetGenerator implements TestSetGenerator<AnEnum> {
3435
@Override
3536
public SampleElements<AnEnum> samples() {

0 commit comments

Comments
 (0)