Skip to content

Commit e447217

Browse files
authored
Add emulation for Collection.toArray(IntFunction) (#10180)
Fixes #10158
1 parent 8d9a7f0 commit e447217

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

user/super/com/google/gwt/emul/java/util/Collection.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import static javaemul.internal.InternalPreconditions.checkNotNull;
1919

20+
import java.util.function.IntFunction;
2021
import java.util.function.Predicate;
2122
import java.util.stream.Stream;
2223
import java.util.stream.StreamSupport;
@@ -87,4 +88,12 @@ default Stream<E> stream() {
8788

8889
@JsIgnore
8990
<T> T[] toArray(T[] a);
91+
92+
@JsIgnore
93+
default <T> T[] toArray(IntFunction<T[]> factory) {
94+
// This differs from the JDK's default implementation, which according to Javadoc
95+
// will call the function with zero, as this then requires GWT to clone the array
96+
// again before using it.
97+
return toArray(factory.apply(size()));
98+
}
9099
}

user/test/com/google/gwt/emultest/EmulJava11Suite.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.google.gwt.emultest;
1717

1818
import com.google.gwt.emultest.java11.lang.StringTest;
19+
import com.google.gwt.emultest.java11.util.CollectionTest;
1920
import com.google.gwt.emultest.java11.util.OptionalDoubleTest;
2021
import com.google.gwt.emultest.java11.util.OptionalIntTest;
2122
import com.google.gwt.emultest.java11.util.OptionalLongTest;
@@ -28,6 +29,7 @@
2829
/** Test JRE emulations. */
2930
@RunWith(Suite.class)
3031
@Suite.SuiteClasses({
32+
CollectionTest.class,
3133
OptionalDoubleTest.class,
3234
OptionalIntTest.class,
3335
OptionalLongTest.class,
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2025 GWT Project Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.google.gwt.emultest.java11.util;
17+
18+
import com.google.gwt.emultest.java.util.EmulTestBase;
19+
20+
import java.util.Arrays;
21+
import java.util.List;
22+
import java.util.Set;
23+
24+
/**
25+
* Tests for java.util.Collection Java 11 API emulation.
26+
* <p>
27+
* We can't easily test Collection methods themselves, so trying a few basic implementations.
28+
*/
29+
public class CollectionTest extends EmulTestBase {
30+
31+
public void testListArrayOfMethodRef() {
32+
String[] arr = List.of("a", "b").toArray(String[]::new);
33+
assertEquals(2, arr.length);
34+
assertTrue(arr instanceof String[]);
35+
36+
assertEquals("a", arr[0]);
37+
assertEquals("b", arr[1]);
38+
}
39+
40+
public void testSetArrayOfMethodRef() {
41+
Set<String> ab = Set.of("a", "b");
42+
String[] arr = ab.toArray(String[]::new);
43+
assertEquals(2, arr.length);
44+
assertTrue(arr instanceof String[]);
45+
assertTrue(ab.containsAll(Arrays.asList(arr)));
46+
}
47+
}

0 commit comments

Comments
 (0)