From 73002525370eb62798d8b52955bb8cdb1fe787ea Mon Sep 17 00:00:00 2001 From: cpovirk Date: Thu, 6 Apr 2023 05:19:57 -0700 Subject: [PATCH] Use type-use `@Nullable` instead of declaration `@CheckForNull` in the 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`](https://github.com/google/guava/issues/5269), we can use type-use annotations. RELNOTES=n/a PiperOrigin-RevId: 522310996 --- .../common/testing/ArbitraryInstances.java | 11 ++- .../common/testing/ClassSanityTester.java | 14 ++-- .../common/testing/FreshValueGenerator.java | 67 +++++++++---------- .../common/testing/NullPointerTester.java | 9 ++- .../google/common/testing/TestLogHandler.java | 4 +- .../com/google/common/cache/LocalCache.java | 2 +- .../com/google/common/cache/LocalCache.java | 2 - 7 files changed, 47 insertions(+), 62 deletions(-) diff --git a/android/guava-testlib/src/com/google/common/testing/ArbitraryInstances.java b/android/guava-testlib/src/com/google/common/testing/ArbitraryInstances.java index 26f4df4749af..0f66184f6ee4 100644 --- a/android/guava-testlib/src/com/google/common/testing/ArbitraryInstances.java +++ b/android/guava-testlib/src/com/google/common/testing/ArbitraryInstances.java @@ -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 @@ -326,8 +326,7 @@ private static void setImplementation(Class type, Class impl } @SuppressWarnings("unchecked") // it's a subtype map - @CheckForNull - private static Class getImplementation(Class type) { + private static @Nullable Class getImplementation(Class type) { return (Class) implementations.get(type); } @@ -337,8 +336,7 @@ private static Class getImplementation(Class type) { * Returns an arbitrary instance for {@code type}, or {@code null} if no arbitrary instance can be * determined. */ - @CheckForNull - public static T get(Class type) { + public static @Nullable T get(Class type) { T defaultValue = DEFAULTS.getInstance(type); if (defaultValue != null) { return defaultValue; @@ -385,8 +383,7 @@ public static T get(Class type) { } } - @CheckForNull - private static T arbitraryConstantInstanceOrNull(Class type) { + private static @Nullable T arbitraryConstantInstanceOrNull(Class type) { Field[] fields = type.getDeclaredFields(); Arrays.sort(fields, BY_FIELD_NAME); for (Field field : fields) { diff --git a/android/guava-testlib/src/com/google/common/testing/ClassSanityTester.java b/android/guava-testlib/src/com/google/common/testing/ClassSanityTester.java index 3f59d4d6a3bd..ca8d1b537bdf 100644 --- a/android/guava-testlib/src/com/google/common/testing/ClassSanityTester.java +++ b/android/guava-testlib/src/com/google/common/testing/ClassSanityTester.java @@ -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 @@ -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 instantiate(Class cls) + @Nullable T instantiate(Class cls) throws ParameterNotInstantiableException, IllegalAccessException, InvocationTargetException, @@ -388,8 +387,7 @@ T instantiate(Class cls) * class, preventing its methods from being accessible. * @throws InvocationTargetException if a static method threw exception. */ - @CheckForNull - private T instantiate(Invokable factory) + private @Nullable T instantiate(Invokable factory) throws ParameterNotInstantiableException, InvocationTargetException, IllegalAccessException { return invoke(factory, getDummyArguments(factory)); } @@ -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; @@ -771,8 +768,7 @@ private static T createInstance(Invokable factory, List a return instance; } - @CheckForNull - private static T invoke(Invokable factory, List args) + private static @Nullable T invoke(Invokable factory, List args) throws InvocationTargetException, IllegalAccessException { T returnValue = factory.invoke(null, args.toArray()); if (returnValue == null) { diff --git a/android/guava-testlib/src/com/google/common/testing/FreshValueGenerator.java b/android/guava-testlib/src/com/google/common/testing/FreshValueGenerator.java index 259f50c45214..f2ac3598ca65 100644 --- a/android/guava-testlib/src/com/google/common/testing/FreshValueGenerator.java +++ b/android/guava-testlib/src/com/google/common/testing/FreshValueGenerator.java @@ -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). @@ -175,8 +175,7 @@ final void addSampleInstances(Class type, Iterable instances *
  • null if no value can be generated. * */ - @CheckForNull - final Object generateFresh(TypeToken type) { + final @Nullable Object generateFresh(TypeToken type) { Object generated = generate(type); if (generated != null) { freshness.incrementAndGet(); @@ -184,8 +183,7 @@ final Object generateFresh(TypeToken type) { return generated; } - @CheckForNull - final T generateFresh(Class type) { + final @Nullable T generateFresh(Class type) { return Primitives.wrap(type).cast(generateFresh(TypeToken.of(type))); } @@ -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; @@ -647,29 +645,29 @@ static > Range generateRange(C freshElement) { } @Generates - static Iterable generateIterable(@CheckForNull E freshElement) { + static Iterable generateIterable(@Nullable E freshElement) { return generateList(freshElement); } @Generates - static Collection generateCollection(@CheckForNull E freshElement) { + static Collection generateCollection(@Nullable E freshElement) { return generateList(freshElement); } @Generates - static List generateList(@CheckForNull E freshElement) { + static List generateList(@Nullable E freshElement) { return generateArrayList(freshElement); } @Generates - static ArrayList generateArrayList(@CheckForNull E freshElement) { + static ArrayList generateArrayList(@Nullable E freshElement) { ArrayList list = Lists.newArrayList(); list.add(freshElement); return list; } @Generates - static LinkedList generateLinkedList(@CheckForNull E freshElement) { + static LinkedList generateLinkedList(@Nullable E freshElement) { LinkedList list = Lists.newLinkedList(); list.add(freshElement); return list; @@ -686,17 +684,17 @@ static ImmutableCollection generateImmutableCollection(E freshElement) { } @Generates - static Set generateSet(@CheckForNull E freshElement) { + static Set generateSet(@Nullable E freshElement) { return generateHashSet(freshElement); } @Generates - static HashSet generateHashSet(@CheckForNull E freshElement) { + static HashSet generateHashSet(@Nullable E freshElement) { return generateLinkedHashSet(freshElement); } @Generates - static LinkedHashSet generateLinkedHashSet(@CheckForNull E freshElement) { + static LinkedHashSet generateLinkedHashSet(@Nullable E freshElement) { LinkedHashSet set = Sets.newLinkedHashSet(); set.add(freshElement); return set; @@ -731,19 +729,19 @@ static > ImmutableSortedSet generateImmutable } @Generates - static Multiset generateMultiset(@CheckForNull E freshElement) { + static Multiset generateMultiset(@Nullable E freshElement) { return generateHashMultiset(freshElement); } @Generates - static HashMultiset generateHashMultiset(@CheckForNull E freshElement) { + static HashMultiset generateHashMultiset(@Nullable E freshElement) { HashMultiset multiset = HashMultiset.create(); multiset.add(freshElement); return multiset; } @Generates - static LinkedHashMultiset generateLinkedHashMultiset(@CheckForNull E freshElement) { + static LinkedHashMultiset generateLinkedHashMultiset(@Nullable E freshElement) { LinkedHashMultiset multiset = LinkedHashMultiset.create(); multiset.add(freshElement); return multiset; @@ -773,18 +771,17 @@ static > ImmutableSortedMultiset generateImmutableSor } @Generates - static Map generateMap(@CheckForNull K key, @CheckForNull V value) { + static Map generateMap(@Nullable K key, @Nullable V value) { return generateHashdMap(key, value); } @Generates - static HashMap generateHashdMap(@CheckForNull K key, @CheckForNull V value) { + static HashMap generateHashdMap(@Nullable K key, @Nullable V value) { return generateLinkedHashMap(key, value); } @Generates - static LinkedHashMap generateLinkedHashMap( - @CheckForNull K key, @CheckForNull V value) { + static LinkedHashMap generateLinkedHashMap(@Nullable K key, @Nullable V value) { LinkedHashMap map = Maps.newLinkedHashMap(); map.put(key, value); return map; @@ -809,19 +806,19 @@ static ConcurrentMap generateConcurrentMap(K key, V value) { @Generates static , V> SortedMap generateSortedMap( - K key, @CheckForNull V value) { + K key, @Nullable V value) { return generateNavigableMap(key, value); } @Generates static , V> NavigableMap generateNavigableMap( - K key, @CheckForNull V value) { + K key, @Nullable V value) { return generateTreeMap(key, value); } @Generates static , V> TreeMap generateTreeMap( - K key, @CheckForNull V value) { + K key, @Nullable V value) { TreeMap map = Maps.newTreeMap(); map.put(key, value); return map; @@ -834,7 +831,7 @@ static , V> ImmutableSortedMap generateImm } @Generates - static Multimap generateMultimap(@CheckForNull K key, @CheckForNull V value) { + static Multimap generateMultimap(@Nullable K key, @Nullable V value) { return generateListMultimap(key, value); } @@ -844,14 +841,13 @@ static ImmutableMultimap generateImmutableMultimap(K key, V value) } @Generates - static ListMultimap generateListMultimap( - @CheckForNull K key, @CheckForNull V value) { + static ListMultimap generateListMultimap(@Nullable K key, @Nullable V value) { return generateArrayListMultimap(key, value); } @Generates static ArrayListMultimap generateArrayListMultimap( - @CheckForNull K key, @CheckForNull V value) { + @Nullable K key, @Nullable V value) { ArrayListMultimap multimap = ArrayListMultimap.create(); multimap.put(key, value); return multimap; @@ -863,13 +859,12 @@ static ImmutableListMultimap generateImmutableListMultimap(K key, V } @Generates - static SetMultimap generateSetMultimap(@CheckForNull K key, @CheckForNull V value) { + static SetMultimap generateSetMultimap(@Nullable K key, @Nullable V value) { return generateLinkedHashMultimap(key, value); } @Generates - static HashMultimap generateHashMultimap( - @CheckForNull K key, @CheckForNull V value) { + static HashMultimap generateHashMultimap(@Nullable K key, @Nullable V value) { HashMultimap multimap = HashMultimap.create(); multimap.put(key, value); return multimap; @@ -877,7 +872,7 @@ static HashMultimap generateHashMultimap( @Generates static LinkedHashMultimap generateLinkedHashMultimap( - @CheckForNull K key, @CheckForNull V value) { + @Nullable K key, @Nullable V value) { LinkedHashMultimap multimap = LinkedHashMultimap.create(); multimap.put(key, value); return multimap; @@ -889,12 +884,12 @@ static ImmutableSetMultimap generateImmutableSetMultimap(K key, V v } @Generates - static BiMap generateBimap(@CheckForNull K key, @CheckForNull V value) { + static BiMap generateBimap(@Nullable K key, @Nullable V value) { return generateHashBiMap(key, value); } @Generates - static HashBiMap generateHashBiMap(@CheckForNull K key, @CheckForNull V value) { + static HashBiMap generateHashBiMap(@Nullable K key, @Nullable V value) { HashBiMap bimap = HashBiMap.create(); bimap.put(key, value); return bimap; @@ -907,13 +902,13 @@ static ImmutableBiMap generateImmutableBimap(K key, V value) { @Generates static Table 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 HashBasedTable generateHashBasedTable( - @CheckForNull R row, @CheckForNull C column, @CheckForNull V value) { + @Nullable R row, @Nullable C column, @Nullable V value) { HashBasedTable table = HashBasedTable.create(); table.put(row, column, value); return table; diff --git a/android/guava-testlib/src/com/google/common/testing/NullPointerTester.java b/android/guava-testlib/src/com/google/common/testing/NullPointerTester.java index 413ba66cedeb..96bcb306e9b9 100644 --- a/android/guava-testlib/src/com/google/common/testing/NullPointerTester.java +++ b/android/guava-testlib/src/com/google/common/testing/NullPointerTester.java @@ -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 @@ -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); @@ -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()); } @@ -487,7 +486,7 @@ R dummyReturnValue(TypeToken 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 { diff --git a/android/guava-testlib/src/com/google/common/testing/TestLogHandler.java b/android/guava-testlib/src/com/google/common/testing/TestLogHandler.java index 263f8908ac36..41dca2ec8b8d 100644 --- a/android/guava-testlib/src/com/google/common/testing/TestLogHandler.java +++ b/android/guava-testlib/src/com/google/common/testing/TestLogHandler.java @@ -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: @@ -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); } diff --git a/android/guava/src/com/google/common/cache/LocalCache.java b/android/guava/src/com/google/common/cache/LocalCache.java index 7b1a06625bc3..74df5a23435c 100644 --- a/android/guava/src/com/google/common/cache/LocalCache.java +++ b/android/guava/src/com/google/common/cache/LocalCache.java @@ -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); diff --git a/guava/src/com/google/common/cache/LocalCache.java b/guava/src/com/google/common/cache/LocalCache.java index b256234be330..a0d58280d6df 100644 --- a/guava/src/com/google/common/cache/LocalCache.java +++ b/guava/src/com/google/common/cache/LocalCache.java @@ -4023,8 +4023,6 @@ public V getIfPresent(Object key) { return value; } - // Only becomes available in Java 8 when it's on the interface. - // @Override @Override @CheckForNull public V getOrDefault(@CheckForNull Object key, @CheckForNull V defaultValue) {