46 import com.sun.tools.javac.util.Context; 47 import com.sun.tools.javac.util.JavacMessages; 48 import com.sun.tools.javac.util.JCDiagnostic; 49 50 /** 51 * Class to handle example code designed to illustrate javac diagnostic messages. 52 */ 53 class Example implements Comparable<Example> { 54 /* Create an Example from the files found at path. 55 * The head of the file, up to the first Java code, is scanned 56 * for information about the test, such as what resource keys it 57 * generates when run, what options are required to run it, and so on. 58 */ 59 Example(File file) { 60 this.file = file; 61 declaredKeys = new TreeSet<String>(); 62 srcFiles = new ArrayList<File>(); 63 procFiles = new ArrayList<File>(); 64 srcPathFiles = new ArrayList<File>(); 65 moduleSourcePathFiles = new ArrayList<File>(); 66 modulePathFiles = new ArrayList<File>(); 67 classPathFiles = new ArrayList<File>(); 68 additionalFiles = new ArrayList<File>(); 69 nonEmptySrcFiles = new ArrayList<File>(); 70 71 findFiles(file, srcFiles); 72 for (File f: srcFiles) { 73 parse(f); 74 } 75 76 if (infoFile == null) 77 throw new Error("Example " + file + " has no info file"); 78 } 79 80 private void findFiles(File f, List<File> files) { 81 if (f.isDirectory()) { 82 for (File c: f.listFiles()) { 83 if (files == srcFiles && c.getName().equals("processors")) 84 findFiles(c, procFiles); 85 else if (files == srcFiles && c.getName().equals("sourcepath")) { 86 srcPathDir = c; 87 findFiles(c, srcPathFiles); 88 } else if (files == srcFiles && c.getName().equals("modulesourcepath")) { 89 moduleSourcePathDir = c; 90 findFiles(c, moduleSourcePathFiles); 91 } else if (files == srcFiles && c.getName().equals("additional")) { 92 additionalFilesDir = c; 93 findFiles(c, additionalFiles); 94 } else if (files == srcFiles && c.getName().equals("modulepath")) { 95 findFiles(c, modulePathFiles); 96 } else if (files == srcFiles && c.getName().equals("classpath")) { 97 findFiles(c, classPathFiles); 98 } else { 99 findFiles(c, files); 100 } 101 } 102 } else if (f.isFile()) { 103 if (f.getName().endsWith(".java")) { 104 files.add(f); 105 } else if (f.getName().equals("modulesourcepath")) { 106 moduleSourcePathDir = f; 107 } 108 } 109 } 110 255 opts.add("-classpath"); 256 opts.add(System.getProperty("test.classes")); 257 } 258 } 259 260 List<File> files = srcFiles; 261 262 if (srcPathDir != null) { 263 opts.add("-sourcepath"); 264 opts.add(srcPathDir.getPath()); 265 } 266 267 if (moduleSourcePathDir != null) { 268 opts.add("--module-source-path"); 269 opts.add(moduleSourcePathDir.getPath()); 270 files = new ArrayList<>(); 271 files.addAll(moduleSourcePathFiles); 272 files.addAll(nonEmptySrcFiles); // srcFiles containing declarations 273 } 274 275 if (additionalFiles.size() > 0) { 276 List<String> sOpts = Arrays.asList("-d", classesDir.getPath()); 277 new Jsr199Compiler(verbose).run(null, null, false, sOpts, additionalFiles); 278 } 279 280 try { 281 Compiler c = Compiler.getCompiler(runOpts, verbose); 282 c.run(out, keys, raw, opts, files); 283 } catch (IllegalArgumentException e) { 284 if (out != null) { 285 out.println("Invalid value for run tag: " + runOpts); 286 } 287 } 288 } 289 290 void createAnnotationServicesFile(File dir, List<File> procFiles) throws IOException { 291 File servicesDir = new File(new File(dir, "META-INF"), "services"); 292 servicesDir.mkdirs(); 293 File annoServices = new File(servicesDir, Processor.class.getName()); 294 Writer out = new FileWriter(annoServices); 326 } 327 328 /** 329 * Clean the contents of a directory. 330 */ 331 boolean clean(File dir) { 332 boolean ok = true; 333 for (File f: dir.listFiles()) { 334 if (f.isDirectory()) 335 ok &= clean(f); 336 ok &= f.delete(); 337 } 338 return ok; 339 } 340 341 File file; 342 List<File> srcFiles; 343 List<File> procFiles; 344 File srcPathDir; 345 File moduleSourcePathDir; 346 File additionalFilesDir; 347 List<File> srcPathFiles; 348 List<File> moduleSourcePathFiles; 349 List<File> modulePathFiles; 350 List<File> classPathFiles; 351 List<File> additionalFiles; 352 List<File> nonEmptySrcFiles; 353 File infoFile; 354 private List<String> runOpts; 355 private List<String> options; 356 private Set<String> actualKeys; 357 private Set<String> declaredKeys; 358 359 static File tempDir = (System.getProperty("test.src") != null) ? 360 new File(System.getProperty("user.dir")): 361 new File(System.getProperty("java.io.tmpdir")); 362 363 static void setTempDir(File tempDir) { 364 Example.tempDir = tempDir; 365 } 366 367 abstract static class Compiler { 368 interface Factory { | 46 import com.sun.tools.javac.util.Context; 47 import com.sun.tools.javac.util.JavacMessages; 48 import com.sun.tools.javac.util.JCDiagnostic; 49 50 /** 51 * Class to handle example code designed to illustrate javac diagnostic messages. 52 */ 53 class Example implements Comparable<Example> { 54 /* Create an Example from the files found at path. 55 * The head of the file, up to the first Java code, is scanned 56 * for information about the test, such as what resource keys it 57 * generates when run, what options are required to run it, and so on. 58 */ 59 Example(File file) { 60 this.file = file; 61 declaredKeys = new TreeSet<String>(); 62 srcFiles = new ArrayList<File>(); 63 procFiles = new ArrayList<File>(); 64 srcPathFiles = new ArrayList<File>(); 65 moduleSourcePathFiles = new ArrayList<File>(); 66 patchModulePathFiles = new ArrayList<File>(); 67 modulePathFiles = new ArrayList<File>(); 68 classPathFiles = new ArrayList<File>(); 69 additionalFiles = new ArrayList<File>(); 70 nonEmptySrcFiles = new ArrayList<File>(); 71 72 findFiles(file, srcFiles); 73 for (File f: srcFiles) { 74 parse(f); 75 } 76 77 if (infoFile == null) 78 throw new Error("Example " + file + " has no info file"); 79 } 80 81 private void findFiles(File f, List<File> files) { 82 if (f.isDirectory()) { 83 for (File c: f.listFiles()) { 84 if (files == srcFiles && c.getName().equals("processors")) 85 findFiles(c, procFiles); 86 else if (files == srcFiles && c.getName().equals("sourcepath")) { 87 srcPathDir = c; 88 findFiles(c, srcPathFiles); 89 } else if (files == srcFiles && c.getName().equals("modulesourcepath")) { 90 moduleSourcePathDir = c; 91 findFiles(c, moduleSourcePathFiles); 92 } else if (files == srcFiles && c.getName().equals("patchmodule")) { 93 patchModulePathDir = c; 94 findFiles(c, patchModulePathFiles); 95 } else if (files == srcFiles && c.getName().equals("additional")) { 96 additionalFilesDir = c; 97 findFiles(c, additionalFiles); 98 } else if (files == srcFiles && c.getName().equals("modulepath")) { 99 findFiles(c, modulePathFiles); 100 } else if (files == srcFiles && c.getName().equals("classpath")) { 101 findFiles(c, classPathFiles); 102 } else { 103 findFiles(c, files); 104 } 105 } 106 } else if (f.isFile()) { 107 if (f.getName().endsWith(".java")) { 108 files.add(f); 109 } else if (f.getName().equals("modulesourcepath")) { 110 moduleSourcePathDir = f; 111 } 112 } 113 } 114 259 opts.add("-classpath"); 260 opts.add(System.getProperty("test.classes")); 261 } 262 } 263 264 List<File> files = srcFiles; 265 266 if (srcPathDir != null) { 267 opts.add("-sourcepath"); 268 opts.add(srcPathDir.getPath()); 269 } 270 271 if (moduleSourcePathDir != null) { 272 opts.add("--module-source-path"); 273 opts.add(moduleSourcePathDir.getPath()); 274 files = new ArrayList<>(); 275 files.addAll(moduleSourcePathFiles); 276 files.addAll(nonEmptySrcFiles); // srcFiles containing declarations 277 } 278 279 if (patchModulePathDir != null) { 280 for (File mod : patchModulePathDir.listFiles()) { 281 opts.add("--patch-module"); 282 opts.add(mod.getName() + "=" + mod.getPath()); 283 } 284 files = new ArrayList<>(); 285 files.addAll(patchModulePathFiles); 286 files.addAll(nonEmptySrcFiles); // srcFiles containing declarations 287 } 288 289 if (additionalFiles.size() > 0) { 290 List<String> sOpts = Arrays.asList("-d", classesDir.getPath()); 291 new Jsr199Compiler(verbose).run(null, null, false, sOpts, additionalFiles); 292 } 293 294 try { 295 Compiler c = Compiler.getCompiler(runOpts, verbose); 296 c.run(out, keys, raw, opts, files); 297 } catch (IllegalArgumentException e) { 298 if (out != null) { 299 out.println("Invalid value for run tag: " + runOpts); 300 } 301 } 302 } 303 304 void createAnnotationServicesFile(File dir, List<File> procFiles) throws IOException { 305 File servicesDir = new File(new File(dir, "META-INF"), "services"); 306 servicesDir.mkdirs(); 307 File annoServices = new File(servicesDir, Processor.class.getName()); 308 Writer out = new FileWriter(annoServices); 340 } 341 342 /** 343 * Clean the contents of a directory. 344 */ 345 boolean clean(File dir) { 346 boolean ok = true; 347 for (File f: dir.listFiles()) { 348 if (f.isDirectory()) 349 ok &= clean(f); 350 ok &= f.delete(); 351 } 352 return ok; 353 } 354 355 File file; 356 List<File> srcFiles; 357 List<File> procFiles; 358 File srcPathDir; 359 File moduleSourcePathDir; 360 File patchModulePathDir; 361 File additionalFilesDir; 362 List<File> srcPathFiles; 363 List<File> moduleSourcePathFiles; 364 List<File> patchModulePathFiles; 365 List<File> modulePathFiles; 366 List<File> classPathFiles; 367 List<File> additionalFiles; 368 List<File> nonEmptySrcFiles; 369 File infoFile; 370 private List<String> runOpts; 371 private List<String> options; 372 private Set<String> actualKeys; 373 private Set<String> declaredKeys; 374 375 static File tempDir = (System.getProperty("test.src") != null) ? 376 new File(System.getProperty("user.dir")): 377 new File(System.getProperty("java.io.tmpdir")); 378 379 static void setTempDir(File tempDir) { 380 Example.tempDir = tempDir; 381 } 382 383 abstract static class Compiler { 384 interface Factory { |