Skip to content

Commit

Permalink
Migrate from @CheckForNull to Checker Framework @Nullable.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Dec 21, 2024
1 parent ae36f57 commit d997ad9
Show file tree
Hide file tree
Showing 673 changed files with 4,710 additions and 6,770 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import javax.annotation.CheckForNull;
import junit.framework.Assert;
import junit.framework.AssertionFailedError;
import org.jspecify.annotations.NullUnmarked;
Expand Down Expand Up @@ -657,8 +656,7 @@ private FreshValueGenerator newFreshValueGenerator() {
FreshValueGenerator generator =
new FreshValueGenerator() {
@Override
@CheckForNull
Object interfaceMethodCalled(Class<?> interfaceType, Method method) {
@Nullable Object interfaceMethodCalled(Class<?> interfaceType, Method method) {
return getDummyValue(TypeToken.of(interfaceType).method(method).getReturnType());
}
};
Expand Down Expand Up @@ -737,8 +735,7 @@ private List<Object> getDummyArguments(Invokable<?, ?> invokable)
return args;
}

@CheckForNull
private <T> T getDummyValue(TypeToken<T> type) {
private <T> @Nullable T getDummyValue(TypeToken<T> type) {
Class<? super T> rawType = type.getRawType();
@SuppressWarnings("unchecked") // Assume all default values are generics safe.
T defaultValue = (T) defaultValues.getInstance(rawType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;
import javax.annotation.CheckForNull;
import org.jspecify.annotations.NullUnmarked;
import org.checkerframework.checker.nullness.qual.Nullable;

Expand Down Expand Up @@ -294,8 +293,8 @@ private final class FreshInvocationHandler extends AbstractInvocationHandler {
}

@Override
@CheckForNull
protected Object handleInvocation(Object proxy, Method method, @Nullable Object[] args) {
protected @Nullable Object handleInvocation(
Object proxy, Method method, @Nullable Object[] args) {
return interfaceMethodCalled(interfaceType, method);
}

Expand All @@ -320,8 +319,7 @@ public String toString() {
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

import com.google.common.annotations.GwtCompatible;
import java.util.concurrent.Future;
import javax.annotation.CheckForNull;
import org.jspecify.annotations.NullUnmarked;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
* Classes and futures used in {@link FuturesGetCheckedTest} and {@link FuturesGetUncheckedTest}.
Expand Down Expand Up @@ -62,7 +62,7 @@ private ExceptionWithPrivateConstructor(String message, Throwable cause) {
}

public static final class ExceptionWithManyConstructorsButOnlyOneThrowable extends Exception {
@CheckForNull private Throwable antecedent;
private @Nullable Throwable antecedent;

public ExceptionWithManyConstructorsButOnlyOneThrowable(String message, String a1) {
super(message);
Expand Down
7 changes: 3 additions & 4 deletions android/guava/src/com/google/common/base/Absent.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.google.common.annotations.GwtCompatible;
import java.util.Collections;
import java.util.Set;
import javax.annotation.CheckForNull;
import org.checkerframework.checker.nullness.qual.Nullable;

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

@Override
@CheckForNull
public T orNull() {
public @Nullable T orNull() {
return null;
}

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

@Override
public boolean equals(@CheckForNull Object object) {
public boolean equals(@Nullable Object object) {
return object == this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.Iterator;
import java.util.NoSuchElementException;
import javax.annotation.CheckForNull;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
Expand All @@ -41,14 +40,12 @@ private enum State {
FAILED,
}

@CheckForNull private T next;
private @Nullable T next;

@CheckForNull
protected abstract T computeNext();
protected abstract @Nullable T computeNext();

@CanIgnoreReturnValue
@CheckForNull
protected final T endOfData() {
protected final @Nullable T endOfData() {
state = State.DONE;
return null;
}
Expand Down
4 changes: 2 additions & 2 deletions android/guava/src/com/google/common/base/CaseFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import com.google.common.annotations.GwtCompatible;
import java.io.Serializable;
import javax.annotation.CheckForNull;
import org.checkerframework.checker.nullness.qual.Nullable;

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

@Override
public boolean equals(@CheckForNull Object object) {
public boolean equals(@Nullable Object object) {
if (object instanceof StringConverter) {
StringConverter that = (StringConverter) object;
return sourceFormat.equals(that.sourceFormat) && targetFormat.equals(that.targetFormat);
Expand Down
39 changes: 15 additions & 24 deletions android/guava/src/com/google/common/base/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import com.google.j2objc.annotations.RetainedWith;
import java.io.Serializable;
import java.util.Iterator;
import javax.annotation.CheckForNull;
import org.checkerframework.checker.nullness.qual.Nullable;

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

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

/** Constructor for use by subclasses. */
protected Converter() {
Expand Down Expand Up @@ -189,13 +189,11 @@ protected Converter() {
*
* @return the converted value; is null <i>if and only if</i> {@code a} is null
*/
@CheckForNull
public final B convert(@CheckForNull A a) {
public final @Nullable B convert(@Nullable A a) {
return correctedDoForward(a);
}

@CheckForNull
B correctedDoForward(@CheckForNull A a) {
@Nullable B correctedDoForward(@Nullable A a) {
if (handleNullAutomatically) {
// TODO(kevinb): we shouldn't be checking for a null result at runtime. Assert?
return a == null ? null : checkNotNull(doForward(a));
Expand All @@ -204,8 +202,7 @@ B correctedDoForward(@CheckForNull A a) {
}
}

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

@CheckForNull
private B unsafeDoForward(@CheckForNull A a) {
private @Nullable B unsafeDoForward(@Nullable A a) {
return doForward(uncheckedCastNullableTToT(a));
}

@CheckForNull
private A unsafeDoBackward(@CheckForNull B b) {
private @Nullable A unsafeDoBackward(@Nullable B b) {
return doBackward(uncheckedCastNullableTToT(b));
}

Expand Down Expand Up @@ -335,14 +330,12 @@ protected B doBackward(A a) {
}

@Override
@CheckForNull
A correctedDoForward(@CheckForNull B b) {
@Nullable A correctedDoForward(@Nullable B b) {
return original.correctedDoBackward(b);
}

@Override
@CheckForNull
B correctedDoBackward(@CheckForNull A a) {
@Nullable B correctedDoBackward(@Nullable A a) {
return original.correctedDoForward(a);
}

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

@Override
public boolean equals(@CheckForNull Object object) {
public boolean equals(@Nullable Object object) {
if (object instanceof ReverseConverter) {
ReverseConverter<?, ?> that = (ReverseConverter<?, ?>) object;
return this.original.equals(that.original);
Expand Down Expand Up @@ -417,19 +410,17 @@ protected A doBackward(C c) {
}

@Override
@CheckForNull
C correctedDoForward(@CheckForNull A a) {
@Nullable C correctedDoForward(@Nullable A a) {
return second.correctedDoForward(first.correctedDoForward(a));
}

@Override
@CheckForNull
A correctedDoBackward(@CheckForNull C c) {
@Nullable A correctedDoBackward(@Nullable C c) {
return first.correctedDoBackward(second.correctedDoBackward(c));
}

@Override
public boolean equals(@CheckForNull Object object) {
public boolean equals(@Nullable Object object) {
if (object instanceof ConverterComposition) {
ConverterComposition<?, ?, ?> that = (ConverterComposition<?, ?, ?>) object;
return this.first.equals(that.first) && this.second.equals(that.second);
Expand Down Expand Up @@ -490,7 +481,7 @@ public final B apply(A a) {
* interchangeable.
*/
@Override
public boolean equals(@CheckForNull Object object) {
public boolean equals(@Nullable Object object) {
return super.equals(object);
}

Expand Down Expand Up @@ -539,7 +530,7 @@ protected A doBackward(B b) {
}

@Override
public boolean equals(@CheckForNull Object object) {
public boolean equals(@Nullable Object object) {
if (object instanceof FunctionBasedConverter) {
FunctionBasedConverter<?, ?> that = (FunctionBasedConverter<?, ?>) object;
return this.forwardFunction.equals(that.forwardFunction)
Expand Down
5 changes: 2 additions & 3 deletions android/guava/src/com/google/common/base/Defaults.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.J2ktIncompatible;
import javax.annotation.CheckForNull;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
* This class provides default values for all Java types, as defined by the JLS.
Expand All @@ -40,8 +40,7 @@ private Defaults() {}
* {@code void}, {@code null} is returned.
*/
@SuppressWarnings("unchecked")
@CheckForNull
public static <T> T defaultValue(Class<T> type) {
public static <T> @Nullable T defaultValue(Class<T> type) {
checkNotNull(type);
if (type.isPrimitive()) {
if (type == boolean.class) {
Expand Down
4 changes: 2 additions & 2 deletions android/guava/src/com/google/common/base/Enums.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;
import javax.annotation.CheckForNull;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
* Utility methods for working with {@link Enum} instances.
Expand Down Expand Up @@ -132,7 +132,7 @@ protected String doBackward(T enumValue) {
}

@Override
public boolean equals(@CheckForNull Object object) {
public boolean equals(@Nullable Object object) {
if (object instanceof StringConverter) {
StringConverter<?> that = (StringConverter<?>) object;
return this.enumClass.equals(that.enumClass);
Expand Down
17 changes: 8 additions & 9 deletions android/guava/src/com/google/common/base/Equivalence.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.google.common.annotations.GwtCompatible;
import com.google.errorprone.annotations.ForOverride;
import java.io.Serializable;
import javax.annotation.CheckForNull;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

Expand Down Expand Up @@ -65,7 +64,7 @@ protected Equivalence() {}
* <p>Note that all calls to {@code equivalent(x, y)} are expected to return the same result as
* long as neither {@code x} nor {@code y} is modified.
*/
public final boolean equivalent(@CheckForNull T a, @CheckForNull T b) {
public final boolean equivalent(@Nullable T a, @Nullable T b) {
if (a == b) {
return true;
}
Expand Down Expand Up @@ -99,7 +98,7 @@ public final boolean equivalent(@CheckForNull T a, @CheckForNull T b) {
* <li>{@code hash(null)} is {@code 0}.
* </ul>
*/
public final int hash(@CheckForNull T t) {
public final int hash(@Nullable T t) {
if (t == null) {
return 0;
}
Expand Down Expand Up @@ -209,7 +208,7 @@ public T get() {
* equivalence.
*/
@Override
public boolean equals(@CheckForNull Object obj) {
public boolean equals(@Nullable Object obj) {
if (obj == this) {
return true;
}
Expand Down Expand Up @@ -273,28 +272,28 @@ public String toString() {
*
* @since 10.0
*/
public final Predicate<@Nullable T> equivalentTo(@CheckForNull T target) {
public final Predicate<@Nullable T> equivalentTo(@Nullable T target) {
return new EquivalentToPredicate<>(this, target);
}

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

private final Equivalence<T> equivalence;
@CheckForNull private final T target;
private final @Nullable T target;

EquivalentToPredicate(Equivalence<T> equivalence, @CheckForNull T target) {
EquivalentToPredicate(Equivalence<T> equivalence, @Nullable T target) {
this.equivalence = checkNotNull(equivalence);
this.target = target;
}

@Override
public boolean apply(@CheckForNull T input) {
public boolean apply(@Nullable T input) {
return equivalence.equivalent(input, target);
}

@Override
public boolean equals(@CheckForNull Object obj) {
public boolean equals(@Nullable Object obj) {
if (this == obj) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import com.google.common.annotations.J2ktIncompatible;
import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
import javax.annotation.CheckForNull;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
* Phantom reference with a {@code finalizeReferent()} method which a background thread invokes
Expand All @@ -40,7 +40,7 @@ public abstract class FinalizablePhantomReference<T> extends PhantomReference<T>
* @param referent to phantom reference
* @param queue that should finalize the referent
*/
protected FinalizablePhantomReference(@CheckForNull T referent, FinalizableReferenceQueue queue) {
protected FinalizablePhantomReference(@Nullable T referent, FinalizableReferenceQueue queue) {
super(referent, queue.queue);
queue.cleanUp();
}
Expand Down
Loading

0 comments on commit d997ad9

Please sign in to comment.