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