| author | mcimadamore |
| Thu May 27 18:11:12 2010 +0100 (24 months ago) | |
| changeset 553 | 7704dcd17e0b |
| child 556 | 81966684ef23 |
| permissions | -rw-r--r-- |
1 /*
2 * Copyright 2010 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24 /*
25 * @test
26 * @summary basic test for capture of non-mutable locals
27 * @author Brian Goetz
28 * @author Maurizio Cimadamore
29 * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableMethodHandles LambdaConv01
30 */
31
32 public class LambdaCapture01 {
33
34 static int assertionCount = 0;
35
36 static void assertTrue(boolean cond) {
37 assertionCount++;
38 if (!cond)
39 throw new AssertionError();
40 }
41
42 interface Tester {
43 void test();
44 }
45
46 interface TU<T, U> {
47 public T foo(U u);
48 }
49
50 public static <T, U> T exec(TU<T, U> lambda, U x) {
51 return lambda.foo(x);
52 }
53
54 public int n = 5;
55
56 //Simple local capture
57 void test1() {
58 final int N = 1;
59 int x = LambdaCapture01.<Integer,Integer>exec(#(Integer x)(x + N), 3);
60 assertTrue(4 == x);
61 }
62
63 //Local capture with multiple scopes (anon class)
64 void test2() {
65 final int N = 1;
66 new Tester() {
67 public void test() {
68 final int M = 2;
69 int x = LambdaCapture01.<Integer,Integer>exec(#(Integer x)(x + N + M), 3);
70 assertTrue(6 == x);
71 }
72 }.test();
73 }
74
75 //Local capture with multiple scopes (local class)
76 void test3() {
77 final int N = 1;
78 class MyTester implements Tester {
79 public void test() {
80 final int M = 2;
81 int x = LambdaCapture01.<Integer,Integer>exec(#(Integer x)(x + N + M), 3);
82 assertTrue(6 == x);
83 }
84 }
85 new MyTester().test();
86 }
87
88 //access to field from enclosing scope
89 void test4() {
90 final int N = 4;
91 int x1 = LambdaCapture01.<Integer,Integer>exec(#(Integer x)(x + n + N), 3);
92 assertTrue(12 == x1);
93 int x2 = LambdaCapture01.<Integer,Integer>exec(#(Integer x)(x + LambdaCapture01.this.n + N), 3);
94 assertTrue(12 == x2);
95 }
96
97 public static void main(String[] args) {
98 LambdaCapture01 t = new LambdaCapture01();
99 t.test1();
100 t.test2();
101 t.test3();
102 t.test4();
103 assertTrue(assertionCount == 5);
104 }
105 }