Skip to content

Commit 8ebb375

Browse files
cpovirkGoogle Java Core Libraries
authored andcommitted
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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(

0 commit comments

Comments
 (0)