Skip to content

Commit d997ad9

Browse files
cpovirkGoogle Java Core Libraries
authored and
Google Java Core Libraries
committed
Migrate from @CheckForNull to Checker Framework @Nullable.
A future step will migrate from these and our already existing usages of the Checker Framework `@Nullable` to the JSpecify `@Nullable`. This is the next step toward [using JSpecify in Guava](jspecify/jspecify#239 (comment)). RELNOTES=Migrated from `@CheckForNull` to Checker Framework `@Nullable`. Most tools recognize both annotations, so we expect this to be a no-op in almost all cases. PiperOrigin-RevId: 708617636
1 parent ae36f57 commit d997ad9

File tree

673 files changed

+4710
-6770
lines changed

Some content is hidden

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

673 files changed

+4710
-6770
lines changed

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

+2-5
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
import java.util.List;
5151
import java.util.Map.Entry;
5252
import java.util.Set;
53-
import javax.annotation.CheckForNull;
5453
import junit.framework.Assert;
5554
import junit.framework.AssertionFailedError;
5655
import org.jspecify.annotations.NullUnmarked;
@@ -657,8 +656,7 @@ private FreshValueGenerator newFreshValueGenerator() {
657656
FreshValueGenerator generator =
658657
new FreshValueGenerator() {
659658
@Override
660-
@CheckForNull
661-
Object interfaceMethodCalled(Class<?> interfaceType, Method method) {
659+
@Nullable Object interfaceMethodCalled(Class<?> interfaceType, Method method) {
662660
return getDummyValue(TypeToken.of(interfaceType).method(method).getReturnType());
663661
}
664662
};
@@ -737,8 +735,7 @@ private List<Object> getDummyArguments(Invokable<?, ?> invokable)
737735
return args;
738736
}
739737

740-
@CheckForNull
741-
private <T> T getDummyValue(TypeToken<T> type) {
738+
private <T> @Nullable T getDummyValue(TypeToken<T> type) {
742739
Class<? super T> rawType = type.getRawType();
743740
@SuppressWarnings("unchecked") // Assume all default values are generics safe.
744741
T defaultValue = (T) defaultValues.getInstance(rawType);

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@
120120
import java.util.concurrent.ConcurrentMap;
121121
import java.util.concurrent.atomic.AtomicInteger;
122122
import java.util.regex.Pattern;
123-
import javax.annotation.CheckForNull;
124123
import org.jspecify.annotations.NullUnmarked;
125124
import org.checkerframework.checker.nullness.qual.Nullable;
126125

@@ -294,8 +293,8 @@ private final class FreshInvocationHandler extends AbstractInvocationHandler {
294293
}
295294

296295
@Override
297-
@CheckForNull
298-
protected Object handleInvocation(Object proxy, Method method, @Nullable Object[] args) {
296+
protected @Nullable Object handleInvocation(
297+
Object proxy, Method method, @Nullable Object[] args) {
299298
return interfaceMethodCalled(interfaceType, method);
300299
}
301300

@@ -320,8 +319,7 @@ public String toString() {
320319
}
321320

322321
/** Subclasses can override to provide different return value for proxied interface methods. */
323-
@CheckForNull
324-
Object interfaceMethodCalled(Class<?> interfaceType, Method method) {
322+
@Nullable Object interfaceMethodCalled(Class<?> interfaceType, Method method) {
325323
throw new UnsupportedOperationException();
326324
}
327325

android/guava-tests/test/com/google/common/util/concurrent/FuturesGetCheckedInputs.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
import com.google.common.annotations.GwtCompatible;
2020
import java.util.concurrent.Future;
21-
import javax.annotation.CheckForNull;
2221
import org.jspecify.annotations.NullUnmarked;
22+
import org.checkerframework.checker.nullness.qual.Nullable;
2323

2424
/**
2525
* Classes and futures used in {@link FuturesGetCheckedTest} and {@link FuturesGetUncheckedTest}.
@@ -62,7 +62,7 @@ private ExceptionWithPrivateConstructor(String message, Throwable cause) {
6262
}
6363

6464
public static final class ExceptionWithManyConstructorsButOnlyOneThrowable extends Exception {
65-
@CheckForNull private Throwable antecedent;
65+
private @Nullable Throwable antecedent;
6666

6767
public ExceptionWithManyConstructorsButOnlyOneThrowable(String message, String a1) {
6868
super(message);

android/guava/src/com/google/common/base/Absent.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import com.google.common.annotations.GwtCompatible;
2020
import java.util.Collections;
2121
import java.util.Set;
22-
import javax.annotation.CheckForNull;
22+
import org.checkerframework.checker.nullness.qual.Nullable;
2323

2424
/** Implementation of an {@link Optional} not containing a reference. */
2525
@GwtCompatible
@@ -61,8 +61,7 @@ public T or(Supplier<? extends T> supplier) {
6161
}
6262

6363
@Override
64-
@CheckForNull
65-
public T orNull() {
64+
public @Nullable T orNull() {
6665
return null;
6766
}
6867

@@ -78,7 +77,7 @@ public <V> Optional<V> transform(Function<? super T, V> function) {
7877
}
7978

8079
@Override
81-
public boolean equals(@CheckForNull Object object) {
80+
public boolean equals(@Nullable Object object) {
8281
return object == this;
8382
}
8483

android/guava/src/com/google/common/base/AbstractIterator.java

+3-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import com.google.errorprone.annotations.CanIgnoreReturnValue;
2222
import java.util.Iterator;
2323
import java.util.NoSuchElementException;
24-
import javax.annotation.CheckForNull;
2524
import org.checkerframework.checker.nullness.qual.Nullable;
2625

2726
/**
@@ -41,14 +40,12 @@ private enum State {
4140
FAILED,
4241
}
4342

44-
@CheckForNull private T next;
43+
private @Nullable T next;
4544

46-
@CheckForNull
47-
protected abstract T computeNext();
45+
protected abstract @Nullable T computeNext();
4846

4947
@CanIgnoreReturnValue
50-
@CheckForNull
51-
protected final T endOfData() {
48+
protected final @Nullable T endOfData() {
5249
state = State.DONE;
5350
return null;
5451
}

android/guava/src/com/google/common/base/CaseFormat.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
import com.google.common.annotations.GwtCompatible;
2121
import java.io.Serializable;
22-
import javax.annotation.CheckForNull;
22+
import org.checkerframework.checker.nullness.qual.Nullable;
2323

2424
/**
2525
* Utility class for converting between various ASCII case formats. Behavior is undefined for
@@ -181,7 +181,7 @@ protected String doBackward(String s) {
181181
}
182182

183183
@Override
184-
public boolean equals(@CheckForNull Object object) {
184+
public boolean equals(@Nullable Object object) {
185185
if (object instanceof StringConverter) {
186186
StringConverter that = (StringConverter) object;
187187
return sourceFormat.equals(that.sourceFormat) && targetFormat.equals(that.targetFormat);

android/guava/src/com/google/common/base/Converter.java

+15-24
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import com.google.j2objc.annotations.RetainedWith;
2626
import java.io.Serializable;
2727
import java.util.Iterator;
28-
import javax.annotation.CheckForNull;
28+
import org.checkerframework.checker.nullness.qual.Nullable;
2929

3030
/**
3131
* A function from {@code A} to {@code B} with an associated <i>reverse</i> function from {@code B}
@@ -143,7 +143,7 @@ public abstract class Converter<A, B> implements Function<A, B> {
143143
private final boolean handleNullAutomatically;
144144

145145
// We lazily cache the reverse view to avoid allocating on every call to reverse().
146-
@LazyInit @RetainedWith @CheckForNull private transient Converter<B, A> reverse;
146+
@LazyInit @RetainedWith private transient @Nullable Converter<B, A> reverse;
147147

148148
/** Constructor for use by subclasses. */
149149
protected Converter() {
@@ -189,13 +189,11 @@ protected Converter() {
189189
*
190190
* @return the converted value; is null <i>if and only if</i> {@code a} is null
191191
*/
192-
@CheckForNull
193-
public final B convert(@CheckForNull A a) {
192+
public final @Nullable B convert(@Nullable A a) {
194193
return correctedDoForward(a);
195194
}
196195

197-
@CheckForNull
198-
B correctedDoForward(@CheckForNull A a) {
196+
@Nullable B correctedDoForward(@Nullable A a) {
199197
if (handleNullAutomatically) {
200198
// TODO(kevinb): we shouldn't be checking for a null result at runtime. Assert?
201199
return a == null ? null : checkNotNull(doForward(a));
@@ -204,8 +202,7 @@ B correctedDoForward(@CheckForNull A a) {
204202
}
205203
}
206204

207-
@CheckForNull
208-
A correctedDoBackward(@CheckForNull B b) {
205+
@Nullable A correctedDoBackward(@Nullable B b) {
209206
if (handleNullAutomatically) {
210207
// TODO(kevinb): we shouldn't be checking for a null result at runtime. Assert?
211208
return b == null ? null : checkNotNull(doBackward(b));
@@ -240,13 +237,11 @@ A correctedDoBackward(@CheckForNull B b) {
240237
* LegacyConverter does violate the assumptions we make elsewhere.
241238
*/
242239

243-
@CheckForNull
244-
private B unsafeDoForward(@CheckForNull A a) {
240+
private @Nullable B unsafeDoForward(@Nullable A a) {
245241
return doForward(uncheckedCastNullableTToT(a));
246242
}
247243

248-
@CheckForNull
249-
private A unsafeDoBackward(@CheckForNull B b) {
244+
private @Nullable A unsafeDoBackward(@Nullable B b) {
250245
return doBackward(uncheckedCastNullableTToT(b));
251246
}
252247

@@ -335,14 +330,12 @@ protected B doBackward(A a) {
335330
}
336331

337332
@Override
338-
@CheckForNull
339-
A correctedDoForward(@CheckForNull B b) {
333+
@Nullable A correctedDoForward(@Nullable B b) {
340334
return original.correctedDoBackward(b);
341335
}
342336

343337
@Override
344-
@CheckForNull
345-
B correctedDoBackward(@CheckForNull A a) {
338+
@Nullable B correctedDoBackward(@Nullable A a) {
346339
return original.correctedDoForward(a);
347340
}
348341

@@ -352,7 +345,7 @@ public Converter<A, B> reverse() {
352345
}
353346

354347
@Override
355-
public boolean equals(@CheckForNull Object object) {
348+
public boolean equals(@Nullable Object object) {
356349
if (object instanceof ReverseConverter) {
357350
ReverseConverter<?, ?> that = (ReverseConverter<?, ?>) object;
358351
return this.original.equals(that.original);
@@ -417,19 +410,17 @@ protected A doBackward(C c) {
417410
}
418411

419412
@Override
420-
@CheckForNull
421-
C correctedDoForward(@CheckForNull A a) {
413+
@Nullable C correctedDoForward(@Nullable A a) {
422414
return second.correctedDoForward(first.correctedDoForward(a));
423415
}
424416

425417
@Override
426-
@CheckForNull
427-
A correctedDoBackward(@CheckForNull C c) {
418+
@Nullable A correctedDoBackward(@Nullable C c) {
428419
return first.correctedDoBackward(second.correctedDoBackward(c));
429420
}
430421

431422
@Override
432-
public boolean equals(@CheckForNull Object object) {
423+
public boolean equals(@Nullable Object object) {
433424
if (object instanceof ConverterComposition) {
434425
ConverterComposition<?, ?, ?> that = (ConverterComposition<?, ?, ?>) object;
435426
return this.first.equals(that.first) && this.second.equals(that.second);
@@ -490,7 +481,7 @@ public final B apply(A a) {
490481
* interchangeable.
491482
*/
492483
@Override
493-
public boolean equals(@CheckForNull Object object) {
484+
public boolean equals(@Nullable Object object) {
494485
return super.equals(object);
495486
}
496487

@@ -539,7 +530,7 @@ protected A doBackward(B b) {
539530
}
540531

541532
@Override
542-
public boolean equals(@CheckForNull Object object) {
533+
public boolean equals(@Nullable Object object) {
543534
if (object instanceof FunctionBasedConverter) {
544535
FunctionBasedConverter<?, ?> that = (FunctionBasedConverter<?, ?>) object;
545536
return this.forwardFunction.equals(that.forwardFunction)

android/guava/src/com/google/common/base/Defaults.java

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

1919
import com.google.common.annotations.GwtIncompatible;
2020
import com.google.common.annotations.J2ktIncompatible;
21-
import javax.annotation.CheckForNull;
21+
import org.checkerframework.checker.nullness.qual.Nullable;
2222

2323
/**
2424
* This class provides default values for all Java types, as defined by the JLS.
@@ -40,8 +40,7 @@ private Defaults() {}
4040
* {@code void}, {@code null} is returned.
4141
*/
4242
@SuppressWarnings("unchecked")
43-
@CheckForNull
44-
public static <T> T defaultValue(Class<T> type) {
43+
public static <T> @Nullable T defaultValue(Class<T> type) {
4544
checkNotNull(type);
4645
if (type.isPrimitive()) {
4746
if (type == boolean.class) {

android/guava/src/com/google/common/base/Enums.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import java.util.HashMap;
2626
import java.util.Map;
2727
import java.util.WeakHashMap;
28-
import javax.annotation.CheckForNull;
28+
import org.checkerframework.checker.nullness.qual.Nullable;
2929

3030
/**
3131
* Utility methods for working with {@link Enum} instances.
@@ -132,7 +132,7 @@ protected String doBackward(T enumValue) {
132132
}
133133

134134
@Override
135-
public boolean equals(@CheckForNull Object object) {
135+
public boolean equals(@Nullable Object object) {
136136
if (object instanceof StringConverter) {
137137
StringConverter<?> that = (StringConverter<?>) object;
138138
return this.enumClass.equals(that.enumClass);

android/guava/src/com/google/common/base/Equivalence.java

+8-9
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import com.google.common.annotations.GwtCompatible;
2020
import com.google.errorprone.annotations.ForOverride;
2121
import java.io.Serializable;
22-
import javax.annotation.CheckForNull;
2322
import org.checkerframework.checker.nullness.qual.NonNull;
2423
import org.checkerframework.checker.nullness.qual.Nullable;
2524

@@ -65,7 +64,7 @@ protected Equivalence() {}
6564
* <p>Note that all calls to {@code equivalent(x, y)} are expected to return the same result as
6665
* long as neither {@code x} nor {@code y} is modified.
6766
*/
68-
public final boolean equivalent(@CheckForNull T a, @CheckForNull T b) {
67+
public final boolean equivalent(@Nullable T a, @Nullable T b) {
6968
if (a == b) {
7069
return true;
7170
}
@@ -99,7 +98,7 @@ public final boolean equivalent(@CheckForNull T a, @CheckForNull T b) {
9998
* <li>{@code hash(null)} is {@code 0}.
10099
* </ul>
101100
*/
102-
public final int hash(@CheckForNull T t) {
101+
public final int hash(@Nullable T t) {
103102
if (t == null) {
104103
return 0;
105104
}
@@ -209,7 +208,7 @@ public T get() {
209208
* equivalence.
210209
*/
211210
@Override
212-
public boolean equals(@CheckForNull Object obj) {
211+
public boolean equals(@Nullable Object obj) {
213212
if (obj == this) {
214213
return true;
215214
}
@@ -273,28 +272,28 @@ public String toString() {
273272
*
274273
* @since 10.0
275274
*/
276-
public final Predicate<@Nullable T> equivalentTo(@CheckForNull T target) {
275+
public final Predicate<@Nullable T> equivalentTo(@Nullable T target) {
277276
return new EquivalentToPredicate<>(this, target);
278277
}
279278

280279
private static final class EquivalentToPredicate<T>
281280
implements Predicate<@Nullable T>, Serializable {
282281

283282
private final Equivalence<T> equivalence;
284-
@CheckForNull private final T target;
283+
private final @Nullable T target;
285284

286-
EquivalentToPredicate(Equivalence<T> equivalence, @CheckForNull T target) {
285+
EquivalentToPredicate(Equivalence<T> equivalence, @Nullable T target) {
287286
this.equivalence = checkNotNull(equivalence);
288287
this.target = target;
289288
}
290289

291290
@Override
292-
public boolean apply(@CheckForNull T input) {
291+
public boolean apply(@Nullable T input) {
293292
return equivalence.equivalent(input, target);
294293
}
295294

296295
@Override
297-
public boolean equals(@CheckForNull Object obj) {
296+
public boolean equals(@Nullable Object obj) {
298297
if (this == obj) {
299298
return true;
300299
}

android/guava/src/com/google/common/base/FinalizablePhantomReference.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import com.google.common.annotations.J2ktIncompatible;
1919
import java.lang.ref.PhantomReference;
2020
import java.lang.ref.ReferenceQueue;
21-
import javax.annotation.CheckForNull;
21+
import org.checkerframework.checker.nullness.qual.Nullable;
2222

2323
/**
2424
* Phantom reference with a {@code finalizeReferent()} method which a background thread invokes
@@ -40,7 +40,7 @@ public abstract class FinalizablePhantomReference<T> extends PhantomReference<T>
4040
* @param referent to phantom reference
4141
* @param queue that should finalize the referent
4242
*/
43-
protected FinalizablePhantomReference(@CheckForNull T referent, FinalizableReferenceQueue queue) {
43+
protected FinalizablePhantomReference(@Nullable T referent, FinalizableReferenceQueue queue) {
4444
super(referent, queue.queue);
4545
queue.cleanUp();
4646
}

0 commit comments

Comments
 (0)