Skip to content

Commit 501a016

Browse files
cpovirkGoogle Java Core Libraries
authored and
Google Java Core Libraries
committedJun 12, 2023
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<T>` 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&lt;[^?]' releases/snapshot-jre/api/docs/com/ | grep -v class-use | s 's/&lt;/</g' | s 's/&gt;/>/g' | s 's/&nbsp;/ /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
1 parent 59fe667 commit 501a016

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed
 

‎android/guava/src/com/google/common/collect/ObjectArrays.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.Arrays;
2626
import java.util.Collection;
2727
import javax.annotation.CheckForNull;
28+
import org.checkerframework.checker.nullness.qual.NonNull;
2829
import org.checkerframework.checker.nullness.qual.Nullable;
2930

3031
/**
@@ -35,6 +36,7 @@
3536
*/
3637
@GwtCompatible(emulated = true)
3738
@ElementTypesAreNonnullByDefault
39+
@SuppressWarnings("AvoidObjectArrays")
3840
public final class ObjectArrays {
3941

4042
private ObjectArrays() {}
@@ -47,7 +49,7 @@ private ObjectArrays() {}
4749
*/
4850
@GwtIncompatible // Array.newInstance(Class, int)
4951
@SuppressWarnings("unchecked")
50-
public static <T> T[] newArray(Class<T> type, int length) {
52+
public static <T extends @Nullable Object> T[] newArray(Class<@NonNull T> type, int length) {
5153
return (T[]) Array.newInstance(type, length);
5254
}
5355

@@ -69,7 +71,8 @@ public static <T> T[] newArray(Class<T> type, int length) {
6971
* @param type the component type of the returned array
7072
*/
7173
@GwtIncompatible // Array.newInstance(Class, int)
72-
public static <T> T[] concat(T[] first, T[] second, Class<T> type) {
74+
public static <T extends @Nullable Object> T[] concat(
75+
T[] first, T[] second, Class<@NonNull T> type) {
7376
T[] result = newArray(type, first.length + second.length);
7477
System.arraycopy(first, 0, result, 0, first.length);
7578
System.arraycopy(second, 0, result, first.length, second.length);

‎guava/src/com/google/common/collect/ObjectArrays.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.lang.reflect.Array;
2525
import java.util.Arrays;
2626
import java.util.Collection;
27+
import org.checkerframework.checker.nullness.qual.NonNull;
2728
import org.checkerframework.checker.nullness.qual.Nullable;
2829

2930
/**
@@ -34,6 +35,7 @@
3435
*/
3536
@GwtCompatible(emulated = true)
3637
@ElementTypesAreNonnullByDefault
38+
@SuppressWarnings("AvoidObjectArrays")
3739
public final class ObjectArrays {
3840

3941
private ObjectArrays() {}
@@ -46,7 +48,7 @@ private ObjectArrays() {}
4648
*/
4749
@GwtIncompatible // Array.newInstance(Class, int)
4850
@SuppressWarnings("unchecked")
49-
public static <T> T[] newArray(Class<T> type, int length) {
51+
public static <T extends @Nullable Object> T[] newArray(Class<@NonNull T> type, int length) {
5052
return (T[]) Array.newInstance(type, length);
5153
}
5254

@@ -68,7 +70,8 @@ public static <T> T[] newArray(Class<T> type, int length) {
6870
* @param type the component type of the returned array
6971
*/
7072
@GwtIncompatible // Array.newInstance(Class, int)
71-
public static <T> T[] concat(T[] first, T[] second, Class<T> type) {
73+
public static <T extends @Nullable Object> T[] concat(
74+
T[] first, T[] second, Class<@NonNull T> type) {
7275
T[] result = newArray(type, first.length + second.length);
7376
System.arraycopy(first, 0, result, 0, first.length);
7477
System.arraycopy(second, 0, result, first.length, second.length);

0 commit comments

Comments
 (0)
Please sign in to comment.