test/langtools/tools/javac/switchextra/SwitchStatementArrow.java
author jlahoda
Tue, 12 Nov 2019 06:32:13 +0000
changeset 59021 cfc7bb9a5a92
parent 53878 4584d0331318
permissions -rw-r--r--
8232684: Make switch expressions final Reviewed-by: alanb, mcimadamore, kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
     1
/*
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
     2
 * @test /nodymaticcopyright/
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
     3
 * @bug 8206986
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
     4
 * @summary Verify rule cases work properly.
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
     5
 * @compile/fail/ref=SwitchStatementArrow-old.out -source 9 -Xlint:-options -XDrawDiagnostics SwitchStatementArrow.java
59021
cfc7bb9a5a92 8232684: Make switch expressions final
jlahoda
parents: 53878
diff changeset
     6
 * @compile SwitchStatementArrow.java
cfc7bb9a5a92 8232684: Make switch expressions final
jlahoda
parents: 53878
diff changeset
     7
 * @run main SwitchStatementArrow
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
     8
 */
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
     9
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    10
import java.util.Objects;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    11
import java.util.function.Function;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    12
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    13
public class SwitchStatementArrow {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    14
    public static void main(String... args) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    15
        new SwitchStatementArrow().run();
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    16
    }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    17
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    18
    private void run() {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    19
        runTest(this::statement1);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    20
        runTest(this::scope);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    21
    }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    22
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    23
    private void runTest(Function<T, String> print) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    24
        check(T.A,  print, "A");
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    25
        check(T.B,  print, "B-C");
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    26
        check(T.C,  print, "B-C");
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    27
        try {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    28
            print.apply(T.D);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    29
            throw new AssertionError();
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    30
        } catch (IllegalStateException ex) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    31
            if (!Objects.equals("D", ex.getMessage()))
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    32
                throw new AssertionError(ex);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    33
        }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    34
        check(T.E,  print, "other");
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    35
    }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    36
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    37
    private String statement1(T t) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    38
        String res;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    39
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    40
        switch (t) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    41
            case A -> { res = "A"; }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    42
            case B, C -> res = "B-C";
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    43
            case D -> throw new IllegalStateException("D");
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    44
            default -> { res = "other"; break; }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    45
        }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    46
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    47
        return res;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    48
    }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    49
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    50
    private String scope(T t) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    51
        String res;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    52
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    53
        switch (t) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    54
            case A -> { String r = "A"; res = r; }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    55
            case B, C -> {String r = "B-C"; res = r; }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    56
            case D -> throw new IllegalStateException("D");
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    57
            default -> { String r = "other"; res = r; break; }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    58
        }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    59
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    60
        return res;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    61
    }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    62
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    63
    private int r;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    64
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    65
    private void check(T t, Function<T, String> print, String expected) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    66
        String result = print.apply(t);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    67
        if (!Objects.equals(result, expected)) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    68
            throw new AssertionError("Unexpected result: " + result);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    69
        }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    70
    }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    71
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    72
    enum T {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    73
        A, B, C, D, E;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    74
    }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    75
}