8144891: ToolBox should use java.nio.file.Path internally, instead of java.io.File

Reviewed-by: jjg
This commit is contained in:
Christian Stein 2023-05-25 08:56:42 +00:00
parent aaa61899c9
commit d87713440a
6 changed files with 16 additions and 19 deletions

View File

@ -25,13 +25,13 @@ package toolbox;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.file.Path;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.Map;
@ -168,9 +168,9 @@ abstract class AbstractTask<T extends AbstractTask<T>> implements Task {
throw new IllegalStateException();
ProcessBuilder pb = new ProcessBuilder();
if (redirects.get(OutputKind.STDOUT) != null)
pb.redirectOutput(new File(redirects.get(OutputKind.STDOUT)));
pb.redirectOutput(Path.of(redirects.get(OutputKind.STDOUT)).toFile());
if (redirects.get(OutputKind.STDERR) != null)
pb.redirectError(new File(redirects.get(OutputKind.STDERR)));
pb.redirectError(Path.of(redirects.get(OutputKind.STDERR)).toFile());
pb.environment().putAll(envVars);
return pb;
}

View File

@ -25,7 +25,6 @@ package toolbox;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOError;
import java.io.IOException;
import java.io.InputStream;
@ -332,7 +331,7 @@ public class JarTask extends AbstractTask<JarTask> {
String p = base.relativize(file)
.normalize()
.toString()
.replace(File.separatorChar, '/');
.replace(ToolBox.fileSeparatorChar, '/');
JarEntry e = new JarEntry(p);
jos.putNextEntry(e);
try {

View File

@ -23,7 +23,6 @@
package toolbox;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Path;
@ -90,7 +89,7 @@ public class JavacTask extends AbstractTask<JavacTask> {
* @return this task object
*/
public JavacTask classpath(String classpath) {
this.classpath = Stream.of(classpath.split(File.pathSeparator))
this.classpath = Stream.of(classpath.split(ToolBox.pathSeparator))
.filter(s -> !s.isEmpty())
.map(s -> Paths.get(s))
.collect(Collectors.toList());
@ -123,7 +122,7 @@ public class JavacTask extends AbstractTask<JavacTask> {
* @return this task object
*/
public JavacTask sourcepath(String sourcepath) {
this.sourcepath = Stream.of(sourcepath.split(File.pathSeparator))
this.sourcepath = Stream.of(sourcepath.split(ToolBox.pathSeparator))
.filter(s -> !s.isEmpty())
.map(s -> Paths.get(s))
.collect(Collectors.toList());
@ -468,7 +467,7 @@ public class JavacTask extends AbstractTask<JavacTask> {
private String toSearchPath(List<Path> files) {
return files.stream()
.map(Path::toString)
.collect(Collectors.joining(File.pathSeparator));
.collect(Collectors.joining(ToolBox.pathSeparator));
}
private Iterable<? extends JavaFileObject> joinFiles(

View File

@ -23,7 +23,6 @@
package toolbox;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Path;
@ -89,7 +88,7 @@ public class JavadocTask extends AbstractTask<JavadocTask> {
* @return this task object
*/
public JavadocTask classpath(String classpath) {
this.classpath = Stream.of(classpath.split(File.pathSeparator))
this.classpath = Stream.of(classpath.split(ToolBox.pathSeparator))
.filter(s -> !s.isEmpty())
.map(s -> Paths.get(s))
.collect(Collectors.toList());
@ -122,7 +121,7 @@ public class JavadocTask extends AbstractTask<JavadocTask> {
* @return this task object
*/
public JavadocTask sourcepath(String sourcepath) {
this.sourcepath = Stream.of(sourcepath.split(File.pathSeparator))
this.sourcepath = Stream.of(sourcepath.split(ToolBox.pathSeparator))
.filter(s -> !s.isEmpty())
.map(s -> Paths.get(s))
.collect(Collectors.toList());
@ -397,7 +396,7 @@ public class JavadocTask extends AbstractTask<JavadocTask> {
private String toSearchPath(List<Path> files) {
return files.stream()
.map(Path::toString)
.collect(Collectors.joining(File.pathSeparator));
.collect(Collectors.joining(ToolBox.pathSeparator));
}
private Iterable<? extends JavaFileObject> joinFiles(

View File

@ -23,7 +23,6 @@
package toolbox;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
@ -257,7 +256,7 @@ public class ModuleBuilder {
/**
* Writes the source files for the module to a specified directory,
* and then compiles them to a given directory.
* @param srcDir the directory in which a directory will be created
* @param src the directory in which a directory will be created
* to contain the source files for the module
* @param modules the directory in which a directory will be created
* to contain the compiled class files for the module
@ -267,7 +266,7 @@ public class ModuleBuilder {
Path moduleSrc = write(src);
String mp = modulePath.stream()
.map(Path::toString)
.collect(Collectors.joining(File.pathSeparator));
.collect(Collectors.joining(ToolBox.pathSeparator));
new JavacTask(tb)
.outdir(Files.createDirectories(modules.resolve(name)))
.options("--module-path", mp)

View File

@ -34,8 +34,6 @@ import java.io.StringWriter;
import java.io.Writer;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
@ -48,7 +46,6 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Deque;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
@ -92,6 +89,10 @@ import javax.tools.ToolProvider;
public class ToolBox {
/** The platform line separator. */
public static final String lineSeparator = System.getProperty("line.separator");
/** The platform path separator. */
public static final String pathSeparator = System.getProperty("path.separator");
/** The platform file separator character. */
public static char fileSeparatorChar = System.getProperty("file.separator").charAt(0);
/** The platform OS name. */
public static final String osName = System.getProperty("os.name");