private Collection<? extends String> toStringList(List<SpoonFile> files) { List<String> res = new ArrayList<String>(); for (SpoonFile f : files) { if (f.isActualFile()) { res.add(f.toString()); } else { try { File file = File.createTempFile(f.getName(), ".java"); file.deleteOnExit(); IOUtils.copy(f.getContent(), new FileOutputStream(file)); res.add(file.toString()); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } } } return res; }
public T sources(List<SpoonFile> sources) { if (sources == null || sources.size() == 0) { args.add("."); return myself; } for (SpoonFile source : sources) { if (source.isActualFile()) { args.add(source.toString()); } else { try { File file = File.createTempFile(source.getName(), ".java"); file.deleteOnExit(); IOUtils.copy(source.getContent(), new FileOutputStream(file)); args.add(file.toString()); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } } } return myself; }
protected void keepOutdatedFiles(List<SpoonFile> files, Collection<File> outputFiles) { // System.out.println("outputfiles: " + outputFiles); int offset = outputDirectory.getAbsolutePath().length() + 1; Collection<String> relativeOutputPaths = new ArrayList<String>(); for (File f : outputFiles) { relativeOutputPaths.add(f.getAbsolutePath().substring(offset)); } for (SpoonFile sf : new ArrayList<SpoonFile>(files)) { if (forceBuildList.contains(sf)) { continue; } File f = sf.toFile(); for (String s : relativeOutputPaths) { if (f.getAbsolutePath().endsWith(s)) { if (f.lastModified() <= new File(outputDirectory, s).lastModified()) { files.remove(sf); } } } } // System.out.println("filtered: " + files); }
@Override public boolean compile() { initInputClassLoader(); factory .getEnvironment() .debugMessage("compiling sources: " + factory.CompilationUnit().getMap().keySet()); long t = System.currentTimeMillis(); javaCompliance = factory.getEnvironment().getComplianceLevel(); JDTBatchCompiler batchCompiler = createBatchCompiler(true); List<String> args = new ArrayList<String>(); args.add("-1." + javaCompliance); if (encoding != null) { args.add("-encoding"); args.add(encoding); } args.add("-preserveAllLocals"); args.add("-enableJavadoc"); args.add("-noExit"); // args.add("-verbose"); args.add("-proc:none"); if (getBinaryOutputDirectory() != null) { args.add("-d"); args.add(getBinaryOutputDirectory().getAbsolutePath()); } else { args.add("-d"); args.add("none"); } // args.add("-d"); // args.add(getDestinationDirectory().toString()); String finalClassPath = null; if (getSourceClasspath() != null) { finalClassPath = computeJdtClassPath(); } else { ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader(); if (currentClassLoader instanceof URLClassLoader) { URL[] urls = ((URLClassLoader) currentClassLoader).getURLs(); if (urls != null && urls.length > 0) { String classpath = "."; for (URL url : urls) { classpath += File.pathSeparator + url.getFile(); } if (classpath != null) { finalClassPath = classpath; } } } } args.add("-cp"); args.add(finalClassPath); if (buildOnlyOutdatedFiles) { // ignore the files that are not outdated if (outputDirectory.exists()) { @SuppressWarnings("unchecked") Collection<File> outputFiles = FileUtils.listFiles(outputDirectory, new String[] {"java"}, true); int offset = outputDirectory.getAbsolutePath().length() + 1; Collection<String> relativeOutputPaths = new ArrayList<String>(); for (File f : outputFiles) { relativeOutputPaths.add(f.getAbsolutePath().substring(offset)); } for (SpoonFile sf : sources.getAllJavaFiles()) { if (factory.CompilationUnit().getMap().containsKey(sf.getPath())) { continue; } File source = sf.toFile(); for (String out : relativeOutputPaths) { if (source.getAbsolutePath().endsWith(out)) { if (source.lastModified() <= new File(outputDirectory, out).lastModified()) { batchCompiler.ignoreFile(new File(outputDirectory, out).getAbsolutePath()); } } } } } args.add(getBinaryOutputDirectory().getAbsolutePath()); } else { args.addAll(toStringList(sources.getAllJavaFiles())); } getFactory().getEnvironment().debugMessage("compile args: " + args); // batchCompiler.batchCompiler.useSingleThread = true; System.setProperty("jdt.compiler.useSingleThread", "true"); batchCompiler.compile(args.toArray(new String[0])); reportProblems(factory.getEnvironment()); factory .getEnvironment() .debugMessage("compiled in " + (System.currentTimeMillis() - t) + " ms"); return probs.size() == 0; }