jdk

Форк
0
/
T8328747.java 
126 строк · 4.4 Кб
1
/*
2
 * Copyright (c) 2024, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
 * or visit www.oracle.com if you need additional information or have any
21
 * questions.
22
 */
23

24
/*
25
 * @test
26
 * @bug 8328747
27
 * @summary WrongMethodTypeException with pattern matching on switch on sealed classes
28
 * @library /tools/lib
29
 * @modules jdk.compiler/com.sun.tools.javac.api
30
 *          jdk.compiler/com.sun.tools.javac.main
31
 *          jdk.jdeps/com.sun.tools.javap
32
 * @build toolbox.ToolBox toolbox.JavapTask
33
 * @compile T8328747.java
34
 * @run main T8328747
35
 */
36

37
import toolbox.*;
38

39
import java.nio.file.Files;
40
import java.nio.file.Path;
41
import java.nio.file.Paths;
42

43
public class T8328747 extends TestRunner  {
44
    private ToolBox tb;
45

46
    public static void main(String... args) throws Exception {
47
        new T8328747().runTests();
48
    }
49

50
    T8328747() {
51
        super(System.err);
52
        tb = new ToolBox();
53
    }
54

55
    public void runTests() throws Exception {
56
        runTests(m -> new Object[] { Paths.get(m.getName()) });
57
    }
58

59
    @Test
60
    public void test(Path base) throws Exception {
61
        Path current = base.resolve(".");
62
        Path src = current.resolve("src");
63
        Path classes = current.resolve("classes");
64
        tb.writeJavaFiles(src,
65
                """
66
                package test;
67
                public class Test {
68
                   public static void main(String[] args) {
69
                     f(new P());
70
                     f(new O());
71
                   }
72

73
                   private static void f(I info) {
74
                     switch (info) {
75
                       case P p -> System.err.println(p);
76
                       case O o -> System.err.println(o);
77
                     }
78
                   }
79

80
                   static sealed interface I permits P, O {}
81
                   private abstract static class A {}
82
                   static final class P extends A implements I {}
83
                   static final class O extends A implements I {}
84
                }
85
                """);
86

87
        Files.createDirectories(classes);
88

89
        {//with --release:
90
            new JavacTask(tb)
91
                    .options("--release", "21")
92
                    .outdir(classes)
93
                    .files(tb.findJavaFiles(src))
94
                    .run(Task.Expect.SUCCESS)
95
                    .writeAll();
96

97
            String javapOut = new JavapTask(tb)
98
                    .options("-v")
99
                    .classpath(classes.toString())
100
                    .classes("test.Test")
101
                    .run()
102
                    .getOutput(Task.OutputKind.DIRECT);
103

104
            if (!javapOut.contains("#25 = InvokeDynamic      #0:#26         // #0:typeSwitch:(Ljava/lang/Object;I)I"))
105
                throw new AssertionError("typeSwitch for a version less than 23 should accept a static type of java.lang.Object");
106
        }
107

108
        {//without:
109
            new JavacTask(tb)
110
                    .outdir(classes)
111
                    .files(tb.findJavaFiles(src))
112
                    .run(Task.Expect.SUCCESS)
113
                    .writeAll();
114

115
            String javapOut = new JavapTask(tb)
116
                    .options("-v")
117
                    .classpath(classes.toString())
118
                    .classes("test.Test")
119
                    .run()
120
                    .getOutput(Task.OutputKind.DIRECT);
121

122
            if (!javapOut.contains("#25 = InvokeDynamic      #0:#26         // #0:typeSwitch:(Ltest/Test$I;I)I"))
123
                throw new AssertionError("typeSwitch from version 23 and beyond should accept a precise selector type");
124
        }
125
    }
126
}
127

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.