jdk

Форк
0
/
BCPOrSystemNotSpecified.java 
224 строки · 8.2 Кб
1
/*
2
 * Copyright (c) 2019, 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 8228460
27
 * @summary Verify --system is required rather than -bootclasspath for -source 9.
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.file
32
 * @build toolbox.ToolBox toolbox.JavacTask toolbox.TestRunner
33
 * @run main BCPOrSystemNotSpecified
34
 */
35

36
import java.io.IOException;
37
import java.nio.file.Path;
38
import java.nio.file.Paths;
39
import java.util.Arrays;
40
import java.util.List;
41

42
import java.io.InputStream;
43
import java.nio.file.Files;
44
import java.util.EnumSet;
45
import javax.tools.JavaFileManager;
46
import javax.tools.JavaFileObject;
47
import javax.tools.StandardJavaFileManager;
48
import javax.tools.StandardLocation;
49
import javax.tools.ToolProvider;
50
import toolbox.JavacTask;
51
import toolbox.Task;
52
import toolbox.Task.Expect;
53
import toolbox.TestRunner;
54
import toolbox.ToolBox;
55

56
import com.sun.tools.javac.file.PathFileObject;
57

58
public class BCPOrSystemNotSpecified extends TestRunner {
59

60
    private final ToolBox tb = new ToolBox();
61
    private final String fileSep = System.getProperty("file.separator");
62

63
    public BCPOrSystemNotSpecified() {
64
        super(System.err);
65
    }
66

67
    public static void main(String... args) throws Exception {
68
        new BCPOrSystemNotSpecified().runTests();
69
    }
70

71
    @Test
72
    public void testSource8(Path base) throws IOException {
73
        Path src = base.resolve("src");
74
        tb.writeJavaFiles(src,
75
                          "package test; public class Test { } ");
76
        Path classes = base.resolve("classes");
77
        tb.createDirectories(classes);
78

79
        List<String> log;
80
        List<String> expected = Arrays.asList(
81
                "- compiler.warn.source.no.bootclasspath: 8, (compiler.misc.source.no.bootclasspath: 8)",
82
                "- compiler.warn.option.obsolete.source: 8",
83
                "- compiler.warn.option.obsolete.suppression",
84
                "3 warnings"
85
        );
86

87
        log = new JavacTask(tb)
88
                .options("-XDrawDiagnostics", "-source", "8")
89
                .outdir(classes)
90
                .files(tb.findJavaFiles(src))
91
                .run(Expect.SUCCESS)
92
                .writeAll()
93
                .getOutputLines(Task.OutputKind.DIRECT);
94

95
        if (!expected.equals(log)) {
96
            throw new AssertionError("Unexpected output: " + log);
97
        }
98

99
        Path bcp = base.resolve("bcp");
100

101
        prepareBCP(bcp);
102

103
        new JavacTask(tb)
104
                .options("-XDrawDiagnostics",
105
                         "-source", "8",
106
                         "-bootclasspath", bcp.toAbsolutePath().toString())
107
                .outdir(classes)
108
                .files(tb.findJavaFiles(src))
109
                .run(Expect.SUCCESS)
110
                .writeAll()
111
                .getOutputLines(Task.OutputKind.DIRECT);
112

113
        if (!expected.equals(log)) {
114
            throw new AssertionError("Unexpected output: " + log);
115
        }
116
    }
117

118
    @Test
119
    public void testSource9(Path base) throws IOException {
120
        Path src = base.resolve("src");
121
        tb.writeJavaFiles(src,
122
                          "package test; public class Test { } ");
123
        Path classes = base.resolve("classes");
124
        tb.createDirectories(classes);
125

126
        List<String> log;
127
        List<String> expected = Arrays.asList(
128
                "- compiler.warn.source.no.system.modules.path: 9, (compiler.misc.source.no.system.modules.path: 9)",
129
                "1 warning"
130
        );
131

132
        log = new JavacTask(tb)
133
                .options("-XDrawDiagnostics",
134
                         "-source", "9")
135
                .outdir(classes)
136
                .files(tb.findJavaFiles(src))
137
                .run(Expect.SUCCESS)
138
                .writeAll()
139
                .getOutputLines(Task.OutputKind.DIRECT);
140

141
        if (!expected.equals(log)) {
142
            throw new AssertionError("Unexpected output: " + log);
143
        }
144

145
        Path bcp = base.resolve("bcp");
146

147
        prepareBCP(bcp);
148

149
        log = new JavacTask(tb)
150
                .options("-XDrawDiagnostics",
151
                         "-source", "9",
152
                         "-bootclasspath", bcp.toAbsolutePath().toString())
153
                .outdir(classes)
154
                .files(tb.findJavaFiles(src))
155
                .run(Expect.SUCCESS)
156
                .writeAll()
157
                .getOutputLines(Task.OutputKind.DIRECT);
158

159
        if (!expected.equals(log)) {
160
            throw new AssertionError("Unexpected output: " + log);
161
        }
162

163
        new JavacTask(tb)
164
                .options("-XDrawDiagnostics",
165
                         "-source", "9",
166
                         "--system", "none",
167
                         "--module-path", bcp.toAbsolutePath().toString(),
168
                         "-Werror")
169
                .outdir(classes)
170
                .files(tb.findJavaFiles(src))
171
                .run(Expect.SUCCESS)
172
                .writeAll()
173
                .getOutputLines(Task.OutputKind.DIRECT);
174

175
        if (!expected.equals(log)) {
176
            throw new AssertionError("Unexpected output: " + log);
177
        }
178

179
        new JavacTask(tb)
180
                .options("-XDrawDiagnostics",
181
                         "-source", "9",
182
                         "--system", System.getProperty("java.home"),
183
                         "-Werror")
184
                .outdir(classes)
185
                .files(tb.findJavaFiles(src))
186
                .run(Expect.SUCCESS)
187
                .writeAll()
188
                .getOutputLines(Task.OutputKind.DIRECT);
189

190
        if (!expected.equals(log)) {
191
            throw new AssertionError("Unexpected output: " + log);
192
        }
193
    }
194

195
    protected void runTests() throws Exception {
196
        runTests(m -> new Object[] { Paths.get(m.getName()).toAbsolutePath() });
197
    }
198

199
    private void prepareBCP(Path target) throws IOException {
200
        try (StandardJavaFileManager jfm = ToolProvider.getSystemJavaCompiler()
201
                                               .getStandardFileManager(null, null, null)) {
202
            for (String pack : new String[] {"", "java.lang", "java.lang.annotation", "jdk.internal.javac"}) {
203
                JavaFileManager.Location javaBase =
204
                        jfm.getLocationForModule(StandardLocation.SYSTEM_MODULES,
205
                                                 "java.base");
206
                for (JavaFileObject file : jfm.list(javaBase,
207
                                                    pack,
208
                                                    EnumSet.of(JavaFileObject.Kind.CLASS),
209
                                                    false)) {
210
                    Path targetDir = target.resolve(pack.replace(".", fileSep));
211
                    Files.createDirectories(targetDir);
212
                    try (InputStream in = file.openInputStream()) {
213
                        String sourcePath = file.getName();
214
                        // Here, we should use file system separator instead of the operating system separator.
215
                        String fileSystemSep = jfm.asPath(file).getFileSystem().getSeparator();
216
                        int sepPos = sourcePath.lastIndexOf(fileSystemSep);
217
                        String fileName = sourcePath.substring(sepPos + 1);
218
                        Files.copy(in, targetDir.resolve(fileName));
219
                    }
220
                }
221
            }
222
        }
223
    }
224
}
225

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

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

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

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