You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2020/05/17 07:10:21 UTC

[groovy] 02/02: GROOVY-9545: add extension method for stream->array using class literal (minor refactor)

This is an automated email from the ASF dual-hosted git repository.

paulk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 1e9650df4360ba5a5ad5f2a9b8b73ca031e4cadc
Author: Paul King <pa...@asert.com.au>
AuthorDate: Sun May 17 17:09:21 2020 +1000

    GROOVY-9545: add extension method for stream->array using class literal (minor refactor)
---
 .../groovy/vmplugin/v8/PluginDefaultGroovyMethods.java    | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/vmplugin/v8/PluginDefaultGroovyMethods.java b/src/main/java/org/codehaus/groovy/vmplugin/v8/PluginDefaultGroovyMethods.java
index cd63f4f..48e150e 100644
--- a/src/main/java/org/codehaus/groovy/vmplugin/v8/PluginDefaultGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/vmplugin/v8/PluginDefaultGroovyMethods.java
@@ -505,14 +505,22 @@ public class PluginDefaultGroovyMethods extends DefaultGroovyMethodsSupport {
     /**
      * Returns an array containing the elements of the stream.
      * <pre class="groovyTestCase">
+     * import static groovy.test.GroovyAssert.shouldFail
+     *
      * assert Arrays.equals([].stream().toArray(Object), new Object[0])
      * assert Arrays.equals([].stream().toArray(String), new String[0])
+     * assert Arrays.equals([].stream().toArray(String[]), new String[0][])
      * assert Arrays.equals(['x'].stream().toArray(Object), ['x'].toArray())
      * assert Arrays.equals(['x'].stream().toArray(String), ['x'] as String[])
+     * assert Arrays.deepEquals([['x'] as String[]].stream().toArray(String[]), [['x'] as String[]] as String[][])
      * assert Arrays.equals(['x'].stream().toArray(CharSequence), ['x'] as CharSequence[])
      *
-     * groovy.test.GroovyAssert.shouldFail(ArrayStoreException) {
-     *   ['x'].stream().toArray(Thread)
+     * shouldFail(ArrayStoreException) {
+     *     ['x'].stream().toArray(Thread)
+     * }
+     *
+     * shouldFail(IllegalArgumentException) {
+     *     ['x'].stream().toArray((Class) null)
      * }
      *
      * // Stream#toArray(IntFunction) should still be used for closure literal:
@@ -525,9 +533,10 @@ public class PluginDefaultGroovyMethods extends DefaultGroovyMethodsSupport {
      * @param self the stream
      * @param type the array element type
      *
-     * @since x.y.z
+     * @since 3.0.4
      */
     public static <T> T[] toArray(final Stream<? extends T> self, final Class<T> type) {
+        if (type == null) throw new IllegalArgumentException("type cannot be null");
         return self.toArray(length -> (T[]) Array.newInstance(type, length));
     }