1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8220041
* @summary Verify variable capture works inside switch expressions which are
* inside variable declarations
* @compile LambdaCapture.java
*/
import java.util.Objects;
public class LambdaCapture {
public static void main(String... args) {
new LambdaCapture().run();
}
void run() {
assertEquals("00", lambdaCapture1(0).t());
assertEquals("12", lambdaCapture1(1).t());
assertEquals("D", lambdaCapture1(2).t());
assertEquals("D", lambdaCapture1(3).t());
assertEquals("00", lambdaCapture2(0).t());
assertEquals("12", lambdaCapture2(1).t());
assertEquals("D", lambdaCapture2(2).t());
assertEquals("D", lambdaCapture2(3).t());
}
I<String> lambdaCapture1(int i) {
int j = i + 1;
I<String> r = switch (i) {
case 0 -> () -> "0" + i; //capture parameter
case 1 -> () -> "1" + j; //capture local variable
default -> {
String k = "D";
yield () -> k; //capture local from the switch expr.
}
};
return r;
}
I<String> lambdaCapture2(int i) {
int j = i + 1;
return switch (i) {
case 0 -> () -> "0" + i; //capture parameter
case 1 -> () -> "1" + j; //capture local variable
default -> {
String k = "D";
yield () -> k; //capture local from the switch expr.
}
};
}
{
int j1 = 1;
I<String> r1 = switch (j1) {
case 1 -> () -> "1" + j1; //capture local variable
default -> {
String k = "D";
yield () -> k; //capture local from the switch expr.
}
};
assertEquals("11", r1.t());
int j2 = 2;
I<String> r2 = switch (j2) {
case 1 -> () -> "1" + j2; //capture local variable
default -> {
String k = "D";
yield () -> k; //capture local from the switch expr.
}
};
assertEquals("D", r2.t());
}
private void assertEquals(Object expected, Object actual) {
if (!Objects.equals(expected, actual)) {
throw new AssertionError("Unexpected value: " + actual);
}
}
interface I<T> {
public T t();
}
}
|