jdk

Форк
0
/
EnumAccessible.java 
124 строки · 4.1 Кб
1
/*
2
 * Copyright (c) 2022, 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 8178701
27
 * @summary Check javac can generate code for protected enums accessible through subclassing.
28
 * @library /tools/lib
29
 * @modules jdk.compiler/com.sun.tools.javac.api
30
 *          jdk.compiler/com.sun.tools.javac.main
31
 *          jdk.compiler/com.sun.tools.javac.util
32
 * @build toolbox.ToolBox toolbox.JavacTask
33
 * @run main EnumAccessible
34
*/
35

36
import java.nio.file.Files;
37
import java.nio.file.Path;
38
import java.nio.file.Paths;
39
import java.util.List;
40
import java.util.Objects;
41

42
import toolbox.TestRunner;
43
import toolbox.JavacTask;
44
import toolbox.JavaTask;
45
import toolbox.Task;
46
import toolbox.ToolBox;
47

48
public class EnumAccessible extends TestRunner {
49

50
    ToolBox tb;
51

52
    public static void main(String... args) throws Exception {
53
        new EnumAccessible().runTests();
54
    }
55

56
    EnumAccessible() {
57
        super(System.err);
58
        tb = new ToolBox();
59
    }
60

61
    public void runTests() throws Exception {
62
        runTests(m -> new Object[] { Paths.get(m.getName()) });
63
    }
64

65
    @Test
66
    public void testPattern(Path base) throws Exception {
67
        Path current = base.resolve(".");
68
        Path src = current.resolve("src");
69
        Path classes = current.resolve("classes");
70
        tb.writeJavaFiles(src,
71
                          """
72
                          package foo;
73
                          import bar.D.E;
74
                          public class A {
75
                              public static class B {
76
                                  protected enum C {
77
                                      X, Y, Z
78
                                  }
79
                              }
80
                              public static void main(String... args) {
81
                                  new E().run(B.C.X);
82
                              }
83
                          }
84
                          """,
85
                          """
86
                          package bar;
87
                          import foo.A.B;
88
                          public class D {
89
                              public static class E extends B {
90
                                  public void run(C arg) {
91
                                      switch (arg) {
92
                                          default: System.out.println("OK");
93
                                      }
94
                                  }
95
                              }
96
                          }
97
                          """);
98

99
        Files.createDirectories(classes);
100

101
        new JavacTask(tb)
102
            .options("-doe")
103
            .outdir(classes)
104
            .files(tb.findJavaFiles(src))
105
            .run(Task.Expect.SUCCESS)
106
            .writeAll();
107

108
        var out = new JavaTask(tb)
109
                .classpath(classes.toString())
110
                .className("foo.A")
111
                .run()
112
                .writeAll()
113
                .getOutputLines(Task.OutputKind.STDOUT);
114

115
        var expectedOut = List.of("OK");
116

117
        if (!Objects.equals(expectedOut, out)) {
118
            throw new AssertionError("Incorrect Output, expected: " + expectedOut +
119
                                      ", actual: " + out);
120

121
        }
122
    }
123

124
}
125

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

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

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

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