Skip to content

Commit

Permalink
Migrate from @NullableDecl to @CheckForNull in the backport.
Browse files Browse the repository at this point in the history
We do so for a couple reasons:

- We've gotten [requests to avoid it](google/truth#704) (and checker-compat-qual in general) before.
- The Checker Framework defines it to have a different meaning than other nullness declaration annotations (as discussed in [this documentation's exception for the `org.checkerframework` package](https://checkerframework.org/manual/#faq-declaration-annotations-moved)), and I would guess that no other tools handle it accordingly.

We mostly aren't using `@NullableDecl` nowadays, since we've migrated to jsr305 and checker-qual annotations. But some annotations remain, mostly in tests and testlib.

RELNOTES=n/a
PiperOrigin-RevId: 399231620
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Sep 28, 2021
1 parent 17e403c commit 4318d28
Show file tree
Hide file tree
Showing 41 changed files with 337 additions and 337 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.checkerframework.checker.nullness.compatqual.NullableDecl;
import javax.annotation.CheckForNull;

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

@SuppressWarnings("unchecked") // it's a subtype map
@NullableDecl
@CheckForNull
private static <T> Class<? extends T> getImplementation(Class<T> type) {
return (Class<? extends T>) implementations.get(type);
}
Expand All @@ -338,7 +338,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.
*/
@NullableDecl
@CheckForNull
public static <T> T get(Class<T> type) {
T defaultValue = DEFAULTS.getInstance(type);
if (defaultValue != null) {
Expand Down Expand Up @@ -386,7 +386,7 @@ public static <T> T get(Class<T> type) {
}
}

@NullableDecl
@CheckForNull
private static <T> T arbitraryConstantInstanceOrNull(Class<T> type) {
Field[] fields = type.getDeclaredFields();
Arrays.sort(fields, BY_FIELD_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,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.compatqual.NullableDecl;

/**
* Tester that runs automated sanity tests for any given class. A typical use case is to test static
Expand Down Expand Up @@ -334,7 +334,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.
*/
@NullableDecl
@CheckForNull
<T> T instantiate(Class<T> cls)
throws ParameterNotInstantiableException, IllegalAccessException, InvocationTargetException,
FactoryMethodReturnsNullException {
Expand Down Expand Up @@ -383,7 +383,7 @@ <T> T instantiate(Class<T> cls)
* class, preventing its methods from being accessible.
* @throws InvocationTargetException if a static method threw exception.
*/
@NullableDecl
@CheckForNull
private <T> T instantiate(Invokable<?, ? extends T> factory)
throws ParameterNotInstantiableException, InvocationTargetException, IllegalAccessException {
return invoke(factory, getDummyArguments(factory));
Expand Down Expand Up @@ -664,7 +664,7 @@ Object interfaceMethodCalled(Class<?> interfaceType, Method method) {
return generator;
}

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

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

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

@NullableDecl
@CheckForNull
final <T> T generateFresh(Class<T> type) {
return Primitives.wrap(type).cast(generateFresh(TypeToken.of(type)));
}
Expand Down Expand Up @@ -299,7 +299,7 @@ public int hashCode() {
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

@Generates
static <K, V> LinkedHashMap<K, V> generateLinkedHashMap(
@NullableDecl K key, @NullableDecl V value) {
@CheckForNull K key, @CheckForNull V value) {
LinkedHashMap<K, V> map = Maps.newLinkedHashMap();
map.put(key, value);
return map;
Expand All @@ -809,19 +809,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, @NullableDecl V value) {
K key, @CheckForNull V value) {
return generateNavigableMap(key, value);
}

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

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

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

Expand All @@ -845,13 +845,13 @@ static <K, V> ImmutableMultimap<K, V> generateImmutableMultimap(K key, V value)

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

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

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

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

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

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

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

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

@Generates
static <R, C, V> HashBasedTable<R, C, V> generateHashBasedTable(
@NullableDecl R row, @NullableDecl C column, @NullableDecl V value) {
@CheckForNull R row, @CheckForNull C column, @CheckForNull V value) {
HashBasedTable<R, C, V> table = HashBasedTable.create();
table.put(row, column, value);
return table;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,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.compatqual.NullableDecl;

/**
* A test utility that verifies that your methods and constructors throw {@link
Expand Down Expand Up @@ -176,7 +176,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(@NullableDecl Object instance, Method method) {
public void testMethod(@CheckForNull Object instance, Method method) {
Class<?>[] types = method.getParameterTypes();
for (int nullIndex = 0; nullIndex < types.length; nullIndex++) {
testMethodParameter(instance, method, nullIndex);
Expand Down Expand Up @@ -208,7 +208,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(
@NullableDecl final Object instance, final Method method, int paramIndex) {
@CheckForNull final Object instance, final Method method, int paramIndex) {
method.setAccessible(true);
testParameter(instance, invokable(instance, method), paramIndex, method.getDeclaringClass());
}
Expand Down Expand Up @@ -461,7 +461,7 @@ <R> R dummyReturnValue(TypeToken<R> returnType) {
}.newProxy(type);
}

private static Invokable<?, ?> invokable(@NullableDecl Object instance, Method method) {
private static Invokable<?, ?> invokable(@CheckForNull Object instance, Method method) {
if (instance == null) {
return Invokable.from(method);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import java.util.List;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
import org.checkerframework.checker.nullness.compatqual.NullableDecl;
import javax.annotation.CheckForNull;

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

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

Expand Down
Loading

0 comments on commit 4318d28

Please sign in to comment.