7006126: (fs) Updates to file system API (1/2011)

Reviewed-by: jjg
This commit is contained in:
Alan Bateman 2011-01-28 09:25:20 +00:00
parent 50d526442a
commit 68c23b7878
3 changed files with 21 additions and 28 deletions
langtools
src/share/classes/com/sun/tools/javac/nio
test/tools/javac/nio/compileTest

@ -39,7 +39,6 @@ import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult; import java.nio.file.FileVisitResult;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor; import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.Attributes;
import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -223,9 +222,7 @@ public class JavacPathFileManager extends BaseFileManager implements PathFileMan
Path path = pathIter.next(); Path path = pathIter.next();
if (pathIter.hasNext()) if (pathIter.hasNext())
throw new IllegalArgumentException("path too long for directory"); throw new IllegalArgumentException("path too long for directory");
if (!path.exists()) if (!isDirectory(path))
throw new FileNotFoundException(path + ": does not exist");
else if (!isDirectory(path))
throw new IOException(path + ": not a directory"); throw new IOException(path + ": not a directory");
} }
@ -326,7 +323,7 @@ public class JavacPathFileManager extends BaseFileManager implements PathFileMan
private void list(Path path, String packageName, final Set<Kind> kinds, private void list(Path path, String packageName, final Set<Kind> kinds,
boolean recurse, final ListBuffer<JavaFileObject> results) boolean recurse, final ListBuffer<JavaFileObject> results)
throws IOException { throws IOException {
if (!path.exists()) if (!Files.exists(path))
return; return;
final Path pathDir; final Path pathDir;
@ -341,7 +338,7 @@ public class JavacPathFileManager extends BaseFileManager implements PathFileMan
String sep = path.getFileSystem().getSeparator(); String sep = path.getFileSystem().getSeparator();
Path packageDir = packageName.isEmpty() ? pathDir Path packageDir = packageName.isEmpty() ? pathDir
: pathDir.resolve(packageName.replace(".", sep)); : pathDir.resolve(packageName.replace(".", sep));
if (!packageDir.exists()) if (!Files.exists(packageDir))
return; return;
/* Alternate impl of list, superceded by use of Files.walkFileTree */ /* Alternate impl of list, superceded by use of Files.walkFileTree */
@ -353,7 +350,7 @@ public class JavacPathFileManager extends BaseFileManager implements PathFileMan
// DirectoryStream<Path> ds = dir.newDirectoryStream(); // DirectoryStream<Path> ds = dir.newDirectoryStream();
// try { // try {
// for (Path p: ds) { // for (Path p: ds) {
// String name = p.getName().toString(); // String name = p.getFileName().toString();
// if (isDirectory(p)) { // if (isDirectory(p)) {
// if (recurse && SourceVersion.isIdentifier(name)) { // if (recurse && SourceVersion.isIdentifier(name)) {
// queue.add(p); // queue.add(p);
@ -376,7 +373,7 @@ public class JavacPathFileManager extends BaseFileManager implements PathFileMan
new SimpleFileVisitor<Path>() { new SimpleFileVisitor<Path>() {
@Override @Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) { public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
Path name = dir.getName(); Path name = dir.getFileName();
if (name == null || SourceVersion.isIdentifier(name.toString())) // JSR 292? if (name == null || SourceVersion.isIdentifier(name.toString())) // JSR 292?
return FileVisitResult.CONTINUE; return FileVisitResult.CONTINUE;
else else
@ -385,7 +382,7 @@ public class JavacPathFileManager extends BaseFileManager implements PathFileMan
@Override @Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
if (attrs.isRegularFile() && kinds.contains(getKind(file.getName().toString()))) { if (attrs.isRegularFile() && kinds.contains(getKind(file.getFileName().toString()))) {
JavaFileObject fe = JavaFileObject fe =
PathFileObject.createDirectoryPathFileObject( PathFileObject.createDirectoryPathFileObject(
JavacPathFileManager.this, file, pathDir); JavacPathFileManager.this, file, pathDir);
@ -431,13 +428,13 @@ public class JavacPathFileManager extends BaseFileManager implements PathFileMan
for (Path p: getLocation(location)) { for (Path p: getLocation(location)) {
if (isDirectory(p)) { if (isDirectory(p)) {
Path f = resolve(p, relativePath); Path f = resolve(p, relativePath);
if (f.exists()) if (Files.exists(f))
return PathFileObject.createDirectoryPathFileObject(this, f, p); return PathFileObject.createDirectoryPathFileObject(this, f, p);
} else { } else {
FileSystem fs = getFileSystem(p); FileSystem fs = getFileSystem(p);
if (fs != null) { if (fs != null) {
Path file = getPath(fs, relativePath); Path file = getPath(fs, relativePath);
if (file.exists()) if (Files.exists(file))
return PathFileObject.createJarPathFileObject(this, file); return PathFileObject.createJarPathFileObject(this, file);
} }
} }
@ -504,7 +501,7 @@ public class JavacPathFileManager extends BaseFileManager implements PathFileMan
private FileSystem getFileSystem(Path p) throws IOException { private FileSystem getFileSystem(Path p) throws IOException {
FileSystem fs = fileSystems.get(p); FileSystem fs = fileSystems.get(p);
if (fs == null) { if (fs == null) {
fs = FileSystems.newFileSystem(p, Collections.<String,Void>emptyMap(), null); fs = FileSystems.newFileSystem(p, null);
fileSystems.put(p, fs); fileSystems.put(p, fs);
} }
return fs; return fs;
@ -530,7 +527,7 @@ public class JavacPathFileManager extends BaseFileManager implements PathFileMan
} }
private static boolean isDirectory(Path path) throws IOException { private static boolean isDirectory(Path path) throws IOException {
BasicFileAttributes attrs = Attributes.readBasicFileAttributes(path); BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
return attrs.isDirectory(); return attrs.isDirectory();
} }

@ -38,7 +38,6 @@ import java.nio.CharBuffer;
import java.nio.charset.CharsetDecoder; import java.nio.charset.CharsetDecoder;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.attribute.Attributes;
import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.BasicFileAttributes;
import javax.lang.model.element.Modifier; import javax.lang.model.element.Modifier;
import javax.lang.model.element.NestingKind; import javax.lang.model.element.NestingKind;
@ -153,7 +152,7 @@ abstract class PathFileObject implements JavaFileObject {
@Override @Override
public Kind getKind() { public Kind getKind() {
return BaseFileManager.getKind(path.getName().toString()); return BaseFileManager.getKind(path.getFileName().toString());
} }
@Override @Override
@ -164,14 +163,14 @@ abstract class PathFileObject implements JavaFileObject {
return false; return false;
} }
String sn = simpleName + kind.extension; String sn = simpleName + kind.extension;
String pn = path.getName().toString(); String pn = path.getFileName().toString();
if (pn.equals(sn)) { if (pn.equals(sn)) {
return true; return true;
} }
if (pn.equalsIgnoreCase(sn)) { if (pn.equalsIgnoreCase(sn)) {
try { try {
// allow for Windows // allow for Windows
return path.toRealPath(false).getName().toString().equals(sn); return path.toRealPath(false).getFileName().toString().equals(sn);
} catch (IOException e) { } catch (IOException e) {
} }
} }
@ -200,13 +199,13 @@ abstract class PathFileObject implements JavaFileObject {
@Override @Override
public InputStream openInputStream() throws IOException { public InputStream openInputStream() throws IOException {
return path.newInputStream(); return Files.newInputStream(path);
} }
@Override @Override
public OutputStream openOutputStream() throws IOException { public OutputStream openOutputStream() throws IOException {
ensureParentDirectoriesExist(); ensureParentDirectoriesExist();
return path.newOutputStream(); return Files.newOutputStream(path);
} }
@Override @Override
@ -242,14 +241,13 @@ abstract class PathFileObject implements JavaFileObject {
@Override @Override
public Writer openWriter() throws IOException { public Writer openWriter() throws IOException {
ensureParentDirectoriesExist(); ensureParentDirectoriesExist();
return new OutputStreamWriter(path.newOutputStream(), fileManager.getEncodingName()); return new OutputStreamWriter(Files.newOutputStream(path), fileManager.getEncodingName());
} }
@Override @Override
public long getLastModified() { public long getLastModified() {
try { try {
BasicFileAttributes attrs = Attributes.readBasicFileAttributes(path); return Files.getLastModifiedTime(path).toMillis();
return attrs.lastModifiedTime().toMillis();
} catch (IOException e) { } catch (IOException e) {
return -1; return -1;
} }
@ -258,7 +256,7 @@ abstract class PathFileObject implements JavaFileObject {
@Override @Override
public boolean delete() { public boolean delete() {
try { try {
path.delete(); Files.delete(path);
return true; return true;
} catch (IOException e) { } catch (IOException e) {
return false; return false;
@ -267,7 +265,7 @@ abstract class PathFileObject implements JavaFileObject {
public boolean isSameFile(PathFileObject other) { public boolean isSameFile(PathFileObject other) {
try { try {
return path.isSameFile(other.path); return Files.isSameFile(path, other.path);
} catch (IOException e) { } catch (IOException e) {
return false; return false;
} }
@ -296,8 +294,7 @@ abstract class PathFileObject implements JavaFileObject {
private long size() { private long size() {
try { try {
BasicFileAttributes attrs = Attributes.readBasicFileAttributes(path); return Files.size(path);
return attrs.size();
} catch (IOException e) { } catch (IOException e) {
return -1; return -1;
} }

@ -84,8 +84,7 @@ public class CompileTest {
System.err.println("Test " + count + " " + Arrays.asList(opts) + " " + className); System.err.println("Test " + count + " " + Arrays.asList(opts) + " " + className);
Path testSrcDir = Paths.get(System.getProperty("test.src")); Path testSrcDir = Paths.get(System.getProperty("test.src"));
Path testClassesDir = Paths.get(System.getProperty("test.classes")); Path testClassesDir = Paths.get(System.getProperty("test.classes"));
Path classes = Paths.get("classes." + count); Path classes = Files.createDirectory(Paths.get("classes." + count));
classes.createDirectory();
Context ctx = new Context(); Context ctx = new Context();
PathFileManager fm = new JavacPathFileManager(ctx, true, null); PathFileManager fm = new JavacPathFileManager(ctx, true, null);