PathFileObjects are, for the most part, straightforward wrappers around
Path objects. The primary complexity is the support for "inferBinaryName".
This is left as an abstract method, implemented by each of a number of
different factory methods, which compute the binary name based on
information available at the time the file object is created.
Method from com.sun.tools.javac.nio.PathFileObject Detail: |
static PathFileObject createDirectoryPathFileObject(JavacPathFileManager fileManager,
Path path,
Path dir) {
return new PathFileObject(fileManager, path) {
@Override
String inferBinaryName(Iterable< ? extends Path > paths) {
return toBinaryName(dir.relativize(path));
}
};
}
Create a PathFileObject within a directory, such that the binary name
can be inferred from the relationship to the parent directory. |
static PathFileObject createJarPathFileObject(JavacPathFileManager fileManager,
Path path) {
return new PathFileObject(fileManager, path) {
@Override
String inferBinaryName(Iterable< ? extends Path > paths) {
return toBinaryName(path);
}
};
}
Create a PathFileObject in a file system such as a jar file, such that
the binary name can be inferred from its position within the filesystem. |
static PathFileObject createSiblingPathFileObject(JavacPathFileManager fileManager,
Path path,
String relativePath) {
return new PathFileObject(fileManager, path) {
@Override
String inferBinaryName(Iterable< ? extends Path > paths) {
return toBinaryName(relativePath, "/");
}
};
}
Create a PathFileObject whose binary name can be inferred from the
relative path to a sibling. |
static PathFileObject createSimplePathFileObject(JavacPathFileManager fileManager,
Path path) {
return new PathFileObject(fileManager, path) {
@Override
String inferBinaryName(Iterable< ? extends Path > paths) {
Path absPath = path.toAbsolutePath();
for (Path p: paths) {
Path ap = p.toAbsolutePath();
if (absPath.startsWith(ap)) {
try {
Path rp = ap.relativize(absPath);
if (rp != null) // maybe null if absPath same as ap
return toBinaryName(rp);
} catch (IllegalArgumentException e) {
// ignore this p if cannot relativize path to p
}
}
}
return null;
}
};
}
Create a PathFileObject whose binary name might be inferred from its
position on a search path. |
public boolean delete() {
try {
Files.delete(path);
return true;
} catch (IOException e) {
return false;
}
}
|
public boolean equals(Object other) {
return (other instanceof PathFileObject && path.equals(((PathFileObject) other).path));
}
|
public Modifier getAccessLevel() {
return null;
}
|
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
CharBuffer cb = fileManager.getCachedContent(this);
if (cb == null) {
InputStream in = openInputStream();
try {
ByteBuffer bb = fileManager.makeByteBuffer(in);
JavaFileObject prev = fileManager.log.useSource(this);
try {
cb = fileManager.decode(bb, ignoreEncodingErrors);
} finally {
fileManager.log.useSource(prev);
}
fileManager.recycleByteBuffer(bb);
if (!ignoreEncodingErrors) {
fileManager.cache(this, cb);
}
} finally {
in.close();
}
}
return cb;
}
|
public Kind getKind() {
return BaseFileManager.getKind(path.getFileName().toString());
}
|
public long getLastModified() {
try {
return Files.getLastModifiedTime(path).toMillis();
} catch (IOException e) {
return -1;
}
}
|
public String getName() {
return path.toString();
}
|
public NestingKind getNestingKind() {
return null;
}
|
Path getPath() {
return path;
}
Return the Path for this object. |
public int hashCode() {
return path.hashCode();
}
|
abstract String inferBinaryName(Iterable<Path> paths)
|
public boolean isNameCompatible(String simpleName,
Kind kind) {
simpleName.getClass();
// null check
if (kind == Kind.OTHER && getKind() != kind) {
return false;
}
String sn = simpleName + kind.extension;
String pn = path.getFileName().toString();
if (pn.equals(sn)) {
return true;
}
if (pn.equalsIgnoreCase(sn)) {
try {
// allow for Windows
return path.toRealPath(LinkOption.NOFOLLOW_LINKS).getFileName().toString().equals(sn);
} catch (IOException e) {
}
}
return false;
}
|
public boolean isSameFile(PathFileObject other) {
try {
return Files.isSameFile(path, other.path);
} catch (IOException e) {
return false;
}
}
|
public InputStream openInputStream() throws IOException {
return Files.newInputStream(path);
}
|
public OutputStream openOutputStream() throws IOException {
ensureParentDirectoriesExist();
return Files.newOutputStream(path);
}
|
public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
CharsetDecoder decoder = fileManager.getDecoder(fileManager.getEncodingName(), ignoreEncodingErrors);
return new InputStreamReader(openInputStream(), decoder);
}
|
public Writer openWriter() throws IOException {
ensureParentDirectoriesExist();
return new OutputStreamWriter(Files.newOutputStream(path), fileManager.getEncodingName());
}
|
protected static String removeExtension(String fileName) {
int lastDot = fileName.lastIndexOf(".");
return (lastDot == -1 ? fileName : fileName.substring(0, lastDot));
}
|
protected static String toBinaryName(Path relativePath) {
return toBinaryName(relativePath.toString(),
relativePath.getFileSystem().getSeparator());
}
|
protected static String toBinaryName(String relativePath,
String sep) {
return removeExtension(relativePath).replace(sep, ".");
}
|
public String toString() {
return getClass().getSimpleName() + "[" + path + "]";
}
|
public URI toUri() {
return path.toUri();
}
|