8144888: ToolBox should close any file manager it opens

Reviewed-by: vromero
This commit is contained in:
Jonathan Gibbons 2015-12-15 15:30:35 -08:00
parent 9c33078299
commit 351fe82e08

View File

@ -124,9 +124,6 @@ public class ToolBox {
/** The stream used for logging output. */ /** The stream used for logging output. */
public PrintStream out = System.err; public PrintStream out = System.err;
JavaCompiler compiler;
StandardJavaFileManager standardJavaFileManager;
/** /**
* Checks if the host OS is some version of Windows. * Checks if the host OS is some version of Windows.
* @return true if the host OS is some version of Windows * @return true if the host OS is some version of Windows
@ -868,15 +865,18 @@ public class ToolBox {
*/ */
public class JavacTask extends AbstractTask<JavacTask> { public class JavacTask extends AbstractTask<JavacTask> {
private boolean includeStandardOptions; private boolean includeStandardOptions;
private String classpath; private List<Path> classpath;
private String sourcepath; private List<Path> sourcepath;
private String outdir; private Path outdir;
private List<String> options; private List<String> options;
private List<String> classes; private List<String> classes;
private List<String> files; private List<String> files;
private List<JavaFileObject> fileObjects; private List<JavaFileObject> fileObjects;
private JavaFileManager fileManager; private JavaFileManager fileManager;
private JavaCompiler compiler;
private StandardJavaFileManager internalFileManager;
/** /**
* Creates a task to execute {@code javac} using API mode. * Creates a task to execute {@code javac} using API mode.
*/ */
@ -898,7 +898,20 @@ public class ToolBox {
* @return this task object * @return this task object
*/ */
public JavacTask classpath(String classpath) { public JavacTask classpath(String classpath) {
this.classpath = classpath; this.classpath = Stream.of(classpath.split(File.pathSeparator))
.filter(s -> !s.isEmpty())
.map(s -> Paths.get(s))
.collect(Collectors.toList());
return this;
}
/**
* Sets the classpath.
* @param classpath the classpath
* @return this task object
*/
public JavacTask classpath(Path... classpath) {
this.classpath = Arrays.asList(classpath);
return this; return this;
} }
@ -908,7 +921,20 @@ public class ToolBox {
* @return this task object * @return this task object
*/ */
public JavacTask sourcepath(String sourcepath) { public JavacTask sourcepath(String sourcepath) {
this.sourcepath = sourcepath; this.sourcepath = Stream.of(sourcepath.split(File.pathSeparator))
.filter(s -> !s.isEmpty())
.map(s -> Paths.get(s))
.collect(Collectors.toList());
return this;
}
/**
* Sets the sourcepath.
* @param classpath the sourcepath
* @return this task object
*/
public JavacTask sourcepath(Path... sourcepath) {
this.sourcepath = Arrays.asList(sourcepath);
return this; return this;
} }
@ -918,6 +944,16 @@ public class ToolBox {
* @return this task object * @return this task object
*/ */
public JavacTask outdir(String outdir) { public JavacTask outdir(String outdir) {
this.outdir = Paths.get(outdir);
return this;
}
/**
* Sets the output directory.
* @param outdir the output directory
* @return this task object
*/
public JavacTask outdir(Path outdir) {
this.outdir = outdir; this.outdir = outdir;
return this; return this;
} }
@ -1039,6 +1075,7 @@ public class ToolBox {
} }
private int runAPI(PrintWriter pw) throws IOException { private int runAPI(PrintWriter pw) throws IOException {
try {
// if (compiler == null) { // if (compiler == null) {
// TODO: allow this to be set externally // TODO: allow this to be set externally
// compiler = ToolProvider.getSystemJavaCompiler(); // compiler = ToolProvider.getSystemJavaCompiler();
@ -1046,13 +1083,13 @@ public class ToolBox {
// } // }
if (fileManager == null) if (fileManager == null)
fileManager = compiler.getStandardFileManager(null, null, null); fileManager = internalFileManager = compiler.getStandardFileManager(null, null, null);
if (outdir != null) if (outdir != null)
setLocation(StandardLocation.CLASS_OUTPUT, toFiles(outdir)); setLocationFromPaths(StandardLocation.CLASS_OUTPUT, Collections.singletonList(outdir));
if (classpath != null) if (classpath != null)
setLocation(StandardLocation.CLASS_PATH, toFiles(classpath)); setLocationFromPaths(StandardLocation.CLASS_PATH, classpath);
if (sourcepath != null) if (sourcepath != null)
setLocation(StandardLocation.SOURCE_PATH, toFiles(sourcepath)); setLocationFromPaths(StandardLocation.SOURCE_PATH, sourcepath);
List<String> allOpts = new ArrayList<>(); List<String> allOpts = new ArrayList<>();
if (options != null) if (options != null)
allOpts.addAll(options); allOpts.addAll(options);
@ -1065,12 +1102,16 @@ public class ToolBox {
classes, classes,
allFiles); allFiles);
return ((JavacTaskImpl) task).doCall().exitCode; return ((JavacTaskImpl) task).doCall().exitCode;
} finally {
if (internalFileManager != null)
internalFileManager.close();
}
} }
private void setLocation(StandardLocation location, List<File> files) throws IOException { private void setLocationFromPaths(StandardLocation location, List<Path> files) throws IOException {
if (!(fileManager instanceof StandardJavaFileManager)) if (!(fileManager instanceof StandardJavaFileManager))
throw new IllegalStateException("not a StandardJavaFileManager"); throw new IllegalStateException("not a StandardJavaFileManager");
((StandardJavaFileManager) fileManager).setLocation(location, files); ((StandardJavaFileManager) fileManager).setLocationFromPaths(location, files);
} }
private int runCommand(PrintWriter pw) { private int runCommand(PrintWriter pw) {
@ -1105,15 +1146,15 @@ public class ToolBox {
args.addAll(options); args.addAll(options);
if (outdir != null) { if (outdir != null) {
args.add("-d"); args.add("-d");
args.add(outdir); args.add(outdir.toString());
} }
if (classpath != null) { if (classpath != null) {
args.add("-classpath"); args.add("-classpath");
args.add(classpath); args.add(toSearchPath(classpath));
} }
if (sourcepath != null) { if (sourcepath != null) {
args.add("-sourcepath"); args.add("-sourcepath");
args.add(sourcepath); args.add(toSearchPath(sourcepath));
} }
if (classes != null) if (classes != null)
args.addAll(classes); args.addAll(classes);
@ -1123,23 +1164,20 @@ public class ToolBox {
return args; return args;
} }
private List<File> toFiles(String path) { private String toSearchPath(List<Path> files) {
List<File> result = new ArrayList<>(); return files.stream()
for (String s : path.split(File.pathSeparator)) { .map(Path::toString)
if (!s.isEmpty()) .collect(Collectors.joining(File.pathSeparator));
result.add(new File(s));
}
return result;
} }
private Iterable<? extends JavaFileObject> joinFiles( private Iterable<? extends JavaFileObject> joinFiles(
List<String> files, List<JavaFileObject> fileObjects) { List<String> files, List<JavaFileObject> fileObjects) {
if (files == null) if (files == null)
return fileObjects; return fileObjects;
if (standardJavaFileManager == null) if (internalFileManager == null)
standardJavaFileManager = compiler.getStandardFileManager(null, null, null); internalFileManager = compiler.getStandardFileManager(null, null, null);
Iterable<? extends JavaFileObject> filesAsFileObjects = Iterable<? extends JavaFileObject> filesAsFileObjects =
standardJavaFileManager.getJavaFileObjectsFromStrings(files); internalFileManager.getJavaFileObjectsFromStrings(files);
if (fileObjects == null) if (fileObjects == null)
return filesAsFileObjects; return filesAsFileObjects;
List<JavaFileObject> combinedList = new ArrayList<>(); List<JavaFileObject> combinedList = new ArrayList<>();
@ -1363,6 +1401,15 @@ public class ToolBox {
jar = Paths.get(path); jar = Paths.get(path);
} }
/**
* Creates a JarTask for use with a given jar file.
* @param path the file
*/
public JarTask(Path path) {
this();
jar = path;
}
/** /**
* Sets a manifest for the jar file. * Sets a manifest for the jar file.
* @param manifest the manifest * @param manifest the manifest
@ -1416,6 +1463,16 @@ public class ToolBox {
return this; return this;
} }
/**
* Sets the base directory for files to be written into the jar file.
* @param baseDir the base directory
* @return this task object
*/
public JarTask baseDir(Path baseDir) {
this.baseDir = baseDir;
return this;
}
/** /**
* Sets the files to be written into the jar file. * Sets the files to be written into the jar file.
* @param files the files * @param files the files