Skip to content

Commit

Permalink
Use Class<@nonnull T> in one more class, ObjectArrays.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Jun 12, 2023
1 parent 59fe667 commit 501a016
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
7 changes: 5 additions & 2 deletions android/guava/src/com/google/common/collect/ObjectArrays.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -35,6 +36,7 @@
*/
@GwtCompatible(emulated = true)
@ElementTypesAreNonnullByDefault
@SuppressWarnings("AvoidObjectArrays")
public final class ObjectArrays {

private ObjectArrays() {}
Expand All @@ -47,7 +49,7 @@ private ObjectArrays() {}
*/
@GwtIncompatible // Array.newInstance(Class, int)
@SuppressWarnings("unchecked")
public static <T> T[] newArray(Class<T> type, int length) {
public static <T extends @Nullable Object> T[] newArray(Class<@NonNull T> type, int length) {
return (T[]) Array.newInstance(type, length);
}

Expand All @@ -69,7 +71,8 @@ public static <T> T[] newArray(Class<T> type, int length) {
* @param type the component type of the returned array
*/
@GwtIncompatible // Array.newInstance(Class, int)
public static <T> T[] concat(T[] first, T[] second, Class<T> type) {
public static <T extends @Nullable Object> 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);
Expand Down
7 changes: 5 additions & 2 deletions guava/src/com/google/common/collect/ObjectArrays.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -34,6 +35,7 @@
*/
@GwtCompatible(emulated = true)
@ElementTypesAreNonnullByDefault
@SuppressWarnings("AvoidObjectArrays")
public final class ObjectArrays {

private ObjectArrays() {}
Expand All @@ -46,7 +48,7 @@ private ObjectArrays() {}
*/
@GwtIncompatible // Array.newInstance(Class, int)
@SuppressWarnings("unchecked")
public static <T> T[] newArray(Class<T> type, int length) {
public static <T extends @Nullable Object> T[] newArray(Class<@NonNull T> type, int length) {
return (T[]) Array.newInstance(type, length);
}

Expand All @@ -68,7 +70,8 @@ public static <T> T[] newArray(Class<T> type, int length) {
* @param type the component type of the returned array
*/
@GwtIncompatible // Array.newInstance(Class, int)
public static <T> T[] concat(T[] first, T[] second, Class<T> type) {
public static <T extends @Nullable Object> 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);
Expand Down

0 comments on commit 501a016

Please sign in to comment.