27579
|
1 |
/*
|
|
2 |
* Copyright (c) 2011, 2014, 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 6430241
|
|
27 |
* @summary Hard to disable symbol file feature through API
|
|
28 |
* @library /tools/lib
|
|
29 |
* @build ToolBox
|
|
30 |
* @run main T6430241
|
|
31 |
*/
|
|
32 |
|
|
33 |
import java.io.*;
|
|
34 |
import java.util.*;
|
|
35 |
|
|
36 |
import javax.tools.*;
|
|
37 |
|
|
38 |
import com.sun.source.util.JavacTask;
|
|
39 |
import com.sun.tools.javac.api.JavacTool;
|
|
40 |
import com.sun.tools.javac.file.JavacFileManager;
|
|
41 |
import com.sun.tools.javac.util.Context;
|
|
42 |
|
|
43 |
public class T6430241 {
|
|
44 |
public static void main(String... args) throws Exception {
|
|
45 |
new T6430241().run();
|
|
46 |
}
|
|
47 |
|
|
48 |
void run() throws Exception {
|
|
49 |
setup();
|
|
50 |
testCommandLine();
|
|
51 |
testSimpleAPI();
|
|
52 |
testTaskAPI();
|
|
53 |
|
|
54 |
if (errors > 0)
|
|
55 |
throw new Exception(errors + " errors found");
|
|
56 |
}
|
|
57 |
|
|
58 |
void setup() throws Exception {
|
|
59 |
classesDir = new File("classes");
|
|
60 |
classesDir.mkdirs();
|
|
61 |
|
|
62 |
emptyDir = new File("empty");
|
|
63 |
emptyDir.mkdirs();
|
|
64 |
|
|
65 |
bootClassPath = createJar().getPath();
|
8034
|
66 |
|
27579
|
67 |
File srcDir = new File("src");
|
|
68 |
String test = "import sun.misc.Unsafe; class Test { }";
|
|
69 |
testFile = writeFile(srcDir, "Test.java", test);
|
|
70 |
}
|
|
71 |
|
|
72 |
//----- tests for command line invocation
|
|
73 |
|
|
74 |
void testCommandLine() throws Exception {
|
|
75 |
testCommandLine(true);
|
|
76 |
testCommandLine(false, "-Xbootclasspath/p:" + emptyDir);
|
|
77 |
testCommandLine(false, "-Xbootclasspath:" + bootClassPath);
|
|
78 |
testCommandLine(false, "-Xbootclasspath/a:" + emptyDir);
|
|
79 |
testCommandLine(false, "-XDignore.symbol.file");
|
|
80 |
System.err.println();
|
|
81 |
}
|
|
82 |
|
|
83 |
void testCommandLine(boolean expectWarnings, String... opts) throws Exception {
|
|
84 |
System.err.println("test command line: " + Arrays.asList(opts));
|
|
85 |
|
|
86 |
String[] args = initArgs(opts);
|
|
87 |
|
|
88 |
StringWriter sw = new StringWriter();
|
|
89 |
PrintWriter pw = new PrintWriter(sw);
|
|
90 |
int rc = com.sun.tools.javac.Main.compile(args, pw);
|
|
91 |
String out = showOutput(sw.toString());
|
|
92 |
|
|
93 |
checkCompilationOK(rc);
|
|
94 |
checkOutput(out, expectWarnings);
|
|
95 |
}
|
|
96 |
|
|
97 |
//----- tests for simple API invocation
|
8034
|
98 |
|
27579
|
99 |
void testSimpleAPI() {
|
|
100 |
testSimpleAPI(true);
|
|
101 |
testSimpleAPI(false, "-Xbootclasspath/p:" + emptyDir);
|
|
102 |
testSimpleAPI(false, "-Xbootclasspath:" + bootClassPath);
|
|
103 |
testSimpleAPI(false, "-Xbootclasspath/a:" + emptyDir);
|
|
104 |
testSimpleAPI(false, "-XDignore.symbol.file");
|
|
105 |
System.err.println();
|
|
106 |
}
|
|
107 |
|
|
108 |
void testSimpleAPI(boolean expectWarnings, String... opts) {
|
|
109 |
System.err.println("test simple API: " + Arrays.asList(opts));
|
|
110 |
|
|
111 |
String[] args = initArgs(opts);
|
|
112 |
|
|
113 |
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
114 |
PrintStream ps = new PrintStream(baos);
|
8034
|
115 |
|
27579
|
116 |
JavacTool tool = JavacTool.create();
|
|
117 |
int rc = tool.run(null, null, ps, args);
|
|
118 |
|
|
119 |
String out = showOutput(baos.toString());
|
8034
|
120 |
|
27579
|
121 |
checkCompilationOK(rc);
|
|
122 |
checkOutput(out, expectWarnings);
|
|
123 |
}
|
|
124 |
|
|
125 |
//----- tests for CompilationTask API invocation
|
|
126 |
|
|
127 |
void testTaskAPI() throws Exception {
|
|
128 |
List<File> bcp = new ArrayList<File>();
|
|
129 |
for (String f: bootClassPath.split(File.pathSeparator)) {
|
|
130 |
if (!f.isEmpty())
|
|
131 |
bcp.add(new File(f));
|
8034
|
132 |
}
|
|
133 |
|
27579
|
134 |
testTaskAPI(true, null);
|
|
135 |
testTaskAPI(false, bcp);
|
|
136 |
System.err.println();
|
|
137 |
}
|
8034
|
138 |
|
27579
|
139 |
void testTaskAPI(boolean expectWarnings, Iterable<? extends File> pcp) throws Exception {
|
|
140 |
System.err.println("test task API: " + pcp);
|
8034
|
141 |
|
27579
|
142 |
JavacTool tool = JavacTool.create();
|
|
143 |
try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
|
8034
|
144 |
|
27579
|
145 |
if (pcp != null)
|
|
146 |
fm.setLocation(StandardLocation.PLATFORM_CLASS_PATH, pcp);
|
8034
|
147 |
|
27579
|
148 |
Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(testFile);
|
8034
|
149 |
|
|
150 |
StringWriter sw = new StringWriter();
|
|
151 |
PrintWriter pw = new PrintWriter(sw);
|
27579
|
152 |
JavacTask task = tool.getTask(pw, fm, null, null, null, files);
|
|
153 |
boolean ok = task.call();
|
8034
|
154 |
String out = showOutput(sw.toString());
|
|
155 |
|
27579
|
156 |
checkCompilationOK(ok);
|
8034
|
157 |
checkOutput(out, expectWarnings);
|
|
158 |
}
|
27579
|
159 |
}
|
8034
|
160 |
|
27579
|
161 |
//----- utility methods
|
8034
|
162 |
|
27579
|
163 |
File createJar() throws IOException {
|
|
164 |
File f = new File("test.jar");
|
|
165 |
try (JavaFileManager fm = new JavacFileManager(new Context(), false, null)) {
|
|
166 |
ToolBox tb = new ToolBox();
|
|
167 |
tb.new JarTask(f.getPath())
|
|
168 |
.files(fm, StandardLocation.PLATFORM_CLASS_PATH, "java.lang.*", "sun.misc.*")
|
|
169 |
.run();
|
8034
|
170 |
}
|
27579
|
171 |
return f;
|
|
172 |
}
|
8034
|
173 |
|
27579
|
174 |
/**
|
|
175 |
* Create a file with given content.
|
|
176 |
*/
|
|
177 |
File writeFile(File dir, String path, String content) throws IOException {
|
|
178 |
File f = new File(dir, path);
|
|
179 |
f.getParentFile().mkdirs();
|
|
180 |
FileWriter out = new FileWriter(f);
|
|
181 |
try {
|
|
182 |
out.write(content);
|
|
183 |
} finally {
|
|
184 |
out.close();
|
8034
|
185 |
}
|
27579
|
186 |
return f;
|
|
187 |
}
|
8034
|
188 |
|
27579
|
189 |
/**
|
|
190 |
* Initialize args for compilation with given opts.
|
|
191 |
* @return opts -d classesDir testFile
|
|
192 |
*/
|
|
193 |
String[] initArgs(String[] opts) {
|
|
194 |
List<String> args = new ArrayList<String>();
|
|
195 |
args.addAll(Arrays.asList(opts));
|
|
196 |
args.add("-d");
|
|
197 |
args.add(classesDir.getPath());
|
|
198 |
args.add(testFile.getPath());
|
|
199 |
return args.toArray(new String[args.size()]);
|
|
200 |
}
|
8034
|
201 |
|
27579
|
202 |
/**
|
|
203 |
* Show output from compilation if non empty.
|
|
204 |
*/
|
|
205 |
String showOutput(String out) {
|
|
206 |
if (!out.isEmpty())
|
|
207 |
System.err.println(out);
|
|
208 |
return out;
|
|
209 |
}
|
8034
|
210 |
|
27579
|
211 |
/**
|
|
212 |
* Verify compilation succeeded.
|
|
213 |
*/
|
|
214 |
void checkCompilationOK(boolean ok) {
|
|
215 |
if (!ok)
|
|
216 |
error("compilation failed");
|
|
217 |
}
|
8034
|
218 |
|
27579
|
219 |
/**
|
|
220 |
* Verify compilation succeeded.
|
|
221 |
*/
|
|
222 |
void checkCompilationOK(int rc) {
|
|
223 |
if (rc != 0)
|
|
224 |
error("compilation failed, rc: " + rc);
|
|
225 |
}
|
8034
|
226 |
|
27579
|
227 |
/**
|
|
228 |
* Check whether output contains warnings if and only if warnings
|
|
229 |
* are expected.
|
|
230 |
*/
|
|
231 |
void checkOutput(String out, boolean expectWarnings) {
|
|
232 |
boolean foundWarnings = out.contains("warning");
|
|
233 |
if (foundWarnings) {
|
|
234 |
if (!expectWarnings)
|
|
235 |
error("unexpected warnings found");
|
|
236 |
} else {
|
|
237 |
if (expectWarnings)
|
|
238 |
error("expected warnings not found");
|
8034
|
239 |
}
|
27579
|
240 |
}
|
8034
|
241 |
|
27579
|
242 |
/**
|
|
243 |
* Report an error.
|
|
244 |
*/
|
|
245 |
void error(String msg) {
|
|
246 |
System.err.println("error: " + msg);
|
|
247 |
errors++;
|
|
248 |
}
|
8034
|
249 |
|
27579
|
250 |
String bootClassPath;
|
|
251 |
File classesDir;
|
|
252 |
File emptyDir;
|
|
253 |
File testFile;
|
|
254 |
int errors;
|
|
255 |
}
|