From 137f87a3809e3d8d8f572b07f464237f3277b0b2 Mon Sep 17 00:00:00 2001 From: John Wagenleitner Date: Sun, 27 May 2018 12:10:59 -0700 Subject: [PATCH 1/3] GROOVY-8610: STC NPE using DGM collect on Iterator --- .../org/codehaus/groovy/runtime/DefaultGroovyMethods.java | 2 +- .../transform/stc/ClosureParamTypeInferenceSTCTest.groovy | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java index 1429102eb52..659a3976ed0 100644 --- a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java +++ b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java @@ -3499,7 +3499,7 @@ public static Collection collect(S[] self, Collection collector, @Cl * @return a List of the transformed values * @since 2.5.0 */ - public static List collect(Iterator self, @ClosureParams(FirstParam.Component.class) Closure transform) { + public static List collect(Iterator self, @ClosureParams(FirstParam.FirstGenericType.class) Closure transform) { return (List) collect(self, new ArrayList(), transform); } diff --git a/src/test/groovy/transform/stc/ClosureParamTypeInferenceSTCTest.groovy b/src/test/groovy/transform/stc/ClosureParamTypeInferenceSTCTest.groovy index 493802eedf7..d9b27c5b437 100644 --- a/src/test/groovy/transform/stc/ClosureParamTypeInferenceSTCTest.groovy +++ b/src/test/groovy/transform/stc/ClosureParamTypeInferenceSTCTest.groovy @@ -231,6 +231,13 @@ def items = [] ''' } + void testDGM_collectOnIterator() { + assertScript ''' + List list = ['foo', 'bar', 'baz'] + list.iterator().collect { it.startsWith('ba') } == [false, true, true] + ''' + } + void testInferenceOnNonExtensionMethod() { assertScript '''import groovy.transform.stc.ClosureParams import groovy.transform.stc.FirstParam From 8bc827ac0555d15a1fd44c9c83e5d61c6c3c24f1 Mon Sep 17 00:00:00 2001 From: John Wagenleitner Date: Sun, 27 May 2018 12:57:45 -0700 Subject: [PATCH 2/3] add missing assert --- .../transform/stc/ClosureParamTypeInferenceSTCTest.groovy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/groovy/transform/stc/ClosureParamTypeInferenceSTCTest.groovy b/src/test/groovy/transform/stc/ClosureParamTypeInferenceSTCTest.groovy index d9b27c5b437..83a78efdc26 100644 --- a/src/test/groovy/transform/stc/ClosureParamTypeInferenceSTCTest.groovy +++ b/src/test/groovy/transform/stc/ClosureParamTypeInferenceSTCTest.groovy @@ -234,7 +234,7 @@ def items = [] void testDGM_collectOnIterator() { assertScript ''' List list = ['foo', 'bar', 'baz'] - list.iterator().collect { it.startsWith('ba') } == [false, true, true] + assert list.iterator().collect { it.startsWith('ba') } == [false, true, true] ''' } From e31c4ba19d9e20d013bf49de2381d3f8a7dd084e Mon Sep 17 00:00:00 2001 From: John Wagenleitner Date: Sun, 27 May 2018 13:02:37 -0700 Subject: [PATCH 3/3] minor test refactor --- .../transform/stc/ClosureParamTypeInferenceSTCTest.groovy | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/groovy/transform/stc/ClosureParamTypeInferenceSTCTest.groovy b/src/test/groovy/transform/stc/ClosureParamTypeInferenceSTCTest.groovy index 83a78efdc26..0e7f2965d42 100644 --- a/src/test/groovy/transform/stc/ClosureParamTypeInferenceSTCTest.groovy +++ b/src/test/groovy/transform/stc/ClosureParamTypeInferenceSTCTest.groovy @@ -233,8 +233,8 @@ def items = [] void testDGM_collectOnIterator() { assertScript ''' - List list = ['foo', 'bar', 'baz'] - assert list.iterator().collect { it.startsWith('ba') } == [false, true, true] + Iterator itr = ['foo', 'bar', 'baz'].iterator() + assert itr.collect { it.startsWith('ba') } == [false, true, true] ''' }