You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2019/12/12 02:33:07 UTC
[groovy] 01/06: GROOVY-8762, GROOVY-9332,
GROOVY-9333: isInClosure() again for lambdas
This is an automated email from the ASF dual-hosted git repository.
sunlan pushed a commit to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit 8996c8fb2922f5906c7d6945d433c541b82064e0
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Thu Dec 12 10:29:37 2019 +0800
GROOVY-8762, GROOVY-9332, GROOVY-9333: isInClosure() again for lambdas
---
.../groovy/classgen/asm/WriterController.java | 2 +-
src/test/groovy/transform/stc/LambdaTest.groovy | 2513 +++++++++++---------
2 files changed, 1355 insertions(+), 1160 deletions(-)
diff --git a/src/main/java/org/codehaus/groovy/classgen/asm/WriterController.java b/src/main/java/org/codehaus/groovy/classgen/asm/WriterController.java
index 7b88390..a1f6caa 100644
--- a/src/main/java/org/codehaus/groovy/classgen/asm/WriterController.java
+++ b/src/main/java/org/codehaus/groovy/classgen/asm/WriterController.java
@@ -285,7 +285,7 @@ public class WriterController {
public boolean isInClosure() {
return classNode.getOuterClass() != null
&& classNode.getSuperClass().equals(ClassHelper.CLOSURE_TYPE)
- && classNode.implementsInterface(ClassHelper.GENERATED_CLOSURE_Type);
+ && classNode.implementsAnyInterfaces(ClassHelper.GENERATED_CLOSURE_Type, ClassHelper.GENERATED_LAMBDA_TYPE);
}
public boolean isInClosureConstructor() {
diff --git a/src/test/groovy/transform/stc/LambdaTest.groovy b/src/test/groovy/transform/stc/LambdaTest.groovy
index 7d02748..d0e1259 100644
--- a/src/test/groovy/transform/stc/LambdaTest.groovy
+++ b/src/test/groovy/transform/stc/LambdaTest.groovy
@@ -16,919 +16,1006 @@
* specific language governing permissions and limitations
* under the License.
*/
-
package groovy.transform.stc
-import groovy.test.GroovyTestCase
+import groovy.test.NotYetImplemented
+import groovy.transform.CompileStatic
+import org.junit.Test
+
+import static groovy.test.GroovyAssert.assertScript
+import static groovy.test.GroovyAssert.shouldFail
-class LambdaTest extends GroovyTestCase {
+@CompileStatic
+final class LambdaTest {
+
+ @Test
void testFunction() {
assertScript '''
- import groovy.transform.CompileStatic
- import java.util.stream.Collectors
- import java.util.stream.Stream
-
- @CompileStatic
- public class Test1 {
- public static void main(String[] args) {
- p();
- }
-
- public static void p() {
- assert [2, 3, 4] == [1, 2, 3].stream().map(e -> e.plus 1).collect(Collectors.toList());
+ import groovy.transform.CompileStatic
+ import java.util.stream.Collectors
+ import java.util.stream.Stream
+
+ @CompileStatic
+ public class Test1 {
+ public static void main(String[] args) {
+ p();
+ }
+
+ public static void p() {
+ assert [2, 3, 4] == [1, 2, 3].stream().map(e -> e.plus 1).collect(Collectors.toList());
+ }
}
- }
'''
}
+ @Test
void testFunction2() {
assertScript '''
- import groovy.transform.CompileStatic
- import java.util.stream.Collectors
- import java.util.stream.Stream
-
- public class Test1 {
- public static void main(String[] args) {
- p();
- }
-
- @CompileStatic
- public static void p() {
- assert [2, 3, 4] == [1, 2, 3].stream().map(e -> e.plus 1).collect(Collectors.toList());
+ import groovy.transform.CompileStatic
+ import java.util.stream.Collectors
+ import java.util.stream.Stream
+
+ public class Test1 {
+ public static void main(String[] args) {
+ p();
+ }
+
+ @CompileStatic
+ public static void p() {
+ assert [2, 3, 4] == [1, 2, 3].stream().map(e -> e.plus 1).collect(Collectors.toList());
+ }
}
- }
'''
}
-
+ @Test
void testFunctionScript() {
assertScript '''
- import groovy.transform.CompileStatic
- import java.util.stream.Collectors
- import java.util.stream.Stream
-
- @CompileStatic
- void p() {
- assert [2, 3, 4] == [1, 2, 3].stream().map(e -> e + 1).collect(Collectors.toList());
- }
-
- p()
+ import groovy.transform.CompileStatic
+ import java.util.stream.Collectors
+ import java.util.stream.Stream
+
+ @CompileStatic
+ void p() {
+ assert [2, 3, 4] == [1, 2, 3].stream().map(e -> e + 1).collect(Collectors.toList());
+ }
+
+ p()
'''
}
+ @Test
void testFunctionScript2() {
assertScript '''
- import groovy.transform.CompileStatic
- import java.util.stream.Collectors
- import java.util.stream.Stream
-
- @CompileStatic
- void p() {
- assert [2, 3, 4] == [1, 2, 3].stream().map(e -> e.plus 1).collect(Collectors.toList());
- }
-
- p()
+ import groovy.transform.CompileStatic
+ import java.util.stream.Collectors
+ import java.util.stream.Stream
+
+ @CompileStatic
+ void p() {
+ assert [2, 3, 4] == [1, 2, 3].stream().map(e -> e.plus 1).collect(Collectors.toList());
+ }
+
+ p()
'''
}
+ @Test
void testBinaryOperator() {
assertScript '''
- import groovy.transform.CompileStatic
- import java.util.stream.Collectors
- import java.util.stream.Stream
-
- @CompileStatic
- public class Test1 {
- public static void main(String[] args) {
- p();
- }
-
- public static void p() {
- assert 13 == [1, 2, 3].stream().reduce(7, (Integer r, Integer e) -> r + e);
+ import groovy.transform.CompileStatic
+ import java.util.stream.Collectors
+ import java.util.stream.Stream
+
+ @CompileStatic
+ public class Test1 {
+ public static void main(String[] args) {
+ p();
+ }
+
+ public static void p() {
+ assert 13 == [1, 2, 3].stream().reduce(7, (Integer r, Integer e) -> r + e);
+ }
}
- }
'''
}
- // GROOVY-8917: Failed to infer parameter type of some SAM, e.g. BinaryOperator
+ @Test // GROOVY-8917: Failed to infer parameter type of some SAM, e.g. BinaryOperator
void testBinaryOperatorWithoutExplicitTypeDef() {
assertScript '''
- import groovy.transform.CompileStatic
- import java.util.stream.Collectors
- import java.util.stream.Stream
-
- @CompileStatic
- public class Test1 {
- public static void main(String[] args) {
- p();
- }
-
- public static void p() {
- assert 13 == [1, 2, 3].stream().reduce(7, (r, e) -> r + e);
+ import groovy.transform.CompileStatic
+ import java.util.stream.Collectors
+ import java.util.stream.Stream
+
+ @CompileStatic
+ public class Test1 {
+ public static void main(String[] args) {
+ p();
+ }
+
+ public static void p() {
+ assert 13 == [1, 2, 3].stream().reduce(7, (r, e) -> r + e);
+ }
}
- }
'''
}
+ @Test
void testBinaryOperatorWithoutExplicitTypeDef2() {
assertScript '''
- import groovy.transform.CompileStatic
- import java.util.stream.Collectors
- import java.util.stream.Stream
- import java.util.function.BinaryOperator
-
- @CompileStatic
- public class Test1 {
- public static void main(String[] args) {
- p();
- }
-
- public static void p() {
- BinaryOperator<Integer> accumulator = (r, e) -> r + e
- assert 13 == [1, 2, 3].stream().reduce(7, accumulator);
+ import groovy.transform.CompileStatic
+ import java.util.stream.Collectors
+ import java.util.stream.Stream
+ import java.util.function.BinaryOperator
+
+ @CompileStatic
+ public class Test1 {
+ public static void main(String[] args) {
+ p();
+ }
+
+ public static void p() {
+ BinaryOperator<Integer> accumulator = (r, e) -> r + e
+ assert 13 == [1, 2, 3].stream().reduce(7, accumulator);
+ }
}
- }
'''
}
+ @Test
void testConsumer() {
assertScript '''
- import groovy.transform.CompileStatic
- import java.util.stream.Collectors
- import java.util.stream.Stream
-
- @CompileStatic
- public class Test1 {
- public static void main(String[] args) {
- p();
- }
-
- public static void p() {
- [1, 2, 3].stream().forEach(e -> { System.out.println(e + 1); });
+ import groovy.transform.CompileStatic
+ import java.util.stream.Collectors
+ import java.util.stream.Stream
+
+ @CompileStatic
+ public class Test1 {
+ public static void main(String[] args) {
+ p();
+ }
+
+ public static void p() {
+ [1, 2, 3].stream().forEach(e -> { System.out.println(e + 1); });
+ }
}
-
- }
'''
}
+ @Test
void testPredicate() {
assertScript '''
- import groovy.transform.CompileStatic
- import java.util.stream.Collectors
- import java.util.stream.Stream
-
- @CompileStatic
- public class Test1 {
- public static void main(String[] args) {
- p();
- }
-
- public static void p() {
- def list = ['ab', 'bc', 'de']
- list.removeIf(e -> e.startsWith("a"))
- assert ['bc', 'de'] == list
+ import groovy.transform.CompileStatic
+ import java.util.stream.Collectors
+ import java.util.stream.Stream
+
+ @CompileStatic
+ public class Test1 {
+ public static void main(String[] args) {
+ p();
+ }
+
+ public static void p() {
+ def list = ['ab', 'bc', 'de']
+ list.removeIf(e -> e.startsWith("a"))
+ assert ['bc', 'de'] == list
+ }
}
- }
'''
}
+ @Test
void testPredicateWithoutExplicitTypeDef() {
assertScript '''
- import groovy.transform.CompileStatic
- import java.util.stream.Collectors
- import java.util.stream.Stream
- import java.util.function.Function
- import java.util.function.Predicate
-
- @CompileStatic
- public class Test1 {
- public static void main(String[] args) {
- p()
- }
-
- public static void p() {
- List<String> myList = Arrays.asList("a1", "a2", "b2", "b1", "c2", "c1")
- Predicate<String> predicate = s -> s.startsWith("b")
- Function<String, String> mapper = s -> s.toUpperCase()
-
- List<String> result =
- myList
- .stream()
- .filter(predicate)
- .map(mapper)
- .sorted()
- .collect(Collectors.toList())
-
- assert ['B1', 'B2'] == result
- }
- }
+ import groovy.transform.CompileStatic
+ import java.util.stream.Collectors
+ import java.util.stream.Stream
+ import java.util.function.Function
+ import java.util.function.Predicate
+
+ @CompileStatic
+ public class Test1 {
+ public static void main(String[] args) {
+ p()
+ }
+
+ public static void p() {
+ List<String> myList = Arrays.asList("a1", "a2", "b2", "b1", "c2", "c1")
+ Predicate<String> predicate = s -> s.startsWith("b")
+ Function<String, String> mapper = s -> s.toUpperCase()
+
+ List<String> result =
+ myList
+ .stream()
+ .filter(predicate)
+ .map(mapper)
+ .sorted()
+ .collect(Collectors.toList())
+
+ assert ['B1', 'B2'] == result
+ }
+ }
'''
}
+ @Test
void testUnaryOperator() {
assertScript '''
- import groovy.transform.CompileStatic
- import java.util.stream.Collectors
- import java.util.stream.Stream
-
- @CompileStatic
- public class Test1 {
- public static void main(String[] args) {
- p();
- }
-
- public static void p() {
- def list = [1, 2, 3]
- list.replaceAll(e -> e + 10)
- assert [11, 12, 13] == list
+ import groovy.transform.CompileStatic
+ import java.util.stream.Collectors
+ import java.util.stream.Stream
+
+ @CompileStatic
+ public class Test1 {
+ public static void main(String[] args) {
+ p();
+ }
+
+ public static void p() {
+ def list = [1, 2, 3]
+ list.replaceAll(e -> e + 10)
+ assert [11, 12, 13] == list
+ }
}
- }
'''
}
+ @Test
void testBiConsumer() {
assertScript '''
- import groovy.transform.CompileStatic
- import java.util.stream.Collectors
- import java.util.stream.Stream
-
- @CompileStatic
- public class Test1 {
- public static void main(String[] args) {
- p();
- }
-
- public static void p() {
- def map = [a: 1, b: 2, c: 3]
- map.forEach((k, v) -> System.out.println(k + ":" + v));
+ import groovy.transform.CompileStatic
+ import java.util.stream.Collectors
+ import java.util.stream.Stream
+
+ @CompileStatic
+ public class Test1 {
+ public static void main(String[] args) {
+ p();
+ }
+
+ public static void p() {
+ def map = [a: 1, b: 2, c: 3]
+ map.forEach((k, v) -> System.out.println(k + ":" + v));
+ }
}
- }
'''
}
+ @Test
void testFunctionWithLocalVariables() {
assertScript '''
- import groovy.transform.CompileStatic
- import java.util.stream.Collectors
- import java.util.stream.Stream
-
- @CompileStatic
- public class Test1 {
- public static void main(String[] args) {
- p();
- }
-
- public static void p() {
- String x = "#"
- assert ['#1', '#2', '#3'] == [1, 2, 3].stream().map(e -> x + e).collect(Collectors.toList());
+ import groovy.transform.CompileStatic
+ import java.util.stream.Collectors
+ import java.util.stream.Stream
+
+ @CompileStatic
+ public class Test1 {
+ public static void main(String[] args) {
+ p();
+ }
+
+ public static void p() {
+ String x = "#"
+ assert ['#1', '#2', '#3'] == [1, 2, 3].stream().map(e -> x + e).collect(Collectors.toList());
+ }
}
- }
'''
}
-
+ @Test
void testFunctionWithLocalVariables2() {
assertScript '''
- import groovy.transform.CompileStatic
- import java.util.stream.Collectors
- import java.util.stream.Stream
-
- @CompileStatic
- public class Test1 {
- public static void main(String[] args) {
- new Test1().p();
- }
-
- public void p() {
- String x = "#"
- Integer y = 23
- assert ['23#1', '23#2', '23#3'] == [1, 2, 3].stream().map(e -> '' + y + x + e).collect(Collectors.toList())
+ import groovy.transform.CompileStatic
+ import java.util.stream.Collectors
+ import java.util.stream.Stream
+
+ @CompileStatic
+ public class Test1 {
+ public static void main(String[] args) {
+ new Test1().p();
+ }
+
+ public void p() {
+ String x = "#"
+ Integer y = 23
+ assert ['23#1', '23#2', '23#3'] == [1, 2, 3].stream().map(e -> '' + y + x + e).collect(Collectors.toList())
+ }
}
- }
'''
}
void testFunctionWithLocalVariables4() {
assertScript '''
- import groovy.transform.CompileStatic
- import java.util.stream.Collectors
- import java.util.stream.Stream
-
- @CompileStatic
- public class Test4 {
- public static void main(String[] args) {
- new Test4().p();
- }
-
- public void p() {
- String x = "x";
- StringBuilder y = new StringBuilder("y");
- assert ['yx1', 'yx2', 'yx3'] == [1, 2, 3].stream().map(e -> y + x + e).collect(Collectors.toList());
+ import groovy.transform.CompileStatic
+ import java.util.stream.Collectors
+ import java.util.stream.Stream
+
+ @CompileStatic
+ public class Test4 {
+ public static void main(String[] args) {
+ new Test4().p();
+ }
+
+ public void p() {
+ String x = "x";
+ StringBuilder y = new StringBuilder("y");
+ assert ['yx1', 'yx2', 'yx3'] == [1, 2, 3].stream().map(e -> y + x + e).collect(Collectors.toList());
+ }
}
- }
'''
}
- void testFunctionWithStaticMethodCall() {
+ @Test
+ void testFunctionWithLocalVariables5() {
assertScript '''
- import groovy.transform.CompileStatic
- import java.util.stream.Collectors
- import java.util.stream.Stream
-
- @CompileStatic
- public class Test4 {
- public static void main(String[] args) {
- p();
+ import groovy.transform.CompileStatic
+ import java.util.stream.Collectors
+ import java.util.stream.Stream
+ import java.util.function.Function
+
+ @CompileStatic
+ public class Test1 {
+ public static void main(String[] args) {
+ Function<Integer, String> f = p();
+ assert '#1' == f(1)
+ }
+
+ static Function<Integer, String> p() {
+ String x = "#"
+ Function<Integer, String> f = (Integer e) -> x + e
+ return f
+ }
}
-
- public static void p() {
- String x = "x";
- StringBuilder y = new StringBuilder("y");
- assert ['Hello yx1', 'Hello yx2', 'Hello yx3'] == [1, 2, 3].stream().map(e -> hello() + y + x + e).collect(Collectors.toList());
+ '''
+ }
+
+ @Test
+ void testFunctionWithLocalVariables6() {
+ assertScript '''
+ import groovy.transform.CompileStatic
+ import java.util.stream.Collectors
+ import java.util.stream.Stream
+ import java.util.function.Function
+
+ @CompileStatic
+ public class Test1 {
+ public static void main(String[] args) {
+ Function<Integer, String> f = new Test1().p();
+ assert '#1' == f(1)
+ }
+
+ Function<Integer, String> p() {
+ String x = "#"
+ Function<Integer, String> f = (Integer e) -> x + e
+ return f
+ }
}
-
- public static String hello() {
- return "Hello ";
+ '''
+ }
+
+ @Test
+ void testFunctionWithStaticMethodCall() {
+ assertScript '''
+ import groovy.transform.CompileStatic
+ import java.util.stream.Collectors
+ import java.util.stream.Stream
+
+ @CompileStatic
+ public class Test4 {
+ public static void main(String[] args) {
+ p();
+ }
+
+ public static void p() {
+ String x = "x";
+ StringBuilder y = new StringBuilder("y");
+ assert ['Hello yx1', 'Hello yx2', 'Hello yx3'] == [1, 2, 3].stream().map(e -> hello() + y + x + e).collect(Collectors.toList());
+ }
+
+ public static String hello() {
+ return "Hello ";
+ }
}
- }
'''
}
+ @Test
void testFunctionWithStaticMethodCall2() {
assertScript '''
- import groovy.transform.CompileStatic
- import java.util.stream.Collectors
- import java.util.stream.Stream
-
- @CompileStatic
- public class Test4 {
- public static void main(String[] args) {
- p();
- }
-
- public static void p() {
- String x = "x";
- StringBuilder y = new StringBuilder("y");
- assert ['Hello yx1', 'Hello yx2', 'Hello yx3'] == [1, 2, 3].stream().map(e -> Test4.hello() + y + x + e).collect(Collectors.toList());
- }
-
- public static String hello() {
- return "Hello ";
+ import groovy.transform.CompileStatic
+ import java.util.stream.Collectors
+ import java.util.stream.Stream
+
+ @CompileStatic
+ public class Test4 {
+ public static void main(String[] args) {
+ p();
+ }
+
+ public static void p() {
+ String x = "x";
+ StringBuilder y = new StringBuilder("y");
+ assert ['Hello yx1', 'Hello yx2', 'Hello yx3'] == [1, 2, 3].stream().map(e -> Test4.hello() + y + x + e).collect(Collectors.toList());
+ }
+
+ public static String hello() {
+ return "Hello ";
+ }
}
- }
'''
}
+ @Test
void testFunctionWithInstanceMethodCall() {
assertScript '''
- import groovy.transform.CompileStatic
- import java.util.stream.Collectors
- import java.util.stream.Stream
-
- @CompileStatic
- public class Test4 {
- public static void main(String[] args) {
- new Test4().p();
- }
-
- public void p() {
- assert ['Hello Jochen', 'Hello Daniel'] == ["Jochen", "Daniel"].stream().map(e -> hello() + e).collect(Collectors.toList());
- }
-
- public String hello() {
- return "Hello ";
+ import groovy.transform.CompileStatic
+ import java.util.stream.Collectors
+ import java.util.stream.Stream
+
+ @CompileStatic
+ public class Test4 {
+ public static void main(String[] args) {
+ new Test4().p();
+ }
+
+ public void p() {
+ assert ['Hello Jochen', 'Hello Daniel'] == ["Jochen", "Daniel"].stream().map(e -> hello() + e).collect(Collectors.toList());
+ }
+
+ public String hello() {
+ return "Hello ";
+ }
}
- }
'''
}
+ @Test
void testFunctionInConstructor() {
assertScript '''
- import groovy.transform.CompileStatic
- import java.util.stream.Collectors
- import java.util.stream.Stream
-
- @CompileStatic
- public class Test4 {
- public static void main(String[] args) {
- new Test4();
- }
-
- public Test4() {
- assert ['Hello Jochen', 'Hello Daniel'] == ["Jochen", "Daniel"].stream().map(e -> hello() + e).collect(Collectors.toList());
- }
-
- public String hello() {
- return "Hello ";
+ import groovy.transform.CompileStatic
+ import java.util.stream.Collectors
+ import java.util.stream.Stream
+
+ @CompileStatic
+ public class Test4 {
+ public static void main(String[] args) {
+ new Test4();
+ }
+
+ public Test4() {
+ assert ['Hello Jochen', 'Hello Daniel'] == ["Jochen", "Daniel"].stream().map(e -> hello() + e).collect(Collectors.toList());
+ }
+
+ public String hello() {
+ return "Hello ";
+ }
}
- }
'''
}
+ @Test
void testFunctionWithInstanceMethodCall2() {
assertScript '''
- import groovy.transform.CompileStatic
- import java.util.stream.Collectors
- import java.util.stream.Stream
-
- @CompileStatic
- public class Test4 {
- public static void main(String[] args) {
- new Test4().p();
- }
-
- public void p() {
- assert ['Hello Jochen', 'Hello Daniel'] == ["Jochen", "Daniel"].stream().map(e -> this.hello() + e).collect(Collectors.toList());
- }
-
- public String hello() {
- return "Hello ";
+ import groovy.transform.CompileStatic
+ import java.util.stream.Collectors
+ import java.util.stream.Stream
+
+ @CompileStatic
+ public class Test4 {
+ public static void main(String[] args) {
+ new Test4().p();
+ }
+
+ public void p() {
+ assert ['Hello Jochen', 'Hello Daniel'] == ["Jochen", "Daniel"].stream().map(e -> this.hello() + e).collect(Collectors.toList());
+ }
+
+ public String hello() {
+ return "Hello ";
+ }
}
- }
'''
}
+ @Test
void testFunctionWithInstanceMethodCall3() {
assertScript '''
- import groovy.transform.CompileStatic
- import java.util.stream.Collectors
- import java.util.stream.Stream
-
- @CompileStatic
- public class Test4 {
- public static void main(String[] args) {
- new Test4().p();
- }
-
- public void p() {
- assert ['Hello Jochen', 'Hello Daniel'] == ["Jochen", "Daniel"].stream().map(e -> hello(e)).collect(Collectors.toList());
- }
-
- public String hello(String name) {
- return "Hello $name";
+ import groovy.transform.CompileStatic
+ import java.util.stream.Collectors
+ import java.util.stream.Stream
+
+ @CompileStatic
+ public class Test4 {
+ public static void main(String[] args) {
+ new Test4().p();
+ }
+
+ public void p() {
+ assert ['Hello Jochen', 'Hello Daniel'] == ["Jochen", "Daniel"].stream().map(e -> hello(e)).collect(Collectors.toList());
+ }
+
+ public String hello(String name) {
+ return "Hello $name";
+ }
}
- }
'''
}
+ @Test
void testFunctionCall() {
assertScript '''
- import groovy.transform.CompileStatic
- import java.util.stream.Collectors
- import java.util.stream.Stream
- import java.util.function.Function
-
- @CompileStatic
- public class Test1 {
- public static void main(String[] args) {
- p();
- }
-
- public static void p() {
- Function<Integer, Integer> f = (Integer e) -> (Integer) (e + 1)
- assert 2 == f(1)
+ import groovy.transform.CompileStatic
+ import java.util.stream.Collectors
+ import java.util.stream.Stream
+ import java.util.function.Function
+
+ @CompileStatic
+ public class Test1 {
+ public static void main(String[] args) {
+ p();
+ }
+
+ public static void p() {
+ Function<Integer, Integer> f = (Integer e) -> (Integer) (e + 1)
+ assert 2 == f(1)
+ }
}
- }
'''
}
+ @Test
void testFunctionCallWithoutExplicitTypeDef() {
assertScript '''
- import groovy.transform.CompileStatic
- import java.util.stream.Collectors
- import java.util.stream.Stream
- import java.util.function.Function
-
- @CompileStatic
- public class Test1 {
- public static void main(String[] args) {
- p();
- }
-
- public static void p() {
- Function<Integer, Integer> f = e -> e + 1
- assert 2 == f(1)
+ import groovy.transform.CompileStatic
+ import java.util.stream.Collectors
+ import java.util.stream.Stream
+ import java.util.function.Function
+
+ @CompileStatic
+ public class Test1 {
+ public static void main(String[] args) {
+ p();
+ }
+
+ public static void p() {
+ Function<Integer, Integer> f = e -> e + 1
+ assert 2 == f(1)
+ }
}
- }
'''
}
+ @Test
void testFunctionCall2() {
assertScript '''
- import groovy.transform.CompileStatic
- import java.util.stream.Collectors
- import java.util.stream.Stream
- import java.util.function.Function
-
- @CompileStatic
- public class Test1 {
- public static void main(String[] args) {
- new Test1().p();
- }
-
- public void p() {
- Function<Integer, Integer> f = (Integer e) -> (Integer) (e + 1)
- assert 2 == f(1)
+ import groovy.transform.CompileStatic
+ import java.util.stream.Collectors
+ import java.util.stream.Stream
+ import java.util.function.Function
+
+ @CompileStatic
+ public class Test1 {
+ public static void main(String[] args) {
+ new Test1().p();
+ }
+
+ public void p() {
+ Function<Integer, Integer> f = (Integer e) -> (Integer) (e + 1)
+ assert 2 == f(1)
+ }
}
- }
'''
}
+ @Test
void testFunctionCall3() {
assertScript '''
- import groovy.transform.CompileStatic
- import java.util.stream.Collectors
- import java.util.stream.Stream
- import java.util.function.Function
-
- @CompileStatic
- public class Test1 {
- public static void main(String[] args) {
- p();
- }
-
- public static void p() {
- Function<Integer, Integer> f = (Integer e) -> (Integer) (e + 1)
- assert 2 == f.apply(1)
+ import groovy.transform.CompileStatic
+ import java.util.stream.Collectors
+ import java.util.stream.Stream
+ import java.util.function.Function
+
+ @CompileStatic
+ public class Test1 {
+ public static void main(String[] args) {
+ p();
+ }
+
+ public static void p() {
+ Function<Integer, Integer> f = (Integer e) -> (Integer) (e + 1)
+ assert 2 == f.apply(1)
+ }
}
- }
'''
}
+ @Test
void testConsumerCall() {
assertScript '''
- import groovy.transform.CompileStatic
- import java.util.stream.Collectors
- import java.util.stream.Stream
- import java.util.function.Consumer
-
- @CompileStatic
- public class Test1 {
- public static void main(String[] args) {
- p();
- }
-
- public static void p() {
- int r = 1
- Consumer<Integer> c = (Integer e) -> { r += e }
- c(2)
- assert 3 == r
+ import groovy.transform.CompileStatic
+ import java.util.stream.Collectors
+ import java.util.stream.Stream
+ import java.util.function.Consumer
+
+ @CompileStatic
+ public class Test1 {
+ public static void main(String[] args) {
+ p();
+ }
+
+ public static void p() {
+ int r = 1
+ Consumer<Integer> c = (Integer e) -> { r += e }
+ c(2)
+ assert 3 == r
+ }
}
- }
'''
}
+ @Test
void testConsumerCallWithoutExplicitTypeDef() {
assertScript '''
- import groovy.transform.CompileStatic
- import java.util.stream.Collectors
- import java.util.stream.Stream
- import java.util.function.Consumer
-
- @CompileStatic
- public class Test1 {
- public static void main(String[] args) {
- p();
- }
-
- public static void p() {
- int r = 1
- Consumer<Integer> c = e -> { r += e }
- c(2)
- assert 3 == r
+ import groovy.transform.CompileStatic
+ import java.util.stream.Collectors
+ import java.util.stream.Stream
+ import java.util.function.Consumer
+
+ @CompileStatic
+ public class Test1 {
+ public static void main(String[] args) {
+ p();
+ }
+
+ public static void p() {
+ int r = 1
+ Consumer<Integer> c = e -> { r += e }
+ c(2)
+ assert 3 == r
+ }
}
- }
'''
}
+ @Test
void testConsumerCall2() {
assertScript '''
- import groovy.transform.CompileStatic
- import java.util.stream.Collectors
- import java.util.stream.Stream
- import java.util.function.Consumer
-
- @CompileStatic
- public class Test1 {
- public static void main(String[] args) {
- new Test1().p();
- }
-
- public void p() {
- int r = 1
- Consumer<Integer> c = (Integer e) -> { r += e }
- c(2)
- assert 3 == r
+ import groovy.transform.CompileStatic
+ import java.util.stream.Collectors
+ import java.util.stream.Stream
+ import java.util.function.Consumer
+
+ @CompileStatic
+ public class Test1 {
+ public static void main(String[] args) {
+ new Test1().p();
+ }
+
+ public void p() {
+ int r = 1
+ Consumer<Integer> c = (Integer e) -> { r += e }
+ c(2)
+ assert 3 == r
+ }
}
- }
'''
}
+ @Test
void testConsumerCall3() {
assertScript '''
- import groovy.transform.CompileStatic
- import java.util.stream.Collectors
- import java.util.stream.Stream
- import java.util.function.Consumer
-
- @CompileStatic
- public class Test1 {
- public static void main(String[] args) {
- p();
- }
-
- public static void p() {
- int r = 1
- Consumer<Integer> c = (Integer e) -> { r += e }
- c.accept(2)
- assert 3 == r
- }
- }
- '''
- }
+ import groovy.transform.CompileStatic
+ import java.util.stream.Collectors
+ import java.util.stream.Stream
+ import java.util.function.Consumer
- void testSamCall() {
+ @CompileStatic
+ public class Test1 {
+ public static void main(String[] args) {
+ p();
+ }
+
+ public static void p() {
+ int r = 1
+ Consumer<Integer> c = (Integer e) -> { r += e }
+ c.accept(2)
+ assert 3 == r
+ }
+ }
+ '''
+ }
+
+ @Test
+ void testSamCall() {
assertScript '''
- import groovy.transform.CompileStatic
- import java.util.stream.Collectors
- import java.util.stream.Stream
-
- @CompileStatic
- public class Test1 {
- public static void main(String[] args) {
- p();
+ import groovy.transform.CompileStatic
+ import java.util.stream.Collectors
+ import java.util.stream.Stream
+
+ @CompileStatic
+ public class Test1 {
+ public static void main(String[] args) {
+ p();
+ }
+
+ public static void p() {
+ SamCallable c = (int e) -> e
+ assert 1 == c(1)
+ }
}
-
- public static void p() {
- SamCallable c = (int e) -> e
- assert 1 == c(1)
+
+ @CompileStatic
+ interface SamCallable {
+ int call(int p);
}
- }
-
- @CompileStatic
- interface SamCallable {
- int call(int p);
- }
'''
}
-
+ @Test
void testSamCallWithoutExplicitTypeDef() {
assertScript '''
- import groovy.transform.CompileStatic
- import java.util.stream.Collectors
- import java.util.stream.Stream
-
- @CompileStatic
- public class Test1 {
- public static void main(String[] args) {
- p();
+ import groovy.transform.CompileStatic
+ import java.util.stream.Collectors
+ import java.util.stream.Stream
+
+ @CompileStatic
+ public class Test1 {
+ public static void main(String[] args) {
+ p();
+ }
+
+ public static void p() {
+ SamCallable c = e -> e
+ assert 1 == c(1)
+ }
}
-
- public static void p() {
- SamCallable c = e -> e
- assert 1 == c(1)
+
+ @CompileStatic
+ interface SamCallable {
+ int call(int p);
}
- }
-
- @CompileStatic
- interface SamCallable {
- int call(int p);
- }
'''
}
+ @Test
void testSamCall2() {
assertScript '''
- import groovy.transform.CompileStatic
- import java.util.stream.Collectors
- import java.util.stream.Stream
-
- @CompileStatic
- public class Test1 {
- public static void main(String[] args) {
- p();
+ import groovy.transform.CompileStatic
+ import java.util.stream.Collectors
+ import java.util.stream.Stream
+
+ @CompileStatic
+ public class Test1 {
+ public static void main(String[] args) {
+ p();
+ }
+
+ public static void p() {
+ SamCallable c = (int e) -> e // This is actually a closure(not a native lambda), because "Functional interface SamCallable is not an interface"
+ assert 1 == c(1)
+ }
}
-
- public static void p() {
- SamCallable c = (int e) -> e // This is actually a closure(not a native lambda), because "Functional interface SamCallable is not an interface"
- assert 1 == c(1)
+
+ @CompileStatic
+ abstract class SamCallable {
+ abstract int call(int p);
}
- }
-
- @CompileStatic
- abstract class SamCallable {
- abstract int call(int p);
- }
'''
}
+ @Test
void testFunctionWithUpdatingLocalVariable() {
assertScript '''
- import groovy.transform.CompileStatic
- import java.util.stream.Collectors
- import java.util.stream.Stream
-
- @CompileStatic
- public class Test1 {
- public static void main(String[] args) {
- p();
- }
-
- public static void p() {
- int i = 1
- assert [2, 4, 7] == [1, 2, 3].stream().map(e -> i += e).collect(Collectors.toList())
- assert 7 == i
+ import groovy.transform.CompileStatic
+ import java.util.stream.Collectors
+ import java.util.stream.Stream
+
+ @CompileStatic
+ public class Test1 {
+ public static void main(String[] args) {
+ p();
+ }
+
+ public static void p() {
+ int i = 1
+ assert [2, 4, 7] == [1, 2, 3].stream().map(e -> i += e).collect(Collectors.toList())
+ assert 7 == i
+ }
}
- }
'''
}
+ @Test
void testFunctionWithUpdatingLocalVariable2() {
assertScript '''
- import groovy.transform.CompileStatic
- import java.util.stream.Collectors
- import java.util.stream.Stream
-
- @CompileStatic
- public class Test1 {
- public static void main(String[] args) {
- new Test1().p();
- }
-
- public void p() {
- int i = 1
- assert [2, 4, 7] == [1, 2, 3].stream().map(e -> i += e).collect(Collectors.toList())
- assert 7 == i
+ import groovy.transform.CompileStatic
+ import java.util.stream.Collectors
+ import java.util.stream.Stream
+
+ @CompileStatic
+ public class Test1 {
+ public static void main(String[] args) {
+ new Test1().p();
+ }
+
+ public void p() {
+ int i = 1
+ assert [2, 4, 7] == [1, 2, 3].stream().map(e -> i += e).collect(Collectors.toList())
+ assert 7 == i
+ }
}
- }
'''
}
+ @Test
void testFunctionWithVariableDeclaration() {
assertScript '''
- import groovy.transform.CompileStatic
- import java.util.stream.Collectors
- import java.util.stream.Stream
- import java.util.function.Function
-
- @CompileStatic
- public class Test1 {
- public static void main(String[] args) {
- p()
- }
-
- public static void p() {
- Function<Integer, String> f = (Integer e) -> 'a' + e
- assert ['a1', 'a2', 'a3'] == [1, 2, 3].stream().map(f).collect(Collectors.toList())
+ import groovy.transform.CompileStatic
+ import java.util.stream.Collectors
+ import java.util.stream.Stream
+ import java.util.function.Function
+
+ @CompileStatic
+ public class Test1 {
+ public static void main(String[] args) {
+ p()
+ }
+
+ public static void p() {
+ Function<Integer, String> f = (Integer e) -> 'a' + e
+ assert ['a1', 'a2', 'a3'] == [1, 2, 3].stream().map(f).collect(Collectors.toList())
+ }
}
- }
-
'''
}
+ @Test
void testFunctionWithMixingVariableDeclarationAndMethodInvocation() {
assertScript '''
- import groovy.transform.CompileStatic
- import java.util.stream.Collectors
- import java.util.stream.Stream
- import java.util.function.Function
-
- @CompileStatic
- public class Test1 {
- public static void main(String[] args) {
- p()
- }
-
- public static void p() {
- String x = "#"
- Integer y = 23
- assert ['23#1', '23#2', '23#3'] == [1, 2, 3].stream().map(e -> '' + y + x + e).collect(Collectors.toList())
-
- Function<Integer, String> f = (Integer e) -> 'a' + e
- assert ['a1', 'a2', 'a3'] == [1, 2, 3].stream().map(f).collect(Collectors.toList())
-
- assert [2, 3, 4] == [1, 2, 3].stream().map(e -> e.plus 1).collect(Collectors.toList());
+ import groovy.transform.CompileStatic
+ import java.util.stream.Collectors
+ import java.util.stream.Stream
+ import java.util.function.Function
+
+ @CompileStatic
+ public class Test1 {
+ public static void main(String[] args) {
+ p()
+ }
+
+ public static void p() {
+ String x = "#"
+ Integer y = 23
+ assert ['23#1', '23#2', '23#3'] == [1, 2, 3].stream().map(e -> '' + y + x + e).collect(Collectors.toList())
+
+ Function<Integer, String> f = (Integer e) -> 'a' + e
+ assert ['a1', 'a2', 'a3'] == [1, 2, 3].stream().map(f).collect(Collectors.toList())
+
+ assert [2, 3, 4] == [1, 2, 3].stream().map(e -> e.plus 1).collect(Collectors.toList());
+ }
}
- }
-
'''
}
+ @Test
void testFunctionWithNestedLambda() {
assertScript '''
- import groovy.transform.CompileStatic
- import java.util.stream.Collectors
- import java.util.stream.Stream
-
- @CompileStatic
- public class Test1 {
- public static void main(String[] args) {
- p()
- }
-
- public static void p() {
- [1, 2].stream().forEach(e -> {
- def list = ['a', 'b'].stream().map(f -> f + e).toList()
- if (1 == e) {
- assert ['a1', 'b1'] == list
- } else if (2 == e) {
- assert ['a2', 'b2'] == list
- }
- })
-
+ import groovy.transform.CompileStatic
+ import java.util.stream.Collectors
+ import java.util.stream.Stream
+
+ @CompileStatic
+ public class Test1 {
+ public static void main(String[] args) {
+ p()
+ }
+
+ public static void p() {
+ [1, 2].stream().forEach(e -> {
+ def list = ['a', 'b'].stream().map(f -> f + e).toList()
+ if (1 == e) {
+ assert ['a1', 'b1'] == list
+ } else if (2 == e) {
+ assert ['a2', 'b2'] == list
+ }
+ })
+ }
}
- }
'''
}
+ @Test
void testFunctionWithNestedLambda2() {
assertScript '''
- import groovy.transform.CompileStatic
- import java.util.stream.Collectors
- import java.util.stream.Stream
-
- @CompileStatic
- public class Test1 {
- public static void main(String[] args) {
- p()
- }
-
- public static void p() {
- def list = ['a', 'b'].stream()
- .map(e -> {
- [1, 2].stream().map(f -> e + f).toList()
- }).toList()
-
- assert ['a1', 'a2'] == list[0]
- assert ['b1', 'b2'] == list[1]
+ import groovy.transform.CompileStatic
+ import java.util.stream.Collectors
+ import java.util.stream.Stream
+
+ @CompileStatic
+ public class Test1 {
+ public static void main(String[] args) {
+ p()
+ }
+
+ public static void p() {
+ def list = ['a', 'b'].stream()
+ .map(e -> {
+ [1, 2].stream().map(f -> e + f).toList()
+ }).toList()
+
+ assert ['a1', 'a2'] == list[0]
+ assert ['b1', 'b2'] == list[1]
+ }
}
- }
'''
}
+ @Test
void testFunctionWithNestedLambda3() {
assertScript '''
- import groovy.transform.CompileStatic
- import java.util.stream.Collectors
- import java.util.stream.Stream
- import java.util.function.Function
-
- @CompileStatic
- public class Test1 {
- public static void main(String[] args) {
- p()
- }
-
- public static void p() {
- def list = ['a', 'b'].stream()
- .map(e -> {
- Function<Integer, String> x = (Integer f) -> e + f
- [1, 2].stream().map(x).toList()
- }).toList()
-
- assert ['a1', 'a2'] == list[0]
- assert ['b1', 'b2'] == list[1]
+ import groovy.transform.CompileStatic
+ import java.util.stream.Collectors
+ import java.util.stream.Stream
+ import java.util.function.Function
+
+ @CompileStatic
+ public class Test1 {
+ public static void main(String[] args) {
+ p()
+ }
+
+ public static void p() {
+ def list = ['a', 'b'].stream()
+ .map(e -> {
+ Function<Integer, String> x = (Integer f) -> e + f
+ [1, 2].stream().map(x).toList()
+ }).toList()
+
+ assert ['a1', 'a2'] == list[0]
+ assert ['b1', 'b2'] == list[1]
+ }
}
- }
'''
}
+ @Test
void testMixingLambdaAndMethodReference() {
assertScript '''
import java.util.stream.Collectors
-
+
@groovy.transform.CompileStatic
void p() {
assert ['1', '2', '3'] == [1, 2, 3].stream().map(Object::toString).collect(Collectors.toList())
assert [2, 3, 4] == [1, 2, 3].stream().map(e -> e.plus 1).collect(Collectors.toList())
assert ['1', '2', '3'] == [1, 2, 3].stream().map(Object::toString).collect(Collectors.toList())
}
-
+
p()
'''
}
+ @Test
void testInitializeBlocks() {
assertScript '''
import java.util.stream.Collectors
-
+
@groovy.transform.CompileStatic
class Test1 {
static sl
def il
static { sl = [1, 2, 3].stream().map(e -> e + 1).toList() }
-
+
{
il = [1, 2, 3].stream().map(e -> e + 2).toList()
}
}
-
+
assert [2, 3, 4] == Test1.sl
assert [3, 4, 5] == new Test1().il
'''
}
+ @Test @NotYetImplemented
void testNestedLambdaAccessingInstanceFields() {
assertScript '''
@groovy.transform.CompileStatic
@@ -943,689 +1030,797 @@ class LambdaTest extends GroovyTestCase {
['abc', 'def', 'ghi'].stream().filter(e -> strListHolder.strList.stream().anyMatch(c -> e.contains(c + b))).toList()
}
}
-
+
assert ['abc'] == new Test1().p()
assert ['abc'] == new Test1().p2()
'''
}
+ @Test // GROOVY-9332
+ void testStaticInitializeBlocks() {
+ assertScript '''
+ @groovy.transform.CompileStatic
+ class Test1 {
+ static list
+ static final int one = 1
+ static { list = [1, 2, 3].stream().map(e -> e + one).toList() }
+ }
+
+ assert [2, 3, 4] == Test1.list
+ '''
+ }
+
+ @Test @NotYetImplemented
+ void testStaticInitializeBlocks2() {
+ assertScript '''
+ @groovy.transform.CompileStatic
+ class Test1 {
+ static list
+ static int acc = 1
+ static { list = [1, 2, 3].stream().map(e -> acc += e).toList() }
+ }
+
+ assert [2, 4, 7] == Test1.list
+ assert 7 == Test1.acc
+ '''
+ }
+
+ @Test
+ void testAccessingThis() {
+ assertScript '''
+ import groovy.transform.CompileStatic
+ import java.util.function.Predicate
+
+ @CompileStatic
+ public class Test4 {
+ public static void main(String[] args) {
+ new Test4().p();
+ }
+
+ public Test4 thisVar = this;
+
+ public void p() {
+ Predicate<Test4> predicate = (Test4 s) -> { this === s }
+
+ assert predicate.test(thisVar)
+ }
+ }
+ '''
+ }
+
+ @Test
+ void testAccessingThis2() {
+ assertScript '''
+ import groovy.transform.CompileStatic
+
+ @CompileStatic
+ public class Test4 {
+ public static void main(String[] args) {
+ new Test4().p();
+ }
+
+ public String a = "a";
+
+ public void p() {
+ assert ['a1', 'a2'] == [1, 2].stream().map(e -> this.a + e).toList();
+ }
+ }
+ '''
+ }
+
+ @Test
+ void testAccessingThis3() {
+ assertScript '''
+ import groovy.transform.CompileStatic
+
+ @CompileStatic
+ public class Test4 {
+ public static void main(String[] args) {
+ new Test4().p();
+ }
+
+ public String a() { "a" }
+
+ public void p() {
+ assert ['a1', 'a2'] == [1, 2].stream().map(e -> this.a() + e).toList();
+ }
+ }
+ '''
+ }
+
+ @Test
void testSerialize() {
assertScript '''
- import java.util.function.Function
-
- interface SerializableFunction<T, R> extends Function<T, R>, Serializable {}
-
- @groovy.transform.CompileStatic
- class Test1 {
- def p() {
- def out = new ByteArrayOutputStream()
- out.withObjectOutputStream {
- SerializableFunction<Integer, String> f = ((Integer e) -> 'a' + e)
- it.writeObject(f)
- }
-
- return out.toByteArray()
+ import java.util.function.Function
+
+ interface SerializableFunction<T, R> extends Function<T, R>, Serializable {}
+
+ @groovy.transform.CompileStatic
+ class Test1 {
+ def p() {
+ def out = new ByteArrayOutputStream()
+ out.withObjectOutputStream {
+ SerializableFunction<Integer, String> f = ((Integer e) -> 'a' + e)
+ it.writeObject(f)
+ }
+
+ return out.toByteArray()
+ }
}
- }
- assert new Test1().p().length > 0
+ assert new Test1().p().length > 0
'''
}
+ @Test
void testSerializeFailed() {
- def errMsg = shouldFail(NotSerializableException, '''
- import java.util.function.Function
-
- @groovy.transform.CompileStatic
- class Test1 {
- def p() {
- def out = new ByteArrayOutputStream()
- out.withObjectOutputStream {
- Function<Integer, String> f = ((Integer e) -> 'a' + e)
- it.writeObject(f)
- }
-
- return out.toByteArray()
+ def err = shouldFail NotSerializableException, '''
+ import java.util.function.Function
+
+ @groovy.transform.CompileStatic
+ class Test1 {
+ def p() {
+ def out = new ByteArrayOutputStream()
+ out.withObjectOutputStream {
+ Function<Integer, String> f = ((Integer e) -> 'a' + e)
+ it.writeObject(f)
+ }
+
+ return out.toByteArray()
+ }
}
- }
- new Test1().p()
- ''')
+ new Test1().p()
+ '''
- assert errMsg.contains('$Lambda$')
+ assert err.message.contains('$Lambda$')
}
+ @Test
void testDeserialize() {
assertScript '''
- package tests.lambda
- import java.util.function.Function
-
- @groovy.transform.CompileStatic
- class Test1 {
- byte[] p() {
- def out = new ByteArrayOutputStream()
- out.withObjectOutputStream {
- SerializableFunction<Integer, String> f = ((Integer e) -> 'a' + e)
- it.writeObject(f)
+ package tests.lambda
+ import java.util.function.Function
+
+ @groovy.transform.CompileStatic
+ class Test1 {
+ byte[] p() {
+ def out = new ByteArrayOutputStream()
+ out.withObjectOutputStream {
+ SerializableFunction<Integer, String> f = ((Integer e) -> 'a' + e)
+ it.writeObject(f)
+ }
+
+ return out.toByteArray()
+ }
+
+ static void main(String[] args) {
+ new ByteArrayInputStream(new Test1().p()).withObjectInputStream(Test1.class.classLoader) {
+ SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
+ assert 'a1' == f.apply(1)
}
-
- return out.toByteArray()
- }
-
- static void main(String[] args) {
- new ByteArrayInputStream(new Test1().p()).withObjectInputStream(Test1.class.classLoader) {
- SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
- assert 'a1' == f.apply(1)
}
+
+ interface SerializableFunction<T, R> extends Function<T, R>, Serializable {}
}
-
- interface SerializableFunction<T, R> extends Function<T, R>, Serializable {}
- }
'''
}
+ @Test
void testDeserializeLambdaInInitializeBlock() {
assertScript '''
package tests.lambda
import java.util.function.Function
-
+
@groovy.transform.CompileStatic
class Test1 implements Serializable {
private static final long serialVersionUID = -1L;
String a = 'a'
SerializableFunction<Integer, String> f
-
+
{
f = ((Integer e) -> a + e)
}
-
+
byte[] p() {
def out = new ByteArrayOutputStream()
out.withObjectOutputStream {
it.writeObject(f)
}
-
+
return out.toByteArray()
}
-
+
static void main(String[] args) {
new ByteArrayInputStream(new Test1().p()).withObjectInputStream(Test1.class.classLoader) {
SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
assert 'a1' == f.apply(1)
}
}
-
+
interface SerializableFunction<T, R> extends Function<T, R>, Serializable {}
}
'''
}
+ @Test
void testDeserializeLambdaInInitializeBlockShouldFail() {
- def errMsg = shouldFail(NotSerializableException, '''
+ def err = shouldFail NotSerializableException, '''
package tests.lambda
import java.util.function.Function
-
+
@groovy.transform.CompileStatic
class Test1 {
String a = 'a'
SerializableFunction<Integer, String> f
-
+
{
f = ((Integer e) -> a + e)
}
-
+
byte[] p() {
def out = new ByteArrayOutputStream()
out.withObjectOutputStream {
it.writeObject(f)
}
-
+
return out.toByteArray()
}
-
+
static void main(String[] args) {
new Test1().p()
}
-
+
interface SerializableFunction<T, R> extends Function<T, R>, Serializable {}
}
- ''')
+ '''
- assert errMsg.contains('tests.lambda.Test1')
+ assert err.message.contains('tests.lambda.Test1')
}
-
+ @Test
void testDeserialize2() {
assertScript '''
- import java.util.function.Function
-
- @groovy.transform.CompileStatic
- class Test1 {
- static byte[] p() {
- def out = new ByteArrayOutputStream()
- out.withObjectOutputStream {
- SerializableFunction<Integer, String> f = ((Integer e) -> 'a' + e)
- it.writeObject(f)
+ import java.util.function.Function
+
+ @groovy.transform.CompileStatic
+ class Test1 {
+ static byte[] p() {
+ def out = new ByteArrayOutputStream()
+ out.withObjectOutputStream {
+ SerializableFunction<Integer, String> f = ((Integer e) -> 'a' + e)
+ it.writeObject(f)
+ }
+
+ return out.toByteArray()
+ }
+
+ static void main(String[] args) {
+ new ByteArrayInputStream(Test1.p()).withObjectInputStream(Test1.class.classLoader) {
+ SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
+ assert 'a1' == f.apply(1)
}
-
- return out.toByteArray()
- }
-
- static void main(String[] args) {
- new ByteArrayInputStream(Test1.p()).withObjectInputStream(Test1.class.classLoader) {
- SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
- assert 'a1' == f.apply(1)
}
+
+ interface SerializableFunction<T, R> extends Function<T, R>, Serializable {}
}
-
- interface SerializableFunction<T, R> extends Function<T, R>, Serializable {}
- }
'''
}
+ @Test
void testDeserialize3() {
assertScript '''
- package tests.lambda
- import java.util.function.Function
-
- @groovy.transform.CompileStatic
- class Test1 {
- byte[] p() {
- def out = new ByteArrayOutputStream()
- out.withObjectOutputStream {
- String c = 'a'
- SerializableFunction<Integer, String> f = (Integer e) -> c + e
- it.writeObject(f)
+ package tests.lambda
+ import java.util.function.Function
+
+ @groovy.transform.CompileStatic
+ class Test1 {
+ byte[] p() {
+ def out = new ByteArrayOutputStream()
+ out.withObjectOutputStream {
+ String c = 'a'
+ SerializableFunction<Integer, String> f = (Integer e) -> c + e
+ it.writeObject(f)
+ }
+
+ return out.toByteArray()
+ }
+
+ static void main(String[] args) {
+ new ByteArrayInputStream(new Test1().p()).withObjectInputStream(Test1.class.classLoader) {
+ SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
+ assert 'a1' == f.apply(1)
}
-
- return out.toByteArray()
- }
-
- static void main(String[] args) {
- new ByteArrayInputStream(new Test1().p()).withObjectInputStream(Test1.class.classLoader) {
- SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
- assert 'a1' == f.apply(1)
}
+
+ interface SerializableFunction<T, R> extends Function<T, R>, Serializable {}
}
-
- interface SerializableFunction<T, R> extends Function<T, R>, Serializable {}
- }
'''
}
+ @Test
void testDeserialize4() {
assertScript '''
- package tests.lambda
- import java.util.function.Function
-
- @groovy.transform.CompileStatic
- class Test1 {
- byte[] p() {
- def out = new ByteArrayOutputStream()
- String c = 'a'
- SerializableFunction<Integer, String> f = (Integer e) -> c + e
- out.withObjectOutputStream {
- it.writeObject(f)
- }
-
- return out.toByteArray()
- }
-
- static void main(String[] args) {
- new ByteArrayInputStream(new Test1().p()).withObjectInputStream(Test1.class.classLoader) {
- SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
- assert 'a1' == f.apply(1)
- }
- }
-
- interface SerializableFunction<T, R> extends Function<T, R>, Serializable {}
- }
+ package tests.lambda
+ import java.util.function.Function
+
+ @groovy.transform.CompileStatic
+ class Test1 {
+ byte[] p() {
+ def out = new ByteArrayOutputStream()
+ String c = 'a'
+ SerializableFunction<Integer, String> f = (Integer e) -> c + e
+ out.withObjectOutputStream {
+ it.writeObject(f)
+ }
+
+ return out.toByteArray()
+ }
+
+ static void main(String[] args) {
+ new ByteArrayInputStream(new Test1().p()).withObjectInputStream(Test1.class.classLoader) {
+ SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
+ assert 'a1' == f.apply(1)
+ }
+ }
+
+ interface SerializableFunction<T, R> extends Function<T, R>, Serializable {}
+ }
'''
}
+ @Test
void testDeserialize5() {
assertScript '''
- package tests.lambda
- import java.util.function.Function
-
- @groovy.transform.CompileStatic
- class Test1 {
- static byte[] p() {
- def out = new ByteArrayOutputStream()
- String c = 'a'
- SerializableFunction<Integer, String> f = (Integer e) -> c + e
- out.withObjectOutputStream {
- it.writeObject(f)
+ package tests.lambda
+ import java.util.function.Function
+
+ @groovy.transform.CompileStatic
+ class Test1 {
+ static byte[] p() {
+ def out = new ByteArrayOutputStream()
+ String c = 'a'
+ SerializableFunction<Integer, String> f = (Integer e) -> c + e
+ out.withObjectOutputStream {
+ it.writeObject(f)
+ }
+
+ return out.toByteArray()
+ }
+
+ static void main(String[] args) {
+ new ByteArrayInputStream(Test1.p()).withObjectInputStream(Test1.class.classLoader) {
+ SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
+ assert 'a1' == f.apply(1)
}
-
- return out.toByteArray()
- }
-
- static void main(String[] args) {
- new ByteArrayInputStream(Test1.p()).withObjectInputStream(Test1.class.classLoader) {
- SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
- assert 'a1' == f.apply(1)
}
+
+ interface SerializableFunction<T, R> extends Function<T, R>, Serializable {}
}
-
- interface SerializableFunction<T, R> extends Function<T, R>, Serializable {}
- }
'''
}
+ @Test
void testDeserialize6InstanceFields() {
assertScript '''
- package tests.lambda
- import java.util.function.Function
-
- @groovy.transform.CompileStatic
- class Test1 implements Serializable {
- private static final long serialVersionUID = -1L;
- private String c = 'a'
-
- byte[] p() {
- def out = new ByteArrayOutputStream()
- SerializableFunction<Integer, String> f = (Integer e) -> c + e
- out.withObjectOutputStream {
- it.writeObject(f)
+ package tests.lambda
+ import java.util.function.Function
+
+ @groovy.transform.CompileStatic
+ class Test1 implements Serializable {
+ private static final long serialVersionUID = -1L;
+ private String c = 'a'
+
+ byte[] p() {
+ def out = new ByteArrayOutputStream()
+ SerializableFunction<Integer, String> f = (Integer e) -> c + e
+ out.withObjectOutputStream {
+ it.writeObject(f)
+ }
+
+ return out.toByteArray()
+ }
+
+ static void main(String[] args) {
+ new ByteArrayInputStream(new Test1().p()).withObjectInputStream(Test1.class.classLoader) {
+ SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
+ assert 'a1' == f.apply(1)
}
-
- return out.toByteArray()
- }
-
- static void main(String[] args) {
- new ByteArrayInputStream(new Test1().p()).withObjectInputStream(Test1.class.classLoader) {
- SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
- assert 'a1' == f.apply(1)
}
+
+ interface SerializableFunction<T, R> extends Function<T, R>, Serializable {}
}
-
- interface SerializableFunction<T, R> extends Function<T, R>, Serializable {}
- }
'''
}
+ @Test
void testDeserialize6InstanceFieldsShouldFail() {
- def errMsg = shouldFail(NotSerializableException, '''
- package tests.lambda
- import java.util.function.Function
-
- @groovy.transform.CompileStatic
- class Test1 {
- private String c = 'a'
-
- byte[] p() {
- def out = new ByteArrayOutputStream()
- SerializableFunction<Integer, String> f = (Integer e) -> c + e
- out.withObjectOutputStream {
- it.writeObject(f)
+ def err = shouldFail NotSerializableException, '''
+ package tests.lambda
+ import java.util.function.Function
+
+ @groovy.transform.CompileStatic
+ class Test1 {
+ private String c = 'a'
+
+ byte[] p() {
+ def out = new ByteArrayOutputStream()
+ SerializableFunction<Integer, String> f = (Integer e) -> c + e
+ out.withObjectOutputStream {
+ it.writeObject(f)
+ }
+
+ return out.toByteArray()
+ }
+
+ static void main(String[] args) {
+ new ByteArrayInputStream(new Test1().p()).withObjectInputStream(Test1.class.classLoader) {
+ SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
+ assert 'a1' == f.apply(1)
}
-
- return out.toByteArray()
- }
-
- static void main(String[] args) {
- new ByteArrayInputStream(new Test1().p()).withObjectInputStream(Test1.class.classLoader) {
- SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
- assert 'a1' == f.apply(1)
}
+
+ interface SerializableFunction<T, R> extends Function<T, R>, Serializable {}
}
-
- interface SerializableFunction<T, R> extends Function<T, R>, Serializable {}
- }
- ''')
+ '''
- assert errMsg.contains('tests.lambda.Test1')
+ assert err.message.contains('tests.lambda.Test1')
}
+ @Test
void testDeserialize6InstanceMethods() {
assertScript '''
- package tests.lambda
- import java.util.function.Function
-
- @groovy.transform.CompileStatic
- class Test1 implements Serializable {
- private static final long serialVersionUID = -1L;
- private String c() { 'a' }
-
- byte[] p() {
- def out = new ByteArrayOutputStream()
- SerializableFunction<Integer, String> f = (Integer e) -> c() + e
- out.withObjectOutputStream {
- it.writeObject(f)
+ package tests.lambda
+ import java.util.function.Function
+
+ @groovy.transform.CompileStatic
+ class Test1 implements Serializable {
+ private static final long serialVersionUID = -1L;
+ private String c() { 'a' }
+
+ byte[] p() {
+ def out = new ByteArrayOutputStream()
+ SerializableFunction<Integer, String> f = (Integer e) -> c() + e
+ out.withObjectOutputStream {
+ it.writeObject(f)
+ }
+
+ return out.toByteArray()
+ }
+
+ static void main(String[] args) {
+ new ByteArrayInputStream(new Test1().p()).withObjectInputStream(Test1.class.classLoader) {
+ SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
+ assert 'a1' == f.apply(1)
}
-
- return out.toByteArray()
- }
-
- static void main(String[] args) {
- new ByteArrayInputStream(new Test1().p()).withObjectInputStream(Test1.class.classLoader) {
- SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
- assert 'a1' == f.apply(1)
}
+
+ interface SerializableFunction<T, R> extends Function<T, R>, Serializable {}
}
-
- interface SerializableFunction<T, R> extends Function<T, R>, Serializable {}
- }
'''
}
+ @Test
void testDeserialize6InstanceMethodsShouldFail() {
- def errMsg = shouldFail(NotSerializableException, '''
- package tests.lambda
- import java.util.function.Function
-
- @groovy.transform.CompileStatic
- class Test1 {
- private String c() { 'a' }
-
- byte[] p() {
- def out = new ByteArrayOutputStream()
- SerializableFunction<Integer, String> f = (Integer e) -> c() + e
- out.withObjectOutputStream {
- it.writeObject(f)
+ def err = shouldFail NotSerializableException, '''
+ package tests.lambda
+ import java.util.function.Function
+
+ @groovy.transform.CompileStatic
+ class Test1 {
+ private String c() { 'a' }
+
+ byte[] p() {
+ def out = new ByteArrayOutputStream()
+ SerializableFunction<Integer, String> f = (Integer e) -> c() + e
+ out.withObjectOutputStream {
+ it.writeObject(f)
+ }
+
+ return out.toByteArray()
+ }
+
+ static void main(String[] args) {
+ new ByteArrayInputStream(new Test1().p()).withObjectInputStream(Test1.class.classLoader) {
+ SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
+ assert 'a1' == f.apply(1)
}
-
- return out.toByteArray()
- }
-
- static void main(String[] args) {
- new ByteArrayInputStream(new Test1().p()).withObjectInputStream(Test1.class.classLoader) {
- SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
- assert 'a1' == f.apply(1)
}
+
+ interface SerializableFunction<T, R> extends Function<T, R>, Serializable {}
}
-
- interface SerializableFunction<T, R> extends Function<T, R>, Serializable {}
- }
- ''')
+ '''
- assert errMsg.contains('tests.lambda.Test1')
+ assert err.message.contains('tests.lambda.Test1')
}
+ @Test
void testDeserialize7StaticFields() {
assertScript '''
- package tests.lambda
- import java.util.function.Function
-
- @groovy.transform.CompileStatic
- class Test1 {
- private static final String c = 'a'
- static byte[] p() {
- def out = new ByteArrayOutputStream()
- SerializableFunction<Integer, String> f = (Integer e) -> c + e
- out.withObjectOutputStream {
- it.writeObject(f)
+ package tests.lambda
+ import java.util.function.Function
+
+ @groovy.transform.CompileStatic
+ class Test1 {
+ private static final String c = 'a'
+ static byte[] p() {
+ def out = new ByteArrayOutputStream()
+ SerializableFunction<Integer, String> f = (Integer e) -> c + e
+ out.withObjectOutputStream {
+ it.writeObject(f)
+ }
+
+ return out.toByteArray()
+ }
+
+ static void main(String[] args) {
+ new ByteArrayInputStream(Test1.p()).withObjectInputStream(Test1.class.classLoader) {
+ SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
+ assert 'a1' == f.apply(1)
}
-
- return out.toByteArray()
- }
-
- static void main(String[] args) {
- new ByteArrayInputStream(Test1.p()).withObjectInputStream(Test1.class.classLoader) {
- SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
- assert 'a1' == f.apply(1)
}
+
+ interface SerializableFunction<T, R> extends Function<T, R>, Serializable {}
}
-
- interface SerializableFunction<T, R> extends Function<T, R>, Serializable {}
- }
'''
}
-
+ @Test
void testDeserialize7StaticMethods() {
assertScript '''
- package tests.lambda
- import java.util.function.Function
-
- @groovy.transform.CompileStatic
- class Test1 {
- private static String c() { 'a' }
- static byte[] p() {
- def out = new ByteArrayOutputStream()
- SerializableFunction<Integer, String> f = (Integer e) -> c() + e
- out.withObjectOutputStream {
- it.writeObject(f)
+ package tests.lambda
+ import java.util.function.Function
+
+ @groovy.transform.CompileStatic
+ class Test1 {
+ private static String c() { 'a' }
+ static byte[] p() {
+ def out = new ByteArrayOutputStream()
+ SerializableFunction<Integer, String> f = (Integer e) -> c() + e
+ out.withObjectOutputStream {
+ it.writeObject(f)
+ }
+
+ return out.toByteArray()
+ }
+
+ static void main(String[] args) {
+ new ByteArrayInputStream(Test1.p()).withObjectInputStream(Test1.class.classLoader) {
+ SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
+ assert 'a1' == f.apply(1)
}
-
- return out.toByteArray()
- }
-
- static void main(String[] args) {
- new ByteArrayInputStream(Test1.p()).withObjectInputStream(Test1.class.classLoader) {
- SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
- assert 'a1' == f.apply(1)
}
+
+ interface SerializableFunction<T, R> extends Function<T, R>, Serializable {}
}
-
- interface SerializableFunction<T, R> extends Function<T, R>, Serializable {}
- }
'''
}
+ @Test
void testDeserializeNestedLambda() {
assertScript '''
- import java.util.function.Function
-
- interface SerializableFunction<T, R> extends Function<T, R>, Serializable {}
-
- @groovy.transform.CompileStatic
- class Test1 {
- def p() {
- def out1 = new ByteArrayOutputStream()
- SerializableFunction<Integer, String> f1 = (Integer e) -> 'a' + e
- out1.withObjectOutputStream {
- it.writeObject(f1)
- }
- def result1 = out1.toByteArray()
-
- def out2 = new ByteArrayOutputStream()
- SerializableFunction<Integer, String> f2 = (Integer e) -> 'b' + e
- out2.withObjectOutputStream {
- it.writeObject(f2)
- }
- def result2 = out2.toByteArray()
-
- // nested lambda expression
- def out3 = new ByteArrayOutputStream()
- SerializableFunction<Integer, String> f3 = (Integer e) -> {
- SerializableFunction<Integer, String> nf = ((Integer ne) -> 'n' + ne)
- 'c' + nf(e)
- }
- out3.withObjectOutputStream {
- it.writeObject(f3)
- }
- def result3 = out3.toByteArray()
-
- return [result1, result2, result3]
- }
- }
-
- def (byte[] serializedLambdaBytes1, byte[] serializedLambdaBytes2, byte[] serializedLambdaBytes3) = new Test1().p()
- new ByteArrayInputStream(serializedLambdaBytes1).withObjectInputStream(Test1.class.classLoader) {
- SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
- assert 'a1' == f.apply(1)
- }
-
- new ByteArrayInputStream(serializedLambdaBytes2).withObjectInputStream(Test1.class.classLoader) {
- SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
- assert 'b1' == f.apply(1)
- }
-
- new ByteArrayInputStream(serializedLambdaBytes3).withObjectInputStream(Test1.class.classLoader) {
- SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
- assert 'cn1' == f.apply(1)
- }
+ import java.util.function.Function
+
+ interface SerializableFunction<T, R> extends Function<T, R>, Serializable {}
+
+ @groovy.transform.CompileStatic
+ class Test1 {
+ def p() {
+ def out1 = new ByteArrayOutputStream()
+ SerializableFunction<Integer, String> f1 = (Integer e) -> 'a' + e
+ out1.withObjectOutputStream {
+ it.writeObject(f1)
+ }
+ def result1 = out1.toByteArray()
+
+ def out2 = new ByteArrayOutputStream()
+ SerializableFunction<Integer, String> f2 = (Integer e) -> 'b' + e
+ out2.withObjectOutputStream {
+ it.writeObject(f2)
+ }
+ def result2 = out2.toByteArray()
+
+ // nested lambda expression
+ def out3 = new ByteArrayOutputStream()
+ SerializableFunction<Integer, String> f3 = (Integer e) -> {
+ SerializableFunction<Integer, String> nf = ((Integer ne) -> 'n' + ne)
+ 'c' + nf(e)
+ }
+ out3.withObjectOutputStream {
+ it.writeObject(f3)
+ }
+ def result3 = out3.toByteArray()
+
+ return [result1, result2, result3]
+ }
+ }
+
+ def (byte[] serializedLambdaBytes1, byte[] serializedLambdaBytes2, byte[] serializedLambdaBytes3) = new Test1().p()
+ new ByteArrayInputStream(serializedLambdaBytes1).withObjectInputStream(Test1.class.classLoader) {
+ SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
+ assert 'a1' == f.apply(1)
+ }
+
+ new ByteArrayInputStream(serializedLambdaBytes2).withObjectInputStream(Test1.class.classLoader) {
+ SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
+ assert 'b1' == f.apply(1)
+ }
+
+ new ByteArrayInputStream(serializedLambdaBytes3).withObjectInputStream(Test1.class.classLoader) {
+ SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
+ assert 'cn1' == f.apply(1)
+ }
'''
}
+ @Test
void testDeserializeNestedLambda2() {
assertScript '''
- import java.util.function.Function
-
- interface SerializableFunction<T, R> extends Function<T, R>, Serializable {}
-
- @groovy.transform.CompileStatic
- class Test1 {
- def p() {
- def out1 = new ByteArrayOutputStream()
- out1.withObjectOutputStream {
- SerializableFunction<Integer, String> f = ((Integer e) -> 'a' + e)
- it.writeObject(f)
- }
- def result1 = out1.toByteArray()
-
- def out2 = new ByteArrayOutputStream()
- out2.withObjectOutputStream {
- SerializableFunction<Integer, String> f = ((Integer e) -> 'b' + e)
- it.writeObject(f)
- }
- def result2 = out2.toByteArray()
-
- // nested lambda expression
- def out3 = new ByteArrayOutputStream()
- out3.withObjectOutputStream {
- SerializableFunction<Integer, String> f = ((Integer e) -> {
- SerializableFunction<Integer, String> nf = ((Integer ne) -> 'n' + ne)
- 'c' + nf(e)
- })
- it.writeObject(f)
- }
- def result3 = out3.toByteArray()
-
- return [result1, result2, result3]
- }
- }
-
- def (byte[] serializedLambdaBytes1, byte[] serializedLambdaBytes2, byte[] serializedLambdaBytes3) = new Test1().p()
- new ByteArrayInputStream(serializedLambdaBytes1).withObjectInputStream(Test1.class.classLoader) {
- SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
- assert 'a1' == f.apply(1)
- }
-
- new ByteArrayInputStream(serializedLambdaBytes2).withObjectInputStream(Test1.class.classLoader) {
- SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
- assert 'b1' == f.apply(1)
- }
-
- new ByteArrayInputStream(serializedLambdaBytes3).withObjectInputStream(Test1.class.classLoader) {
- SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
- assert 'cn1' == f.apply(1)
- }
+ import java.util.function.Function
+
+ interface SerializableFunction<T, R> extends Function<T, R>, Serializable {}
+
+ @groovy.transform.CompileStatic
+ class Test1 {
+ def p() {
+ def out1 = new ByteArrayOutputStream()
+ out1.withObjectOutputStream {
+ SerializableFunction<Integer, String> f = ((Integer e) -> 'a' + e)
+ it.writeObject(f)
+ }
+ def result1 = out1.toByteArray()
+
+ def out2 = new ByteArrayOutputStream()
+ out2.withObjectOutputStream {
+ SerializableFunction<Integer, String> f = ((Integer e) -> 'b' + e)
+ it.writeObject(f)
+ }
+ def result2 = out2.toByteArray()
+
+ // nested lambda expression
+ def out3 = new ByteArrayOutputStream()
+ out3.withObjectOutputStream {
+ SerializableFunction<Integer, String> f = ((Integer e) -> {
+ SerializableFunction<Integer, String> nf = ((Integer ne) -> 'n' + ne)
+ 'c' + nf(e)
+ })
+ it.writeObject(f)
+ }
+ def result3 = out3.toByteArray()
+
+ return [result1, result2, result3]
+ }
+ }
+
+ def (byte[] serializedLambdaBytes1, byte[] serializedLambdaBytes2, byte[] serializedLambdaBytes3) = new Test1().p()
+ new ByteArrayInputStream(serializedLambdaBytes1).withObjectInputStream(Test1.class.classLoader) {
+ SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
+ assert 'a1' == f.apply(1)
+ }
+
+ new ByteArrayInputStream(serializedLambdaBytes2).withObjectInputStream(Test1.class.classLoader) {
+ SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
+ assert 'b1' == f.apply(1)
+ }
+
+ new ByteArrayInputStream(serializedLambdaBytes3).withObjectInputStream(Test1.class.classLoader) {
+ SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
+ assert 'cn1' == f.apply(1)
+ }
'''
}
+ @Test
void testDeserializeNestedLambda3() {
assertScript '''
- import java.util.function.Function
-
- interface SerializableFunction<T, R> extends Function<T, R>, Serializable {}
-
- @groovy.transform.CompileStatic
- class Test1 {
- static p() {
- def out1 = new ByteArrayOutputStream()
- out1.withObjectOutputStream {
- SerializableFunction<Integer, String> f = ((Integer e) -> 'a' + e)
- it.writeObject(f)
- }
- def result1 = out1.toByteArray()
-
- def out2 = new ByteArrayOutputStream()
- out2.withObjectOutputStream {
- SerializableFunction<Integer, String> f = ((Integer e) -> 'b' + e)
- it.writeObject(f)
- }
- def result2 = out2.toByteArray()
-
- // nested lambda expression
- def out3 = new ByteArrayOutputStream()
- out3.withObjectOutputStream {
- SerializableFunction<Integer, String> f = ((Integer e) -> {
- SerializableFunction<Integer, String> nf = ((Integer ne) -> 'n' + ne)
- 'c' + nf(e)
- })
- it.writeObject(f)
- }
- def result3 = out3.toByteArray()
-
- return [result1, result2, result3]
- }
- }
-
- def (byte[] serializedLambdaBytes1, byte[] serializedLambdaBytes2, byte[] serializedLambdaBytes3) = Test1.p()
- new ByteArrayInputStream(serializedLambdaBytes1).withObjectInputStream(Test1.class.classLoader) {
- SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
- assert 'a1' == f.apply(1)
- }
-
- new ByteArrayInputStream(serializedLambdaBytes2).withObjectInputStream(Test1.class.classLoader) {
- SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
- assert 'b1' == f.apply(1)
- }
-
- new ByteArrayInputStream(serializedLambdaBytes3).withObjectInputStream(Test1.class.classLoader) {
- SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
- assert 'cn1' == f.apply(1)
- }
+ import java.util.function.Function
+
+ interface SerializableFunction<T, R> extends Function<T, R>, Serializable {}
+
+ @groovy.transform.CompileStatic
+ class Test1 {
+ static p() {
+ def out1 = new ByteArrayOutputStream()
+ out1.withObjectOutputStream {
+ SerializableFunction<Integer, String> f = ((Integer e) -> 'a' + e)
+ it.writeObject(f)
+ }
+ def result1 = out1.toByteArray()
+
+ def out2 = new ByteArrayOutputStream()
+ out2.withObjectOutputStream {
+ SerializableFunction<Integer, String> f = ((Integer e) -> 'b' + e)
+ it.writeObject(f)
+ }
+ def result2 = out2.toByteArray()
+
+ // nested lambda expression
+ def out3 = new ByteArrayOutputStream()
+ out3.withObjectOutputStream {
+ SerializableFunction<Integer, String> f = ((Integer e) -> {
+ SerializableFunction<Integer, String> nf = ((Integer ne) -> 'n' + ne)
+ 'c' + nf(e)
+ })
+ it.writeObject(f)
+ }
+ def result3 = out3.toByteArray()
+
+ return [result1, result2, result3]
+ }
+ }
+
+ def (byte[] serializedLambdaBytes1, byte[] serializedLambdaBytes2, byte[] serializedLambdaBytes3) = Test1.p()
+ new ByteArrayInputStream(serializedLambdaBytes1).withObjectInputStream(Test1.class.classLoader) {
+ SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
+ assert 'a1' == f.apply(1)
+ }
+
+ new ByteArrayInputStream(serializedLambdaBytes2).withObjectInputStream(Test1.class.classLoader) {
+ SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
+ assert 'b1' == f.apply(1)
+ }
+
+ new ByteArrayInputStream(serializedLambdaBytes3).withObjectInputStream(Test1.class.classLoader) {
+ SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
+ assert 'cn1' == f.apply(1)
+ }
'''
}
+ @Test
void testDeserializeNestedLambda4() {
assertScript '''
- import java.util.function.Function
-
- interface SerializableFunction<T, R> extends Function<T, R>, Serializable {}
-
- @groovy.transform.CompileStatic
- class Test1 {
- static p() {
- def out1 = new ByteArrayOutputStream()
- SerializableFunction<Integer, String> f1 = (Integer e) -> 'a' + e
- out1.withObjectOutputStream {
- it.writeObject(f1)
- }
- def result1 = out1.toByteArray()
-
- def out2 = new ByteArrayOutputStream()
- SerializableFunction<Integer, String> f2 = (Integer e) -> 'b' + e
- out2.withObjectOutputStream {
- it.writeObject(f2)
- }
- def result2 = out2.toByteArray()
-
- // nested lambda expression
- def out3 = new ByteArrayOutputStream()
- SerializableFunction<Integer, String> f3 = (Integer e) -> {
- SerializableFunction<Integer, String> nf = ((Integer ne) -> 'n' + ne)
- 'c' + nf(e)
- }
- out3.withObjectOutputStream {
- it.writeObject(f3)
- }
- def result3 = out3.toByteArray()
-
- return [result1, result2, result3]
- }
- }
-
- def (byte[] serializedLambdaBytes1, byte[] serializedLambdaBytes2, byte[] serializedLambdaBytes3) = Test1.p()
- new ByteArrayInputStream(serializedLambdaBytes1).withObjectInputStream(Test1.class.classLoader) {
- SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
- assert 'a1' == f.apply(1)
- }
-
- new ByteArrayInputStream(serializedLambdaBytes2).withObjectInputStream(Test1.class.classLoader) {
- SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
- assert 'b1' == f.apply(1)
- }
-
- new ByteArrayInputStream(serializedLambdaBytes3).withObjectInputStream(Test1.class.classLoader) {
- SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
- assert 'cn1' == f.apply(1)
- }
+ import java.util.function.Function
+
+ interface SerializableFunction<T, R> extends Function<T, R>, Serializable {}
+
+ @groovy.transform.CompileStatic
+ class Test1 {
+ static p() {
+ def out1 = new ByteArrayOutputStream()
+ SerializableFunction<Integer, String> f1 = (Integer e) -> 'a' + e
+ out1.withObjectOutputStream {
+ it.writeObject(f1)
+ }
+ def result1 = out1.toByteArray()
+
+ def out2 = new ByteArrayOutputStream()
+ SerializableFunction<Integer, String> f2 = (Integer e) -> 'b' + e
+ out2.withObjectOutputStream {
+ it.writeObject(f2)
+ }
+ def result2 = out2.toByteArray()
+
+ // nested lambda expression
+ def out3 = new ByteArrayOutputStream()
+ SerializableFunction<Integer, String> f3 = (Integer e) -> {
+ SerializableFunction<Integer, String> nf = ((Integer ne) -> 'n' + ne)
+ 'c' + nf(e)
+ }
+ out3.withObjectOutputStream {
+ it.writeObject(f3)
+ }
+ def result3 = out3.toByteArray()
+
+ return [result1, result2, result3]
+ }
+ }
+
+ def (byte[] serializedLambdaBytes1, byte[] serializedLambdaBytes2, byte[] serializedLambdaBytes3) = Test1.p()
+ new ByteArrayInputStream(serializedLambdaBytes1).withObjectInputStream(Test1.class.classLoader) {
+ SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
+ assert 'a1' == f.apply(1)
+ }
+
+ new ByteArrayInputStream(serializedLambdaBytes2).withObjectInputStream(Test1.class.classLoader) {
+ SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
+ assert 'b1' == f.apply(1)
+ }
+
+ new ByteArrayInputStream(serializedLambdaBytes3).withObjectInputStream(Test1.class.classLoader) {
+ SerializableFunction<Integer, String> f = (SerializableFunction<Integer, String>) it.readObject()
+ assert 'cn1' == f.apply(1)
+ }
'''
}
-
}