Skip to content

Commit

Permalink
Use type-use @Nullable instead of declaration @CheckForNull in th…
Browse files Browse the repository at this point in the history
…e remaining guava-android sources (mostly testing utilities).

guava-android was using `@CheckForNull` because it couldn't use type-use annotations when it targeted Java 7. Now that [it's built with `-source 8 -target 8`](#5269), we can use type-use annotations.

RELNOTES=n/a
PiperOrigin-RevId: 524912499
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Apr 17, 2023
1 parent cc92442 commit 8f0fda7
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 62 deletions.
Expand Up @@ -142,7 +142,7 @@
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.CheckForNull;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
* Supplies an arbitrary "default" instance for a wide range of types, often useful in testing
Expand Down Expand Up @@ -326,8 +326,7 @@ private static <T> void setImplementation(Class<T> type, Class<? extends T> impl
}

@SuppressWarnings("unchecked") // it's a subtype map
@CheckForNull
private static <T> Class<? extends T> getImplementation(Class<T> type) {
private static <T> @Nullable Class<? extends T> getImplementation(Class<T> type) {
return (Class<? extends T>) implementations.get(type);
}

Expand All @@ -337,8 +336,7 @@ private static <T> Class<? extends T> getImplementation(Class<T> type) {
* Returns an arbitrary instance for {@code type}, or {@code null} if no arbitrary instance can be
* determined.
*/
@CheckForNull
public static <T> T get(Class<T> type) {
public static <T> @Nullable T get(Class<T> type) {
T defaultValue = DEFAULTS.getInstance(type);
if (defaultValue != null) {
return defaultValue;
Expand Down Expand Up @@ -385,8 +383,7 @@ public static <T> T get(Class<T> type) {
}
}

@CheckForNull
private static <T> T arbitraryConstantInstanceOrNull(Class<T> type) {
private static <T> @Nullable T arbitraryConstantInstanceOrNull(Class<T> type) {
Field[] fields = type.getDeclaredFields();
Arrays.sort(fields, BY_FIELD_NAME);
for (Field field : fields) {
Expand Down
Expand Up @@ -51,9 +51,9 @@
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.checkerframework.checker.nullness.qual.Nullable;

/**
* Tester that runs automated sanity tests for any given class. A typical use case is to test static
Expand Down Expand Up @@ -337,8 +337,7 @@ void doTestEquals(Class<?> cls)
* @return The instantiated instance, or {@code null} if the class has no non-private constructor
* or factory method to be constructed.
*/
@CheckForNull
<T> T instantiate(Class<T> cls)
<T> @Nullable T instantiate(Class<T> cls)
throws ParameterNotInstantiableException,
IllegalAccessException,
InvocationTargetException,
Expand Down Expand Up @@ -388,8 +387,7 @@ <T> T instantiate(Class<T> cls)
* class, preventing its methods from being accessible.
* @throws InvocationTargetException if a static method threw exception.
*/
@CheckForNull
private <T> T instantiate(Invokable<?, ? extends T> factory)
private <T> @Nullable T instantiate(Invokable<?, ? extends T> factory)
throws ParameterNotInstantiableException, InvocationTargetException, IllegalAccessException {
return invoke(factory, getDummyArguments(factory));
}
Expand Down Expand Up @@ -674,8 +672,7 @@ Object interfaceMethodCalled(Class<?> interfaceType, Method method) {
return generator;
}

@CheckForNull
private static Object generateDummyArg(Parameter param, FreshValueGenerator generator)
private static @Nullable Object generateDummyArg(Parameter param, FreshValueGenerator generator)
throws ParameterNotInstantiableException {
if (isNullable(param)) {
return null;
Expand Down Expand Up @@ -771,8 +768,7 @@ private static <T> T createInstance(Invokable<?, ? extends T> factory, List<?> a
return instance;
}

@CheckForNull
private static <T> T invoke(Invokable<?, ? extends T> factory, List<?> args)
private static <T> @Nullable T invoke(Invokable<?, ? extends T> factory, List<?> args)
throws InvocationTargetException, IllegalAccessException {
T returnValue = factory.invoke(null, args.toArray());
if (returnValue == null) {
Expand Down
Expand Up @@ -118,7 +118,7 @@
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;
import javax.annotation.CheckForNull;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
* Generates fresh instances of types that are different from each other (if possible).
Expand Down Expand Up @@ -175,17 +175,15 @@ final <T> void addSampleInstances(Class<T> type, Iterable<? extends T> instances
* <li>null if no value can be generated.
* </ul>
*/
@CheckForNull
final Object generateFresh(TypeToken<?> type) {
final @Nullable Object generateFresh(TypeToken<?> type) {
Object generated = generate(type);
if (generated != null) {
freshness.incrementAndGet();
}
return generated;
}

@CheckForNull
final <T> T generateFresh(Class<T> type) {
final <T> @Nullable T generateFresh(Class<T> type) {
return Primitives.wrap(type).cast(generateFresh(TypeToken.of(type)));
}

Expand Down Expand Up @@ -299,7 +297,7 @@ public int hashCode() {
}

@Override
public boolean equals(@CheckForNull Object obj) {
public boolean equals(@Nullable Object obj) {
if (obj instanceof FreshInvocationHandler) {
FreshInvocationHandler that = (FreshInvocationHandler) obj;
return identity == that.identity;
Expand Down Expand Up @@ -647,29 +645,29 @@ static <C extends Comparable<?>> Range<C> generateRange(C freshElement) {
}

@Generates
static <E> Iterable<E> generateIterable(@CheckForNull E freshElement) {
static <E> Iterable<E> generateIterable(@Nullable E freshElement) {
return generateList(freshElement);
}

@Generates
static <E> Collection<E> generateCollection(@CheckForNull E freshElement) {
static <E> Collection<E> generateCollection(@Nullable E freshElement) {
return generateList(freshElement);
}

@Generates
static <E> List<E> generateList(@CheckForNull E freshElement) {
static <E> List<E> generateList(@Nullable E freshElement) {
return generateArrayList(freshElement);
}

@Generates
static <E> ArrayList<E> generateArrayList(@CheckForNull E freshElement) {
static <E> ArrayList<E> generateArrayList(@Nullable E freshElement) {
ArrayList<E> list = Lists.newArrayList();
list.add(freshElement);
return list;
}

@Generates
static <E> LinkedList<E> generateLinkedList(@CheckForNull E freshElement) {
static <E> LinkedList<E> generateLinkedList(@Nullable E freshElement) {
LinkedList<E> list = Lists.newLinkedList();
list.add(freshElement);
return list;
Expand All @@ -686,17 +684,17 @@ static <E> ImmutableCollection<E> generateImmutableCollection(E freshElement) {
}

@Generates
static <E> Set<E> generateSet(@CheckForNull E freshElement) {
static <E> Set<E> generateSet(@Nullable E freshElement) {
return generateHashSet(freshElement);
}

@Generates
static <E> HashSet<E> generateHashSet(@CheckForNull E freshElement) {
static <E> HashSet<E> generateHashSet(@Nullable E freshElement) {
return generateLinkedHashSet(freshElement);
}

@Generates
static <E> LinkedHashSet<E> generateLinkedHashSet(@CheckForNull E freshElement) {
static <E> LinkedHashSet<E> generateLinkedHashSet(@Nullable E freshElement) {
LinkedHashSet<E> set = Sets.newLinkedHashSet();
set.add(freshElement);
return set;
Expand Down Expand Up @@ -731,19 +729,19 @@ static <E extends Comparable<? super E>> ImmutableSortedSet<E> generateImmutable
}

@Generates
static <E> Multiset<E> generateMultiset(@CheckForNull E freshElement) {
static <E> Multiset<E> generateMultiset(@Nullable E freshElement) {
return generateHashMultiset(freshElement);
}

@Generates
static <E> HashMultiset<E> generateHashMultiset(@CheckForNull E freshElement) {
static <E> HashMultiset<E> generateHashMultiset(@Nullable E freshElement) {
HashMultiset<E> multiset = HashMultiset.create();
multiset.add(freshElement);
return multiset;
}

@Generates
static <E> LinkedHashMultiset<E> generateLinkedHashMultiset(@CheckForNull E freshElement) {
static <E> LinkedHashMultiset<E> generateLinkedHashMultiset(@Nullable E freshElement) {
LinkedHashMultiset<E> multiset = LinkedHashMultiset.create();
multiset.add(freshElement);
return multiset;
Expand Down Expand Up @@ -773,18 +771,17 @@ static <E extends Comparable<E>> ImmutableSortedMultiset<E> generateImmutableSor
}

@Generates
static <K, V> Map<K, V> generateMap(@CheckForNull K key, @CheckForNull V value) {
static <K, V> Map<K, V> generateMap(@Nullable K key, @Nullable V value) {
return generateHashdMap(key, value);
}

@Generates
static <K, V> HashMap<K, V> generateHashdMap(@CheckForNull K key, @CheckForNull V value) {
static <K, V> HashMap<K, V> generateHashdMap(@Nullable K key, @Nullable V value) {
return generateLinkedHashMap(key, value);
}

@Generates
static <K, V> LinkedHashMap<K, V> generateLinkedHashMap(
@CheckForNull K key, @CheckForNull V value) {
static <K, V> LinkedHashMap<K, V> generateLinkedHashMap(@Nullable K key, @Nullable V value) {
LinkedHashMap<K, V> map = Maps.newLinkedHashMap();
map.put(key, value);
return map;
Expand All @@ -809,19 +806,19 @@ static <K, V> ConcurrentMap<K, V> generateConcurrentMap(K key, V value) {

@Generates
static <K extends Comparable<? super K>, V> SortedMap<K, V> generateSortedMap(
K key, @CheckForNull V value) {
K key, @Nullable V value) {
return generateNavigableMap(key, value);
}

@Generates
static <K extends Comparable<? super K>, V> NavigableMap<K, V> generateNavigableMap(
K key, @CheckForNull V value) {
K key, @Nullable V value) {
return generateTreeMap(key, value);
}

@Generates
static <K extends Comparable<? super K>, V> TreeMap<K, V> generateTreeMap(
K key, @CheckForNull V value) {
K key, @Nullable V value) {
TreeMap<K, V> map = Maps.newTreeMap();
map.put(key, value);
return map;
Expand All @@ -834,7 +831,7 @@ static <K extends Comparable<? super K>, V> ImmutableSortedMap<K, V> generateImm
}

@Generates
static <K, V> Multimap<K, V> generateMultimap(@CheckForNull K key, @CheckForNull V value) {
static <K, V> Multimap<K, V> generateMultimap(@Nullable K key, @Nullable V value) {
return generateListMultimap(key, value);
}

Expand All @@ -844,14 +841,13 @@ static <K, V> ImmutableMultimap<K, V> generateImmutableMultimap(K key, V value)
}

@Generates
static <K, V> ListMultimap<K, V> generateListMultimap(
@CheckForNull K key, @CheckForNull V value) {
static <K, V> ListMultimap<K, V> generateListMultimap(@Nullable K key, @Nullable V value) {
return generateArrayListMultimap(key, value);
}

@Generates
static <K, V> ArrayListMultimap<K, V> generateArrayListMultimap(
@CheckForNull K key, @CheckForNull V value) {
@Nullable K key, @Nullable V value) {
ArrayListMultimap<K, V> multimap = ArrayListMultimap.create();
multimap.put(key, value);
return multimap;
Expand All @@ -863,21 +859,20 @@ static <K, V> ImmutableListMultimap<K, V> generateImmutableListMultimap(K key, V
}

@Generates
static <K, V> SetMultimap<K, V> generateSetMultimap(@CheckForNull K key, @CheckForNull V value) {
static <K, V> SetMultimap<K, V> generateSetMultimap(@Nullable K key, @Nullable V value) {
return generateLinkedHashMultimap(key, value);
}

@Generates
static <K, V> HashMultimap<K, V> generateHashMultimap(
@CheckForNull K key, @CheckForNull V value) {
static <K, V> HashMultimap<K, V> generateHashMultimap(@Nullable K key, @Nullable V value) {
HashMultimap<K, V> multimap = HashMultimap.create();
multimap.put(key, value);
return multimap;
}

@Generates
static <K, V> LinkedHashMultimap<K, V> generateLinkedHashMultimap(
@CheckForNull K key, @CheckForNull V value) {
@Nullable K key, @Nullable V value) {
LinkedHashMultimap<K, V> multimap = LinkedHashMultimap.create();
multimap.put(key, value);
return multimap;
Expand All @@ -889,12 +884,12 @@ static <K, V> ImmutableSetMultimap<K, V> generateImmutableSetMultimap(K key, V v
}

@Generates
static <K, V> BiMap<K, V> generateBimap(@CheckForNull K key, @CheckForNull V value) {
static <K, V> BiMap<K, V> generateBimap(@Nullable K key, @Nullable V value) {
return generateHashBiMap(key, value);
}

@Generates
static <K, V> HashBiMap<K, V> generateHashBiMap(@CheckForNull K key, @CheckForNull V value) {
static <K, V> HashBiMap<K, V> generateHashBiMap(@Nullable K key, @Nullable V value) {
HashBiMap<K, V> bimap = HashBiMap.create();
bimap.put(key, value);
return bimap;
Expand All @@ -907,13 +902,13 @@ static <K, V> ImmutableBiMap<K, V> generateImmutableBimap(K key, V value) {

@Generates
static <R, C, V> Table<R, C, V> generateTable(
@CheckForNull R row, @CheckForNull C column, @CheckForNull V value) {
@Nullable R row, @Nullable C column, @Nullable V value) {
return generateHashBasedTable(row, column, value);
}

@Generates
static <R, C, V> HashBasedTable<R, C, V> generateHashBasedTable(
@CheckForNull R row, @CheckForNull C column, @CheckForNull V value) {
@Nullable R row, @Nullable C column, @Nullable V value) {
HashBasedTable<R, C, V> table = HashBasedTable.create();
table.put(row, column, value);
return table;
Expand Down
Expand Up @@ -46,9 +46,9 @@
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ConcurrentMap;
import javax.annotation.CheckForNull;
import junit.framework.Assert;
import junit.framework.AssertionFailedError;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
* A test utility that verifies that your methods and constructors throw {@link
Expand Down Expand Up @@ -195,7 +195,7 @@ public void testAllPublicInstanceMethods(Object instance) {
*
* @param instance the instance to invoke {@code method} on, or null if {@code method} is static
*/
public void testMethod(@CheckForNull Object instance, Method method) {
public void testMethod(@Nullable Object instance, Method method) {
Class<?>[] types = method.getParameterTypes();
for (int nullIndex = 0; nullIndex < types.length; nullIndex++) {
testMethodParameter(instance, method, nullIndex);
Expand Down Expand Up @@ -226,8 +226,7 @@ public void testConstructor(Constructor<?> ctor) {
*
* @param instance the instance to invoke {@code method} on, or null if {@code method} is static
*/
public void testMethodParameter(
@CheckForNull final Object instance, final Method method, int paramIndex) {
public void testMethodParameter(@Nullable Object instance, Method method, int paramIndex) {
method.setAccessible(true);
testParameter(instance, invokable(instance, method), paramIndex, method.getDeclaringClass());
}
Expand Down Expand Up @@ -487,7 +486,7 @@ <R> R dummyReturnValue(TypeToken<R> returnType) {
}.newProxy(type);
}

private static Invokable<?, ?> invokable(@CheckForNull Object instance, Method method) {
private static Invokable<?, ?> invokable(@Nullable Object instance, Method method) {
if (instance == null) {
return Invokable.from(method);
} else {
Expand Down
Expand Up @@ -22,7 +22,7 @@
import java.util.List;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
import javax.annotation.CheckForNull;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
* Tests may use this to intercept messages that are logged by the code under test. Example:
Expand Down Expand Up @@ -58,7 +58,7 @@ public class TestLogHandler extends Handler {

/** Adds the most recently logged record to our list. */
@Override
public synchronized void publish(@CheckForNull LogRecord record) {
public synchronized void publish(@Nullable LogRecord record) {
list.add(record);
}

Expand Down
2 changes: 1 addition & 1 deletion android/guava/src/com/google/common/cache/LocalCache.java
Expand Up @@ -3907,7 +3907,7 @@ public V getIfPresent(Object key) {
return value;
}

@SuppressWarnings("MissingOverride") // Supermethod will not exist if we build with --release 7.
@Override
@CheckForNull
public V getOrDefault(@CheckForNull Object key, @CheckForNull V defaultValue) {
V result = get(key);
Expand Down

0 comments on commit 8f0fda7

Please sign in to comment.