Thu, 31 Aug 2017 15:17:03 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | */ |
aoqi@0 | 23 | |
aoqi@0 | 24 | import java.io.*; |
aoqi@0 | 25 | import java.net.URL; |
aoqi@0 | 26 | import java.net.URLClassLoader; |
aoqi@0 | 27 | import java.util.*; |
aoqi@0 | 28 | import java.util.regex.*; |
aoqi@0 | 29 | import javax.annotation.processing.Processor; |
aoqi@0 | 30 | import javax.tools.Diagnostic; |
aoqi@0 | 31 | import javax.tools.DiagnosticCollector; |
aoqi@0 | 32 | import javax.tools.JavaCompiler; |
aoqi@0 | 33 | import javax.tools.JavaCompiler.CompilationTask; |
aoqi@0 | 34 | import javax.tools.JavaFileObject; |
aoqi@0 | 35 | import javax.tools.StandardJavaFileManager; |
aoqi@0 | 36 | import javax.tools.ToolProvider; |
aoqi@0 | 37 | |
aoqi@0 | 38 | // The following two classes are both used, but cannot be imported directly |
aoqi@0 | 39 | // import com.sun.tools.javac.Main |
aoqi@0 | 40 | // import com.sun.tools.javac.main.Main |
aoqi@0 | 41 | |
aoqi@0 | 42 | import com.sun.tools.javac.api.ClientCodeWrapper; |
aoqi@0 | 43 | import com.sun.tools.javac.file.JavacFileManager; |
aoqi@0 | 44 | import com.sun.tools.javac.main.Main; |
aoqi@0 | 45 | import com.sun.tools.javac.util.Context; |
aoqi@0 | 46 | import com.sun.tools.javac.util.JavacMessages; |
aoqi@0 | 47 | import com.sun.tools.javac.util.JCDiagnostic; |
aoqi@0 | 48 | |
aoqi@0 | 49 | /** |
aoqi@0 | 50 | * Class to handle example code designed to illustrate javac diagnostic messages. |
aoqi@0 | 51 | */ |
aoqi@0 | 52 | class Example implements Comparable<Example> { |
aoqi@0 | 53 | /* Create an Example from the files found at path. |
aoqi@0 | 54 | * The head of the file, up to the first Java code, is scanned |
aoqi@0 | 55 | * for information about the test, such as what resource keys it |
aoqi@0 | 56 | * generates when run, what options are required to run it, and so on. |
aoqi@0 | 57 | */ |
aoqi@0 | 58 | Example(File file) { |
aoqi@0 | 59 | this.file = file; |
aoqi@0 | 60 | declaredKeys = new TreeSet<String>(); |
aoqi@0 | 61 | srcFiles = new ArrayList<File>(); |
aoqi@0 | 62 | procFiles = new ArrayList<File>(); |
aoqi@0 | 63 | supportFiles = new ArrayList<File>(); |
aoqi@0 | 64 | srcPathFiles = new ArrayList<File>(); |
aoqi@0 | 65 | |
aoqi@0 | 66 | findFiles(file, srcFiles); |
aoqi@0 | 67 | for (File f: srcFiles) { |
aoqi@0 | 68 | parse(f); |
aoqi@0 | 69 | } |
aoqi@0 | 70 | |
aoqi@0 | 71 | if (infoFile == null) |
aoqi@0 | 72 | throw new Error("Example " + file + " has no info file"); |
aoqi@0 | 73 | } |
aoqi@0 | 74 | |
aoqi@0 | 75 | private void findFiles(File f, List<File> files) { |
aoqi@0 | 76 | if (f.isDirectory()) { |
aoqi@0 | 77 | for (File c: f.listFiles()) { |
aoqi@0 | 78 | if (files == srcFiles && c.getName().equals("processors")) |
aoqi@0 | 79 | findFiles(c, procFiles); |
aoqi@0 | 80 | else if (files == srcFiles && c.getName().equals("sourcepath")) { |
aoqi@0 | 81 | srcPathDir = c; |
aoqi@0 | 82 | findFiles(c, srcPathFiles); |
aoqi@0 | 83 | } else if (files == srcFiles && c.getName().equals("support")) |
aoqi@0 | 84 | findFiles(c, supportFiles); |
aoqi@0 | 85 | else |
aoqi@0 | 86 | findFiles(c, files); |
aoqi@0 | 87 | } |
aoqi@0 | 88 | } else if (f.isFile() && f.getName().endsWith(".java")) { |
aoqi@0 | 89 | files.add(f); |
aoqi@0 | 90 | } |
aoqi@0 | 91 | } |
aoqi@0 | 92 | |
aoqi@0 | 93 | private void parse(File f) { |
aoqi@0 | 94 | Pattern keyPat = Pattern.compile(" *// *key: *([^ ]+) *"); |
aoqi@0 | 95 | Pattern optPat = Pattern.compile(" *// *options: *(.*)"); |
aoqi@0 | 96 | Pattern runPat = Pattern.compile(" *// *run: *(.*)"); |
aoqi@0 | 97 | Pattern javaPat = Pattern.compile(" *@?[A-Za-z].*"); |
aoqi@0 | 98 | try { |
aoqi@0 | 99 | String[] lines = read(f).split("[\r\n]+"); |
aoqi@0 | 100 | for (String line: lines) { |
aoqi@0 | 101 | Matcher keyMatch = keyPat.matcher(line); |
aoqi@0 | 102 | if (keyMatch.matches()) { |
aoqi@0 | 103 | foundInfo(f); |
aoqi@0 | 104 | declaredKeys.add(keyMatch.group(1)); |
aoqi@0 | 105 | continue; |
aoqi@0 | 106 | } |
aoqi@0 | 107 | Matcher optMatch = optPat.matcher(line); |
aoqi@0 | 108 | if (optMatch.matches()) { |
aoqi@0 | 109 | foundInfo(f); |
aoqi@0 | 110 | options = Arrays.asList(optMatch.group(1).trim().split(" +")); |
aoqi@0 | 111 | continue; |
aoqi@0 | 112 | } |
aoqi@0 | 113 | Matcher runMatch = runPat.matcher(line); |
aoqi@0 | 114 | if (runMatch.matches()) { |
aoqi@0 | 115 | foundInfo(f); |
aoqi@0 | 116 | runOpts = Arrays.asList(runMatch.group(1).trim().split(" +")); |
aoqi@0 | 117 | } |
aoqi@0 | 118 | if (javaPat.matcher(line).matches()) |
aoqi@0 | 119 | break; |
aoqi@0 | 120 | } |
aoqi@0 | 121 | } catch (IOException e) { |
aoqi@0 | 122 | throw new Error(e); |
aoqi@0 | 123 | } |
aoqi@0 | 124 | } |
aoqi@0 | 125 | |
aoqi@0 | 126 | private void foundInfo(File file) { |
aoqi@0 | 127 | if (infoFile != null && !infoFile.equals(file)) |
aoqi@0 | 128 | throw new Error("multiple info files found: " + infoFile + ", " + file); |
aoqi@0 | 129 | infoFile = file; |
aoqi@0 | 130 | } |
aoqi@0 | 131 | |
aoqi@0 | 132 | String getName() { |
aoqi@0 | 133 | return file.getName(); |
aoqi@0 | 134 | } |
aoqi@0 | 135 | |
aoqi@0 | 136 | /** |
aoqi@0 | 137 | * Get the set of resource keys that this test declares it will generate |
aoqi@0 | 138 | * when it is run. |
aoqi@0 | 139 | */ |
aoqi@0 | 140 | Set<String> getDeclaredKeys() { |
aoqi@0 | 141 | return declaredKeys; |
aoqi@0 | 142 | } |
aoqi@0 | 143 | |
aoqi@0 | 144 | /** |
aoqi@0 | 145 | * Get the set of resource keys that this test generates when it is run. |
aoqi@0 | 146 | * The test will be run if it has not already been run. |
aoqi@0 | 147 | */ |
aoqi@0 | 148 | Set<String> getActualKeys() { |
aoqi@0 | 149 | if (actualKeys == null) |
aoqi@0 | 150 | actualKeys = run(false); |
aoqi@0 | 151 | return actualKeys; |
aoqi@0 | 152 | } |
aoqi@0 | 153 | |
aoqi@0 | 154 | /** |
aoqi@0 | 155 | * Run the test. Information in the test header is used to determine |
aoqi@0 | 156 | * how to run the test. |
aoqi@0 | 157 | */ |
aoqi@0 | 158 | void run(PrintWriter out, boolean raw, boolean verbose) { |
aoqi@0 | 159 | if (out == null) |
aoqi@0 | 160 | throw new NullPointerException(); |
aoqi@0 | 161 | try { |
aoqi@0 | 162 | run(out, null, raw, verbose); |
aoqi@0 | 163 | } catch (IOException e) { |
aoqi@0 | 164 | e.printStackTrace(out); |
aoqi@0 | 165 | } |
aoqi@0 | 166 | } |
aoqi@0 | 167 | |
aoqi@0 | 168 | Set<String> run(boolean verbose) { |
aoqi@0 | 169 | Set<String> keys = new TreeSet<String>(); |
aoqi@0 | 170 | try { |
aoqi@0 | 171 | run(null, keys, true, verbose); |
aoqi@0 | 172 | } catch (IOException e) { |
aoqi@0 | 173 | e.printStackTrace(System.err); |
aoqi@0 | 174 | } |
aoqi@0 | 175 | return keys; |
aoqi@0 | 176 | } |
aoqi@0 | 177 | |
aoqi@0 | 178 | /** |
aoqi@0 | 179 | * Run the test. Information in the test header is used to determine |
aoqi@0 | 180 | * how to run the test. |
aoqi@0 | 181 | */ |
aoqi@0 | 182 | private void run(PrintWriter out, Set<String> keys, boolean raw, boolean verbose) |
aoqi@0 | 183 | throws IOException { |
aoqi@0 | 184 | ClassLoader loader = getClass().getClassLoader(); |
aoqi@0 | 185 | if (supportFiles.size() > 0) { |
aoqi@0 | 186 | File supportDir = new File(tempDir, "support"); |
aoqi@0 | 187 | supportDir.mkdirs(); |
aoqi@0 | 188 | clean(supportDir); |
aoqi@0 | 189 | List<String> sOpts = Arrays.asList("-d", supportDir.getPath()); |
aoqi@0 | 190 | new Jsr199Compiler(verbose).run(null, null, false, sOpts, procFiles); |
aoqi@0 | 191 | URLClassLoader ucl = |
aoqi@0 | 192 | new URLClassLoader(new URL[] { supportDir.toURI().toURL() }, loader); |
aoqi@0 | 193 | loader = ucl; |
aoqi@0 | 194 | } |
aoqi@0 | 195 | |
aoqi@0 | 196 | File classesDir = new File(tempDir, "classes"); |
aoqi@0 | 197 | classesDir.mkdirs(); |
aoqi@0 | 198 | clean(classesDir); |
aoqi@0 | 199 | |
aoqi@0 | 200 | List<String> opts = new ArrayList<String>(); |
aoqi@0 | 201 | opts.add("-d"); |
aoqi@0 | 202 | opts.add(classesDir.getPath()); |
aoqi@0 | 203 | if (options != null) |
aoqi@0 | 204 | opts.addAll(options); |
aoqi@0 | 205 | |
aoqi@0 | 206 | if (procFiles.size() > 0) { |
aoqi@0 | 207 | List<String> pOpts = Arrays.asList("-d", classesDir.getPath()); |
aoqi@0 | 208 | new Jsr199Compiler(verbose).run(null, null, false, pOpts, procFiles); |
aoqi@0 | 209 | opts.add("-classpath"); // avoid using -processorpath for now |
aoqi@0 | 210 | opts.add(classesDir.getPath()); |
aoqi@0 | 211 | createAnnotationServicesFile(classesDir, procFiles); |
aoqi@0 | 212 | } else if (options != null) { |
aoqi@0 | 213 | int i = options.indexOf("-processor"); |
aoqi@0 | 214 | // check for built-in anno-processor(s) |
aoqi@0 | 215 | if (i != -1 && options.get(i + 1).equals("DocCommentProcessor")) { |
aoqi@0 | 216 | opts.add("-classpath"); |
aoqi@0 | 217 | opts.add(System.getProperty("test.classes")); |
aoqi@0 | 218 | } |
aoqi@0 | 219 | } |
aoqi@0 | 220 | |
aoqi@0 | 221 | if (srcPathDir != null) { |
aoqi@0 | 222 | opts.add("-sourcepath"); |
aoqi@0 | 223 | opts.add(srcPathDir.getPath()); |
aoqi@0 | 224 | } |
aoqi@0 | 225 | |
aoqi@0 | 226 | try { |
aoqi@0 | 227 | Compiler c = Compiler.getCompiler(runOpts, verbose); |
aoqi@0 | 228 | c.run(out, keys, raw, opts, srcFiles); |
aoqi@0 | 229 | } catch (IllegalArgumentException e) { |
aoqi@0 | 230 | if (out != null) { |
aoqi@0 | 231 | out.println("Invalid value for run tag: " + runOpts); |
aoqi@0 | 232 | } |
aoqi@0 | 233 | } |
aoqi@0 | 234 | } |
aoqi@0 | 235 | |
aoqi@0 | 236 | void createAnnotationServicesFile(File dir, List<File> procFiles) throws IOException { |
aoqi@0 | 237 | File servicesDir = new File(new File(dir, "META-INF"), "services"); |
aoqi@0 | 238 | servicesDir.mkdirs(); |
aoqi@0 | 239 | File annoServices = new File(servicesDir, Processor.class.getName()); |
aoqi@0 | 240 | Writer out = new FileWriter(annoServices); |
aoqi@0 | 241 | try { |
aoqi@0 | 242 | for (File f: procFiles) { |
aoqi@0 | 243 | out.write(f.getName().toString().replace(".java", "")); |
aoqi@0 | 244 | } |
aoqi@0 | 245 | } finally { |
aoqi@0 | 246 | out.close(); |
aoqi@0 | 247 | } |
aoqi@0 | 248 | } |
aoqi@0 | 249 | |
aoqi@0 | 250 | @Override |
aoqi@0 | 251 | public int compareTo(Example e) { |
aoqi@0 | 252 | return file.compareTo(e.file); |
aoqi@0 | 253 | } |
aoqi@0 | 254 | |
aoqi@0 | 255 | @Override |
aoqi@0 | 256 | public String toString() { |
aoqi@0 | 257 | return file.getPath(); |
aoqi@0 | 258 | } |
aoqi@0 | 259 | |
aoqi@0 | 260 | /** |
aoqi@0 | 261 | * Read the contents of a file. |
aoqi@0 | 262 | */ |
aoqi@0 | 263 | private String read(File f) throws IOException { |
aoqi@0 | 264 | byte[] bytes = new byte[(int) f.length()]; |
aoqi@0 | 265 | DataInputStream in = new DataInputStream(new FileInputStream(f)); |
aoqi@0 | 266 | try { |
aoqi@0 | 267 | in.readFully(bytes); |
aoqi@0 | 268 | } finally { |
aoqi@0 | 269 | in.close(); |
aoqi@0 | 270 | } |
aoqi@0 | 271 | return new String(bytes); |
aoqi@0 | 272 | } |
aoqi@0 | 273 | |
aoqi@0 | 274 | /** |
aoqi@0 | 275 | * Clean the contents of a directory. |
aoqi@0 | 276 | */ |
aoqi@0 | 277 | boolean clean(File dir) { |
aoqi@0 | 278 | boolean ok = true; |
aoqi@0 | 279 | for (File f: dir.listFiles()) { |
aoqi@0 | 280 | if (f.isDirectory()) |
aoqi@0 | 281 | ok &= clean(f); |
aoqi@0 | 282 | ok &= f.delete(); |
aoqi@0 | 283 | } |
aoqi@0 | 284 | return ok; |
aoqi@0 | 285 | } |
aoqi@0 | 286 | |
aoqi@0 | 287 | File file; |
aoqi@0 | 288 | List<File> srcFiles; |
aoqi@0 | 289 | List<File> procFiles; |
aoqi@0 | 290 | File srcPathDir; |
aoqi@0 | 291 | List<File> srcPathFiles; |
aoqi@0 | 292 | List<File> supportFiles; |
aoqi@0 | 293 | File infoFile; |
aoqi@0 | 294 | private List<String> runOpts; |
aoqi@0 | 295 | private List<String> options; |
aoqi@0 | 296 | private Set<String> actualKeys; |
aoqi@0 | 297 | private Set<String> declaredKeys; |
aoqi@0 | 298 | |
aoqi@0 | 299 | static File tempDir = (System.getProperty("test.src") != null) ? |
aoqi@0 | 300 | new File(System.getProperty("user.dir")): |
aoqi@0 | 301 | new File(System.getProperty("java.io.tmpdir")); |
aoqi@0 | 302 | |
aoqi@0 | 303 | static void setTempDir(File tempDir) { |
aoqi@0 | 304 | Example.tempDir = tempDir; |
aoqi@0 | 305 | } |
aoqi@0 | 306 | |
aoqi@0 | 307 | abstract static class Compiler { |
aoqi@0 | 308 | interface Factory { |
aoqi@0 | 309 | Compiler getCompiler(List<String> opts, boolean verbose); |
aoqi@0 | 310 | } |
aoqi@0 | 311 | |
aoqi@0 | 312 | static class DefaultFactory implements Factory { |
aoqi@0 | 313 | public Compiler getCompiler(List<String> opts, boolean verbose) { |
aoqi@0 | 314 | String first; |
aoqi@0 | 315 | String[] rest; |
aoqi@0 | 316 | if (opts == null || opts.isEmpty()) { |
aoqi@0 | 317 | first = null; |
aoqi@0 | 318 | rest = new String[0]; |
aoqi@0 | 319 | } else { |
aoqi@0 | 320 | first = opts.get(0); |
aoqi@0 | 321 | rest = opts.subList(1, opts.size()).toArray(new String[opts.size() - 1]); |
aoqi@0 | 322 | } |
aoqi@0 | 323 | if (first == null || first.equals("jsr199")) |
aoqi@0 | 324 | return new Jsr199Compiler(verbose, rest); |
aoqi@0 | 325 | else if (first.equals("simple")) |
aoqi@0 | 326 | return new SimpleCompiler(verbose); |
aoqi@0 | 327 | else if (first.equals("backdoor")) |
aoqi@0 | 328 | return new BackdoorCompiler(verbose); |
aoqi@0 | 329 | else |
aoqi@0 | 330 | throw new IllegalArgumentException(first); |
aoqi@0 | 331 | } |
aoqi@0 | 332 | } |
aoqi@0 | 333 | |
aoqi@0 | 334 | static Factory factory; |
aoqi@0 | 335 | |
aoqi@0 | 336 | static Compiler getCompiler(List<String> opts, boolean verbose) { |
aoqi@0 | 337 | if (factory == null) |
aoqi@0 | 338 | factory = new DefaultFactory(); |
aoqi@0 | 339 | |
aoqi@0 | 340 | return factory.getCompiler(opts, verbose); |
aoqi@0 | 341 | } |
aoqi@0 | 342 | |
aoqi@0 | 343 | protected Compiler(boolean verbose) { |
aoqi@0 | 344 | this.verbose = verbose; |
aoqi@0 | 345 | } |
aoqi@0 | 346 | |
aoqi@0 | 347 | abstract boolean run(PrintWriter out, Set<String> keys, boolean raw, |
aoqi@0 | 348 | List<String> opts, List<File> files); |
aoqi@0 | 349 | |
aoqi@0 | 350 | void setSupportClassLoader(ClassLoader cl) { |
aoqi@0 | 351 | loader = cl; |
aoqi@0 | 352 | } |
aoqi@0 | 353 | |
aoqi@0 | 354 | protected ClassLoader loader; |
aoqi@0 | 355 | protected boolean verbose; |
aoqi@0 | 356 | } |
aoqi@0 | 357 | |
aoqi@0 | 358 | /** |
aoqi@0 | 359 | * Compile using the JSR 199 API. The diagnostics generated are |
aoqi@0 | 360 | * scanned for resource keys. Not all diagnostic keys are generated |
aoqi@0 | 361 | * via the JSR 199 API -- for example, rich diagnostics are not directly |
aoqi@0 | 362 | * accessible, and some diagnostics generated by the file manager may |
aoqi@0 | 363 | * not be generated (for example, the JSR 199 file manager does not see |
aoqi@0 | 364 | * -Xlint:path). |
aoqi@0 | 365 | */ |
aoqi@0 | 366 | static class Jsr199Compiler extends Compiler { |
aoqi@0 | 367 | List<String> fmOpts; |
aoqi@0 | 368 | |
aoqi@0 | 369 | Jsr199Compiler(boolean verbose, String... args) { |
aoqi@0 | 370 | super(verbose); |
aoqi@0 | 371 | for (int i = 0; i < args.length; i++) { |
aoqi@0 | 372 | String arg = args[i]; |
aoqi@0 | 373 | if (arg.equals("-filemanager") && (i + 1 < args.length)) { |
aoqi@0 | 374 | fmOpts = Arrays.asList(args[++i].split(",")); |
aoqi@0 | 375 | } else |
aoqi@0 | 376 | throw new IllegalArgumentException(arg); |
aoqi@0 | 377 | } |
aoqi@0 | 378 | } |
aoqi@0 | 379 | |
aoqi@0 | 380 | @Override |
aoqi@0 | 381 | boolean run(PrintWriter out, Set<String> keys, boolean raw, List<String> opts, List<File> files) { |
aoqi@0 | 382 | if (out != null && keys != null) |
aoqi@0 | 383 | throw new IllegalArgumentException(); |
aoqi@0 | 384 | |
aoqi@0 | 385 | if (verbose) |
aoqi@0 | 386 | System.err.println("run_jsr199: " + opts + " " + files); |
aoqi@0 | 387 | |
aoqi@0 | 388 | DiagnosticCollector<JavaFileObject> dc = null; |
aoqi@0 | 389 | if (keys != null) |
aoqi@0 | 390 | dc = new DiagnosticCollector<JavaFileObject>(); |
aoqi@0 | 391 | |
aoqi@0 | 392 | if (raw) { |
aoqi@0 | 393 | List<String> newOpts = new ArrayList<String>(); |
aoqi@0 | 394 | newOpts.add("-XDrawDiagnostics"); |
aoqi@0 | 395 | newOpts.addAll(opts); |
aoqi@0 | 396 | opts = newOpts; |
aoqi@0 | 397 | } |
aoqi@0 | 398 | |
aoqi@0 | 399 | JavaCompiler c = ToolProvider.getSystemJavaCompiler(); |
aoqi@0 | 400 | |
aoqi@0 | 401 | StandardJavaFileManager fm = c.getStandardFileManager(dc, null, null); |
aoqi@0 | 402 | if (fmOpts != null) |
aoqi@0 | 403 | fm = new FileManager(fm, fmOpts); |
aoqi@0 | 404 | |
aoqi@0 | 405 | Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(files); |
aoqi@0 | 406 | |
aoqi@0 | 407 | CompilationTask t = c.getTask(out, fm, dc, opts, null, fos); |
aoqi@0 | 408 | Boolean ok = t.call(); |
aoqi@0 | 409 | |
aoqi@0 | 410 | if (keys != null) { |
aoqi@0 | 411 | for (Diagnostic<? extends JavaFileObject> d: dc.getDiagnostics()) { |
aoqi@0 | 412 | scanForKeys(unwrap(d), keys); |
aoqi@0 | 413 | } |
aoqi@0 | 414 | } |
aoqi@0 | 415 | |
aoqi@0 | 416 | return ok; |
aoqi@0 | 417 | } |
aoqi@0 | 418 | |
aoqi@0 | 419 | /** |
aoqi@0 | 420 | * Scan a diagnostic for resource keys. This will not detect additional |
aoqi@0 | 421 | * sub diagnostics that might be generated by a rich diagnostic formatter. |
aoqi@0 | 422 | */ |
aoqi@0 | 423 | private static void scanForKeys(JCDiagnostic d, Set<String> keys) { |
aoqi@0 | 424 | keys.add(d.getCode()); |
aoqi@0 | 425 | for (Object o: d.getArgs()) { |
aoqi@0 | 426 | if (o instanceof JCDiagnostic) { |
aoqi@0 | 427 | scanForKeys((JCDiagnostic) o, keys); |
aoqi@0 | 428 | } |
aoqi@0 | 429 | } |
aoqi@0 | 430 | for (JCDiagnostic sd: d.getSubdiagnostics()) |
aoqi@0 | 431 | scanForKeys(sd, keys); |
aoqi@0 | 432 | } |
aoqi@0 | 433 | |
aoqi@0 | 434 | private JCDiagnostic unwrap(Diagnostic<? extends JavaFileObject> diagnostic) { |
aoqi@0 | 435 | if (diagnostic instanceof JCDiagnostic) |
aoqi@0 | 436 | return (JCDiagnostic) diagnostic; |
aoqi@0 | 437 | if (diagnostic instanceof ClientCodeWrapper.DiagnosticSourceUnwrapper) |
aoqi@0 | 438 | return ((ClientCodeWrapper.DiagnosticSourceUnwrapper)diagnostic).d; |
aoqi@0 | 439 | throw new IllegalArgumentException(); |
aoqi@0 | 440 | } |
aoqi@0 | 441 | } |
aoqi@0 | 442 | |
aoqi@0 | 443 | /** |
aoqi@0 | 444 | * Run the test using the standard simple entry point. |
aoqi@0 | 445 | */ |
aoqi@0 | 446 | static class SimpleCompiler extends Compiler { |
aoqi@0 | 447 | SimpleCompiler(boolean verbose) { |
aoqi@0 | 448 | super(verbose); |
aoqi@0 | 449 | } |
aoqi@0 | 450 | |
aoqi@0 | 451 | @Override |
aoqi@0 | 452 | boolean run(PrintWriter out, Set<String> keys, boolean raw, List<String> opts, List<File> files) { |
aoqi@0 | 453 | if (out != null && keys != null) |
aoqi@0 | 454 | throw new IllegalArgumentException(); |
aoqi@0 | 455 | |
aoqi@0 | 456 | if (verbose) |
aoqi@0 | 457 | System.err.println("run_simple: " + opts + " " + files); |
aoqi@0 | 458 | |
aoqi@0 | 459 | List<String> args = new ArrayList<String>(); |
aoqi@0 | 460 | |
aoqi@0 | 461 | if (keys != null || raw) |
aoqi@0 | 462 | args.add("-XDrawDiagnostics"); |
aoqi@0 | 463 | |
aoqi@0 | 464 | args.addAll(opts); |
aoqi@0 | 465 | for (File f: files) |
aoqi@0 | 466 | args.add(f.getPath()); |
aoqi@0 | 467 | |
aoqi@0 | 468 | StringWriter sw = null; |
aoqi@0 | 469 | PrintWriter pw; |
aoqi@0 | 470 | if (keys != null) { |
aoqi@0 | 471 | sw = new StringWriter(); |
aoqi@0 | 472 | pw = new PrintWriter(sw); |
aoqi@0 | 473 | } else |
aoqi@0 | 474 | pw = out; |
aoqi@0 | 475 | |
aoqi@0 | 476 | int rc = com.sun.tools.javac.Main.compile(args.toArray(new String[args.size()]), pw); |
aoqi@0 | 477 | |
aoqi@0 | 478 | if (keys != null) { |
aoqi@0 | 479 | pw.close(); |
aoqi@0 | 480 | scanForKeys(sw.toString(), keys); |
aoqi@0 | 481 | } |
aoqi@0 | 482 | |
aoqi@0 | 483 | return (rc == 0); |
aoqi@0 | 484 | } |
aoqi@0 | 485 | |
aoqi@0 | 486 | private static void scanForKeys(String text, Set<String> keys) { |
aoqi@0 | 487 | StringTokenizer st = new StringTokenizer(text, " ,\r\n():"); |
aoqi@0 | 488 | while (st.hasMoreElements()) { |
aoqi@0 | 489 | String t = st.nextToken(); |
aoqi@0 | 490 | if (t.startsWith("compiler.")) |
aoqi@0 | 491 | keys.add(t); |
aoqi@0 | 492 | } |
aoqi@0 | 493 | } |
aoqi@0 | 494 | } |
aoqi@0 | 495 | |
aoqi@0 | 496 | static class BackdoorCompiler extends Compiler { |
aoqi@0 | 497 | BackdoorCompiler(boolean verbose) { |
aoqi@0 | 498 | super(verbose); |
aoqi@0 | 499 | } |
aoqi@0 | 500 | |
aoqi@0 | 501 | @Override |
aoqi@0 | 502 | boolean run(PrintWriter out, Set<String> keys, boolean raw, List<String> opts, List<File> files) { |
aoqi@0 | 503 | if (out != null && keys != null) |
aoqi@0 | 504 | throw new IllegalArgumentException(); |
aoqi@0 | 505 | |
aoqi@0 | 506 | if (verbose) |
aoqi@0 | 507 | System.err.println("run_simple: " + opts + " " + files); |
aoqi@0 | 508 | |
aoqi@0 | 509 | List<String> args = new ArrayList<String>(); |
aoqi@0 | 510 | |
aoqi@0 | 511 | if (out != null && raw) |
aoqi@0 | 512 | args.add("-XDrawDiagnostics"); |
aoqi@0 | 513 | |
aoqi@0 | 514 | args.addAll(opts); |
aoqi@0 | 515 | for (File f: files) |
aoqi@0 | 516 | args.add(f.getPath()); |
aoqi@0 | 517 | |
aoqi@0 | 518 | StringWriter sw = null; |
aoqi@0 | 519 | PrintWriter pw; |
aoqi@0 | 520 | if (keys != null) { |
aoqi@0 | 521 | sw = new StringWriter(); |
aoqi@0 | 522 | pw = new PrintWriter(sw); |
aoqi@0 | 523 | } else |
aoqi@0 | 524 | pw = out; |
aoqi@0 | 525 | |
aoqi@0 | 526 | Context c = new Context(); |
aoqi@0 | 527 | JavacFileManager.preRegister(c); // can't create it until Log has been set up |
aoqi@0 | 528 | MessageTracker.preRegister(c, keys); |
aoqi@0 | 529 | Main m = new Main("javac", pw); |
aoqi@0 | 530 | Main.Result rc = m.compile(args.toArray(new String[args.size()]), c); |
aoqi@0 | 531 | |
aoqi@0 | 532 | if (keys != null) { |
aoqi@0 | 533 | pw.close(); |
aoqi@0 | 534 | } |
aoqi@0 | 535 | |
aoqi@0 | 536 | return rc.isOK(); |
aoqi@0 | 537 | } |
aoqi@0 | 538 | |
aoqi@0 | 539 | static class MessageTracker extends JavacMessages { |
aoqi@0 | 540 | |
aoqi@0 | 541 | MessageTracker(Context context) { |
aoqi@0 | 542 | super(context); |
aoqi@0 | 543 | } |
aoqi@0 | 544 | |
aoqi@0 | 545 | static void preRegister(Context c, final Set<String> keys) { |
aoqi@0 | 546 | if (keys != null) { |
aoqi@0 | 547 | c.put(JavacMessages.messagesKey, new Context.Factory<JavacMessages>() { |
aoqi@0 | 548 | public JavacMessages make(Context c) { |
aoqi@0 | 549 | return new MessageTracker(c) { |
aoqi@0 | 550 | @Override |
aoqi@0 | 551 | public String getLocalizedString(Locale l, String key, Object... args) { |
aoqi@0 | 552 | keys.add(key); |
aoqi@0 | 553 | return super.getLocalizedString(l, key, args); |
aoqi@0 | 554 | } |
aoqi@0 | 555 | }; |
aoqi@0 | 556 | } |
aoqi@0 | 557 | }); |
aoqi@0 | 558 | } |
aoqi@0 | 559 | } |
aoqi@0 | 560 | } |
aoqi@0 | 561 | |
aoqi@0 | 562 | } |
aoqi@0 | 563 | } |