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.
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.
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).
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.
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
27
* @summary Verify --system is required rather than -bootclasspath for -source 9.
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
36
import java.io.IOException;
37
import java.nio.file.Path;
38
import java.nio.file.Paths;
39
import java.util.Arrays;
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;
52
import toolbox.Task.Expect;
53
import toolbox.TestRunner;
54
import toolbox.ToolBox;
56
import com.sun.tools.javac.file.PathFileObject;
58
public class BCPOrSystemNotSpecified extends TestRunner {
60
private final ToolBox tb = new ToolBox();
61
private final String fileSep = System.getProperty("file.separator");
63
public BCPOrSystemNotSpecified() {
67
public static void main(String... args) throws Exception {
68
new BCPOrSystemNotSpecified().runTests();
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);
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",
87
log = new JavacTask(tb)
88
.options("-XDrawDiagnostics", "-source", "8")
90
.files(tb.findJavaFiles(src))
93
.getOutputLines(Task.OutputKind.DIRECT);
95
if (!expected.equals(log)) {
96
throw new AssertionError("Unexpected output: " + log);
99
Path bcp = base.resolve("bcp");
104
.options("-XDrawDiagnostics",
106
"-bootclasspath", bcp.toAbsolutePath().toString())
108
.files(tb.findJavaFiles(src))
111
.getOutputLines(Task.OutputKind.DIRECT);
113
if (!expected.equals(log)) {
114
throw new AssertionError("Unexpected output: " + log);
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);
127
List<String> expected = Arrays.asList(
128
"- compiler.warn.source.no.system.modules.path: 9, (compiler.misc.source.no.system.modules.path: 9)",
132
log = new JavacTask(tb)
133
.options("-XDrawDiagnostics",
136
.files(tb.findJavaFiles(src))
139
.getOutputLines(Task.OutputKind.DIRECT);
141
if (!expected.equals(log)) {
142
throw new AssertionError("Unexpected output: " + log);
145
Path bcp = base.resolve("bcp");
149
log = new JavacTask(tb)
150
.options("-XDrawDiagnostics",
152
"-bootclasspath", bcp.toAbsolutePath().toString())
154
.files(tb.findJavaFiles(src))
157
.getOutputLines(Task.OutputKind.DIRECT);
159
if (!expected.equals(log)) {
160
throw new AssertionError("Unexpected output: " + log);
164
.options("-XDrawDiagnostics",
167
"--module-path", bcp.toAbsolutePath().toString(),
170
.files(tb.findJavaFiles(src))
173
.getOutputLines(Task.OutputKind.DIRECT);
175
if (!expected.equals(log)) {
176
throw new AssertionError("Unexpected output: " + log);
180
.options("-XDrawDiagnostics",
182
"--system", System.getProperty("java.home"),
185
.files(tb.findJavaFiles(src))
188
.getOutputLines(Task.OutputKind.DIRECT);
190
if (!expected.equals(log)) {
191
throw new AssertionError("Unexpected output: " + log);
195
protected void runTests() throws Exception {
196
runTests(m -> new Object[] { Paths.get(m.getName()).toAbsolutePath() });
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,
206
for (JavaFileObject file : jfm.list(javaBase,
208
EnumSet.of(JavaFileObject.Kind.CLASS),
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));