From 501a01631f742bbcb73cf46ae409abf567903944 Mon Sep 17 00:00:00 2001 From: cpovirk Date: Mon, 12 Jun 2023 08:52:08 -0700 Subject: [PATCH] Use `Class<@NonNull T>` in one more class, `ObjectArrays`. Arrays are weird, as discussed in cl/382268713, so we're still making compromises here. This CL at least: - lets users create arrays with nullable element types. That said, if people want a `@Nullable String[]`, we were already letting them create a `String[]` and then widen it to `@Nullable String`, at least under our current treatment of arrays. Kotlin wouldn't be so accommodating, but I doubt that Kotlin users need this class much, of at all. - lets users concatenate arrays with nullable element types: `ObjectArrays.concat(nullableStrings, moreNullableStrings, String.class)`. I don't remember how I had identified `Class` usages to convert to `Class<@NonNull T>` previously; it might just have been that I remembered some offhand but not others. And perhaps I'd put `ObjectArrays` out of mind entirely on the grounds that arrays are weird. But now I tried to grep through our Javadoc in the `gh-pages` branch and then gradually filter out usages that didn't look related. It's still possible that I missed something, but I can report that `ObjectArrays` contained the only usages left at the end of the process: ``` git grep 'Class<[^?]' releases/snapshot-jre/api/docs/com/ | grep -v class-use | s 's/<//g' | s 's/ / /g' | grep -v -e catching -e exceptionType -e exceptionClass -e declaredType -e Annotation -e Enum -e enum -e interfaceType -e wrap -e filter -e Iterables -e Iterators -e FluentIterable -e defaultValue -e CauseType -e InstanceOf -e ToInstanceMap -e ArrayTable -e Primitives -e Invokable -e TypeParameter -e TypeToken -e Sets.html ``` RELNOTES=n/a PiperOrigin-RevId: 539667000 --- .../guava/src/com/google/common/collect/ObjectArrays.java | 7 +++++-- guava/src/com/google/common/collect/ObjectArrays.java | 7 +++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/android/guava/src/com/google/common/collect/ObjectArrays.java b/android/guava/src/com/google/common/collect/ObjectArrays.java index 0f928e170275..309572afd5af 100644 --- a/android/guava/src/com/google/common/collect/ObjectArrays.java +++ b/android/guava/src/com/google/common/collect/ObjectArrays.java @@ -25,6 +25,7 @@ import java.util.Arrays; import java.util.Collection; import javax.annotation.CheckForNull; +import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; /** @@ -35,6 +36,7 @@ */ @GwtCompatible(emulated = true) @ElementTypesAreNonnullByDefault +@SuppressWarnings("AvoidObjectArrays") public final class ObjectArrays { private ObjectArrays() {} @@ -47,7 +49,7 @@ private ObjectArrays() {} */ @GwtIncompatible // Array.newInstance(Class, int) @SuppressWarnings("unchecked") - public static T[] newArray(Class type, int length) { + public static T[] newArray(Class<@NonNull T> type, int length) { return (T[]) Array.newInstance(type, length); } @@ -69,7 +71,8 @@ public static T[] newArray(Class type, int length) { * @param type the component type of the returned array */ @GwtIncompatible // Array.newInstance(Class, int) - public static T[] concat(T[] first, T[] second, Class type) { + public static T[] concat( + T[] first, T[] second, Class<@NonNull T> type) { T[] result = newArray(type, first.length + second.length); System.arraycopy(first, 0, result, 0, first.length); System.arraycopy(second, 0, result, first.length, second.length); diff --git a/guava/src/com/google/common/collect/ObjectArrays.java b/guava/src/com/google/common/collect/ObjectArrays.java index d3b048da7e45..1a2df3b3e787 100644 --- a/guava/src/com/google/common/collect/ObjectArrays.java +++ b/guava/src/com/google/common/collect/ObjectArrays.java @@ -24,6 +24,7 @@ import java.lang.reflect.Array; import java.util.Arrays; import java.util.Collection; +import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; /** @@ -34,6 +35,7 @@ */ @GwtCompatible(emulated = true) @ElementTypesAreNonnullByDefault +@SuppressWarnings("AvoidObjectArrays") public final class ObjectArrays { private ObjectArrays() {} @@ -46,7 +48,7 @@ private ObjectArrays() {} */ @GwtIncompatible // Array.newInstance(Class, int) @SuppressWarnings("unchecked") - public static T[] newArray(Class type, int length) { + public static T[] newArray(Class<@NonNull T> type, int length) { return (T[]) Array.newInstance(type, length); } @@ -68,7 +70,8 @@ public static T[] newArray(Class type, int length) { * @param type the component type of the returned array */ @GwtIncompatible // Array.newInstance(Class, int) - public static T[] concat(T[] first, T[] second, Class type) { + public static T[] concat( + T[] first, T[] second, Class<@NonNull T> type) { T[] result = newArray(type, first.length + second.length); System.arraycopy(first, 0, result, 0, first.length); System.arraycopy(second, 0, result, first.length, second.length);