AllowEnclosingVarCaptureTest.java revision 3087:ed4c306ec942
139193Sjdp/*
2130809Smarcel * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3130809Smarcel * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
498948Sobrien *
539193Sjdp * This code is free software; you can redistribute it and/or modify it
698948Sobrien * under the terms of the GNU General Public License version 2 only, as
739193Sjdp * published by the Free Software Foundation.
898948Sobrien *
998948Sobrien * This code is distributed in the hope that it will be useful, but WITHOUT
1098948Sobrien * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1198948Sobrien * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1239193Sjdp * version 2 for more details (a copy is included in the LICENSE file that
1398948Sobrien * accompanied this code).
1498948Sobrien *
1598948Sobrien * You should have received a copy of the GNU General Public License version
1698948Sobrien * 2 along with this work; if not, write to the Free Software Foundation,
1739193Sjdp * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1898948Sobrien *
1998948Sobrien * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2098948Sobrien * or visit www.oracle.com if you need additional information or have any
2198948Sobrien * questions.
2239193Sjdp */
23130809Smarcel
24130809Smarcel/*
25130809Smarcel * @test
26130809Smarcel * @bug 8129740 8133111
27130809Smarcel * @summary Incorrect class file created when passing lambda in inner class constructor
28130809Smarcel * @run main AllowEnclosingVarCaptureTest
29130809Smarcel */
30130809Smarcel
3139193Sjdppublic class AllowEnclosingVarCaptureTest {
3239193Sjdp
33130809Smarcel    int var = 0;
34130809Smarcel
35130809Smarcel    void foo() {
36130809Smarcel        var *= 2;
3739193Sjdp    }
38130809Smarcel
39130809Smarcel    public static void main(String[] args) {
40130809Smarcel        new AllowEnclosingVarCaptureTest().new Inner(9764);
4139193Sjdp    }
4239193Sjdp
4339193Sjdp    public class Inner {
4439193Sjdp        public Inner(Runnable r) {
4539193Sjdp            r.run();
46130809Smarcel            if (var != 66704)
4798948Sobrien                throw new AssertionError("Unexpected output: " + var);
4846289Sdfr        }
49130809Smarcel
5046289Sdfr        public Inner(int x) {
51130809Smarcel            this(() -> {
52130809Smarcel                var = x + 1234;
53130809Smarcel                AllowEnclosingVarCaptureTest.this.var += 5678;
5498948Sobrien                foo();
55130809Smarcel                AllowEnclosingVarCaptureTest.this.foo();
56130809Smarcel            });
57130809Smarcel        }
5839193Sjdp    }
59130809Smarcel}
6039193Sjdp