Skip to content
This repository was archived by the owner on Feb 25, 2024. It is now read-only.

Commit f6cf1c4

Browse files
committed
JDK-8156071
1 parent aec328e commit f6cf1c4

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
lines changed

src/main/java/java9/util/ImmutableCollections.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ final class ImmutableCollections {
9595
// use the lowest bit to determine if we should reverse iteration
9696
REVERSE = (SALT32L & 1) == 0;
9797
EMPTY = new Object();
98-
EMPTY_LIST = new ListN<>();
98+
EMPTY_LIST = new ListN<>(new Object[0]);
9999
EMPTY_SET = new SetN<>();
100100
EMPTY_MAP = new MapN<>();
101101
}
@@ -488,15 +488,31 @@ static final class ListN<E> extends AbstractImmutableList<E>
488488

489489
private final E[] elements;
490490

491+
ListN(E[] array) {
492+
elements = array;
493+
}
494+
495+
// creates a new internal array, and checks and rejects null elements
491496
@SafeVarargs
492-
ListN(E... input) {
497+
static <E> List<E> fromArray(E... input) {
493498
// copy and check manually to avoid TOCTOU
494499
@SuppressWarnings("unchecked")
495500
E[] tmp = (E[]) new Object[input.length]; // implicit nullcheck of input
496501
for (int i = 0; i < input.length; i++) {
497502
tmp[i] = Objects.requireNonNull(input[i]);
498503
}
499-
elements = tmp;
504+
return new ListN<>(tmp);
505+
}
506+
507+
// Avoids creating a new array, but checks and rejects null elements.
508+
// Declared with Object... arg so that varargs calls don't accidentally
509+
// create an array of a subtype.
510+
@SuppressWarnings("unchecked")
511+
static <E> List<E> fromTrustedArray(Object... input) {
512+
for (Object o : input) {
513+
Objects.requireNonNull(o);
514+
}
515+
return new ListN<>((E[])input);
500516
}
501517

502518
@Override

src/main/java/java9/util/Lists.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public static <E> List<E> of(E e1, E e2) {
127127
* @since 9
128128
*/
129129
public static <E> List<E> of(E e1, E e2, E e3) {
130-
return new ImmutableCollections.ListN<E>(e1, e2, e3);
130+
return ImmutableCollections.ListN.fromTrustedArray(e1, e2, e3);
131131
}
132132

133133
/**
@@ -146,7 +146,7 @@ public static <E> List<E> of(E e1, E e2, E e3) {
146146
* @since 9
147147
*/
148148
public static <E> List<E> of(E e1, E e2, E e3, E e4) {
149-
return new ImmutableCollections.ListN<E>(e1, e2, e3, e4);
149+
return ImmutableCollections.ListN.fromTrustedArray(e1, e2, e3, e4);
150150
}
151151

152152
/**
@@ -166,7 +166,7 @@ public static <E> List<E> of(E e1, E e2, E e3, E e4) {
166166
* @since 9
167167
*/
168168
public static <E> List<E> of(E e1, E e2, E e3, E e4, E e5) {
169-
return new ImmutableCollections.ListN<E>(e1, e2, e3, e4, e5);
169+
return ImmutableCollections.ListN.fromTrustedArray(e1, e2, e3, e4, e5);
170170
}
171171

172172
/**
@@ -187,8 +187,8 @@ public static <E> List<E> of(E e1, E e2, E e3, E e4, E e5) {
187187
* @since 9
188188
*/
189189
public static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6) {
190-
return new ImmutableCollections.ListN<E>(e1, e2, e3, e4, e5,
191-
e6);
190+
return ImmutableCollections.ListN.fromTrustedArray(e1, e2, e3, e4, e5,
191+
e6);
192192
}
193193

194194
/**
@@ -210,8 +210,8 @@ public static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6) {
210210
* @since 9
211211
*/
212212
public static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7) {
213-
return new ImmutableCollections.ListN<E>(e1, e2, e3, e4, e5,
214-
e6, e7);
213+
return ImmutableCollections.ListN.fromTrustedArray(e1, e2, e3, e4, e5,
214+
e6, e7);
215215
}
216216

217217
/**
@@ -234,8 +234,8 @@ public static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7) {
234234
* @since 9
235235
*/
236236
public static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8) {
237-
return new ImmutableCollections.ListN<E>(e1, e2, e3, e4, e5,
238-
e6, e7, e8);
237+
return ImmutableCollections.ListN.fromTrustedArray(e1, e2, e3, e4, e5,
238+
e6, e7, e8);
239239
}
240240

241241
/**
@@ -259,8 +259,8 @@ public static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8) {
259259
* @since 9
260260
*/
261261
public static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9) {
262-
return new ImmutableCollections.ListN<E>(e1, e2, e3, e4, e5,
263-
e6, e7, e8, e9);
262+
return ImmutableCollections.ListN.fromTrustedArray(e1, e2, e3, e4, e5,
263+
e6, e7, e8, e9);
264264
}
265265

266266
/**
@@ -285,8 +285,8 @@ public static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e
285285
* @since 9
286286
*/
287287
public static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10) {
288-
return new ImmutableCollections.ListN<E>(e1, e2, e3, e4, e5,
289-
e6, e7, e8, e9, e10);
288+
return ImmutableCollections.ListN.fromTrustedArray(e1, e2, e3, e4, e5,
289+
e6, e7, e8, e9, e10);
290290
}
291291

292292
/**
@@ -326,7 +326,7 @@ public static <E> List<E> of(E... elements) {
326326
case 2:
327327
return new ImmutableCollections.List12<E>(elements[0], elements[1]);
328328
default:
329-
return new ImmutableCollections.ListN<E>(elements);
329+
return ImmutableCollections.ListN.fromArray(elements);
330330
}
331331
}
332332

0 commit comments

Comments
 (0)