Skip to content

Commit

Permalink
Make our nullness checking work with an Android bootclasspath.
Browse files Browse the repository at this point in the history
This involves accommodating a bug and a shortcoming in our checker and a bug and a design decision in Android's nullness annotations.

RELNOTES=n/a
PiperOrigin-RevId: 591312563
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Dec 15, 2023
1 parent 747924e commit b90e3be
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 16 deletions.
13 changes: 10 additions & 3 deletions android/guava/src/com/google/common/base/Joiner.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.AbstractList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.annotation.CheckForNull;
Expand Down Expand Up @@ -129,7 +130,9 @@ public <A extends Appendable> A appendTo(A appendable, Iterator<? extends @Nulla
@CanIgnoreReturnValue
public final <A extends Appendable> A appendTo(A appendable, @Nullable Object[] parts)
throws IOException {
return appendTo(appendable, Arrays.asList(parts));
@SuppressWarnings("nullness") // TODO: b/316358623 - Remove suppression after fixing checker
List<?> partsList = Arrays.<@Nullable Object>asList(parts);
return appendTo(appendable, partsList);
}

/** Appends to {@code appendable} the string representation of each of the remaining arguments. */
Expand Down Expand Up @@ -179,7 +182,9 @@ public final StringBuilder appendTo(
*/
@CanIgnoreReturnValue
public final StringBuilder appendTo(StringBuilder builder, @Nullable Object[] parts) {
return appendTo(builder, Arrays.asList(parts));
@SuppressWarnings("nullness") // TODO: b/316358623 - Remove suppression after fixing checker
List<?> partsList = Arrays.<@Nullable Object>asList(parts);
return appendTo(builder, partsList);
}

/**
Expand Down Expand Up @@ -219,7 +224,9 @@ public final String join(Iterator<? extends @Nullable Object> parts) {
* previously configured separator between each.
*/
public final String join(@Nullable Object[] parts) {
return join(Arrays.asList(parts));
@SuppressWarnings("nullness") // TODO: b/316358623 - Remove suppression after fixing checker
List<?> partsList = Arrays.<@Nullable Object>asList(parts);
return join(partsList);
}

/**
Expand Down
5 changes: 4 additions & 1 deletion android/guava/src/com/google/common/escape/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package com.google.common.escape;

import static java.util.Objects.requireNonNull;

import com.google.common.annotations.GwtCompatible;

/**
Expand All @@ -28,7 +30,8 @@ private Platform() {}

/** Returns a thread-local 1024-char array. */
static char[] charBufferFromThreadLocal() {
return DEST_TL.get();
// requireNonNull accommodates Android's @RecentlyNullable annotation on ThreadLocal.get
return requireNonNull(DEST_TL.get());
}

/**
Expand Down
4 changes: 3 additions & 1 deletion android/guava/src/com/google/common/eventbus/Dispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package com.google.common.eventbus;

import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.Objects.requireNonNull;

import com.google.common.collect.Queues;
import java.util.Iterator;
Expand Down Expand Up @@ -97,7 +98,8 @@ protected Boolean initialValue() {
void dispatch(Object event, Iterator<Subscriber> subscribers) {
checkNotNull(event);
checkNotNull(subscribers);
Queue<Event> queueForThread = queue.get();
// requireNonNull accommodates Android's @RecentlyNullable annotation on ThreadLocal.get
Queue<Event> queueForThread = requireNonNull(queue.get());
queueForThread.offer(new Event(event, subscribers));

if (!dispatching.get()) {
Expand Down
4 changes: 3 additions & 1 deletion android/guava/src/com/google/common/net/InetAddresses.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.Objects.requireNonNull;

import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.J2ktIncompatible;
Expand Down Expand Up @@ -409,7 +410,8 @@ public static String toAddrString(InetAddress ip) {
checkNotNull(ip);
if (ip instanceof Inet4Address) {
// For IPv4, Java's formatting is good enough.
return ip.getHostAddress();
// requireNonNull accommodates Android's @RecentlyNullable annotation on getHostAddress
return requireNonNull(ip.getHostAddress());
}
checkArgument(ip instanceof Inet6Address);
byte[] bytes = ip.getAddress();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,8 @@ private ExampleStackTrace findPathTo(LockGraphNode node, Set<LockGraphNode> seen
*/
private void aboutToAcquire(CycleDetectingLock lock) {
if (!lock.isAcquiredByCurrentThread()) {
ArrayList<LockGraphNode> acquiredLockList = acquiredLocks.get();
// requireNonNull accommodates Android's @RecentlyNullable annotation on ThreadLocal.get
ArrayList<LockGraphNode> acquiredLockList = requireNonNull(acquiredLocks.get());
LockGraphNode node = lock.getLockGraphNode();
node.checkAcquiredLocks(policy, acquiredLockList);
acquiredLockList.add(node);
Expand All @@ -724,7 +725,8 @@ private void aboutToAcquire(CycleDetectingLock lock) {
*/
private static void lockStateChanged(CycleDetectingLock lock) {
if (!lock.isAcquiredByCurrentThread()) {
ArrayList<LockGraphNode> acquiredLockList = acquiredLocks.get();
// requireNonNull accommodates Android's @RecentlyNullable annotation on ThreadLocal.get
ArrayList<LockGraphNode> acquiredLockList = requireNonNull(acquiredLocks.get());
LockGraphNode node = lock.getLockGraphNode();
// Iterate in reverse because locks are usually locked/unlocked in a
// LIFO order.
Expand Down
13 changes: 10 additions & 3 deletions guava/src/com/google/common/base/Joiner.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.AbstractList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.annotation.CheckForNull;
Expand Down Expand Up @@ -129,7 +130,9 @@ public <A extends Appendable> A appendTo(A appendable, Iterator<? extends @Nulla
@CanIgnoreReturnValue
public final <A extends Appendable> A appendTo(A appendable, @Nullable Object[] parts)
throws IOException {
return appendTo(appendable, Arrays.asList(parts));
@SuppressWarnings("nullness") // TODO: b/316358623 - Remove suppression after fixing checker
List<?> partsList = Arrays.<@Nullable Object>asList(parts);
return appendTo(appendable, partsList);
}

/** Appends to {@code appendable} the string representation of each of the remaining arguments. */
Expand Down Expand Up @@ -179,7 +182,9 @@ public final StringBuilder appendTo(
*/
@CanIgnoreReturnValue
public final StringBuilder appendTo(StringBuilder builder, @Nullable Object[] parts) {
return appendTo(builder, Arrays.asList(parts));
@SuppressWarnings("nullness") // TODO: b/316358623 - Remove suppression after fixing checker
List<?> partsList = Arrays.<@Nullable Object>asList(parts);
return appendTo(builder, partsList);
}

/**
Expand Down Expand Up @@ -219,7 +224,9 @@ public final String join(Iterator<? extends @Nullable Object> parts) {
* previously configured separator between each.
*/
public final String join(@Nullable Object[] parts) {
return join(Arrays.asList(parts));
@SuppressWarnings("nullness") // TODO: b/316358623 - Remove suppression after fixing checker
List<?> partsList = Arrays.<@Nullable Object>asList(parts);
return join(partsList);
}

/**
Expand Down
5 changes: 4 additions & 1 deletion guava/src/com/google/common/escape/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package com.google.common.escape;

import static java.util.Objects.requireNonNull;

import com.google.common.annotations.GwtCompatible;

/**
Expand All @@ -28,7 +30,8 @@ private Platform() {}

/** Returns a thread-local 1024-char array. */
static char[] charBufferFromThreadLocal() {
return DEST_TL.get();
// requireNonNull accommodates Android's @RecentlyNullable annotation on ThreadLocal.get
return requireNonNull(DEST_TL.get());
}

/**
Expand Down
4 changes: 3 additions & 1 deletion guava/src/com/google/common/eventbus/Dispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package com.google.common.eventbus;

import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.Objects.requireNonNull;

import com.google.common.collect.Queues;
import java.util.Iterator;
Expand Down Expand Up @@ -97,7 +98,8 @@ protected Boolean initialValue() {
void dispatch(Object event, Iterator<Subscriber> subscribers) {
checkNotNull(event);
checkNotNull(subscribers);
Queue<Event> queueForThread = queue.get();
// requireNonNull accommodates Android's @RecentlyNullable annotation on ThreadLocal.get
Queue<Event> queueForThread = requireNonNull(queue.get());
queueForThread.offer(new Event(event, subscribers));

if (!dispatching.get()) {
Expand Down
4 changes: 3 additions & 1 deletion guava/src/com/google/common/net/InetAddresses.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.Objects.requireNonNull;

import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.J2ktIncompatible;
Expand Down Expand Up @@ -409,7 +410,8 @@ public static String toAddrString(InetAddress ip) {
checkNotNull(ip);
if (ip instanceof Inet4Address) {
// For IPv4, Java's formatting is good enough.
return ip.getHostAddress();
// requireNonNull accommodates Android's @RecentlyNullable annotation on getHostAddress
return requireNonNull(ip.getHostAddress());
}
checkArgument(ip instanceof Inet6Address);
byte[] bytes = ip.getAddress();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,8 @@ private ExampleStackTrace findPathTo(LockGraphNode node, Set<LockGraphNode> seen
*/
private void aboutToAcquire(CycleDetectingLock lock) {
if (!lock.isAcquiredByCurrentThread()) {
ArrayList<LockGraphNode> acquiredLockList = acquiredLocks.get();
// requireNonNull accommodates Android's @RecentlyNullable annotation on ThreadLocal.get
ArrayList<LockGraphNode> acquiredLockList = requireNonNull(acquiredLocks.get());
LockGraphNode node = lock.getLockGraphNode();
node.checkAcquiredLocks(policy, acquiredLockList);
acquiredLockList.add(node);
Expand All @@ -724,7 +725,8 @@ private void aboutToAcquire(CycleDetectingLock lock) {
*/
private static void lockStateChanged(CycleDetectingLock lock) {
if (!lock.isAcquiredByCurrentThread()) {
ArrayList<LockGraphNode> acquiredLockList = acquiredLocks.get();
// requireNonNull accommodates Android's @RecentlyNullable annotation on ThreadLocal.get
ArrayList<LockGraphNode> acquiredLockList = requireNonNull(acquiredLocks.get());
LockGraphNode node = lock.getLockGraphNode();
// Iterate in reverse because locks are usually locked/unlocked in a
// LIFO order.
Expand Down

0 comments on commit b90e3be

Please sign in to comment.