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