aoqi@0: /* aoqi@0: * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.tools.javac.nio; aoqi@0: aoqi@0: import java.io.IOException; aoqi@0: import java.io.InputStream; aoqi@0: import java.io.InputStreamReader; aoqi@0: import java.io.OutputStream; aoqi@0: import java.io.OutputStreamWriter; aoqi@0: import java.io.Reader; aoqi@0: import java.io.Writer; aoqi@0: import java.net.URI; aoqi@0: import java.nio.ByteBuffer; aoqi@0: import java.nio.CharBuffer; aoqi@0: import java.nio.charset.CharsetDecoder; aoqi@0: import java.nio.file.Files; aoqi@0: import java.nio.file.LinkOption; aoqi@0: import java.nio.file.Path; aoqi@0: import javax.lang.model.element.Modifier; aoqi@0: import javax.lang.model.element.NestingKind; aoqi@0: import javax.tools.JavaFileObject; aoqi@0: aoqi@0: import com.sun.tools.javac.util.BaseFileManager; aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Implementation of JavaFileObject using java.nio.file API. aoqi@0: * aoqi@0: *

PathFileObjects are, for the most part, straightforward wrappers around aoqi@0: * Path objects. The primary complexity is the support for "inferBinaryName". aoqi@0: * This is left as an abstract method, implemented by each of a number of aoqi@0: * different factory methods, which compute the binary name based on aoqi@0: * information available at the time the file object is created. aoqi@0: * aoqi@0: *

This is NOT part of any supported API. aoqi@0: * If you write code that depends on this, you do so at your own risk. aoqi@0: * This code and its internal interfaces are subject to change or aoqi@0: * deletion without notice. aoqi@0: */ aoqi@0: abstract class PathFileObject implements JavaFileObject { aoqi@0: private JavacPathFileManager fileManager; aoqi@0: private Path path; aoqi@0: aoqi@0: /** aoqi@0: * Create a PathFileObject within a directory, such that the binary name aoqi@0: * can be inferred from the relationship to the parent directory. aoqi@0: */ aoqi@0: static PathFileObject createDirectoryPathFileObject(JavacPathFileManager fileManager, aoqi@0: final Path path, final Path dir) { aoqi@0: return new PathFileObject(fileManager, path) { aoqi@0: @Override aoqi@0: String inferBinaryName(Iterable paths) { aoqi@0: return toBinaryName(dir.relativize(path)); aoqi@0: } aoqi@0: }; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Create a PathFileObject in a file system such as a jar file, such that aoqi@0: * the binary name can be inferred from its position within the filesystem. aoqi@0: */ aoqi@0: static PathFileObject createJarPathFileObject(JavacPathFileManager fileManager, aoqi@0: final Path path) { aoqi@0: return new PathFileObject(fileManager, path) { aoqi@0: @Override aoqi@0: String inferBinaryName(Iterable paths) { aoqi@0: return toBinaryName(path); aoqi@0: } aoqi@0: }; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Create a PathFileObject whose binary name can be inferred from the aoqi@0: * relative path to a sibling. aoqi@0: */ aoqi@0: static PathFileObject createSiblingPathFileObject(JavacPathFileManager fileManager, aoqi@0: final Path path, final String relativePath) { aoqi@0: return new PathFileObject(fileManager, path) { aoqi@0: @Override aoqi@0: String inferBinaryName(Iterable paths) { aoqi@0: return toBinaryName(relativePath, "/"); aoqi@0: } aoqi@0: }; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Create a PathFileObject whose binary name might be inferred from its aoqi@0: * position on a search path. aoqi@0: */ aoqi@0: static PathFileObject createSimplePathFileObject(JavacPathFileManager fileManager, aoqi@0: final Path path) { aoqi@0: return new PathFileObject(fileManager, path) { aoqi@0: @Override aoqi@0: String inferBinaryName(Iterable paths) { aoqi@0: Path absPath = path.toAbsolutePath(); aoqi@0: for (Path p: paths) { aoqi@0: Path ap = p.toAbsolutePath(); aoqi@0: if (absPath.startsWith(ap)) { aoqi@0: try { aoqi@0: Path rp = ap.relativize(absPath); aoqi@0: if (rp != null) // maybe null if absPath same as ap aoqi@0: return toBinaryName(rp); aoqi@0: } catch (IllegalArgumentException e) { aoqi@0: // ignore this p if cannot relativize path to p aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: return null; aoqi@0: } aoqi@0: }; aoqi@0: } aoqi@0: aoqi@0: protected PathFileObject(JavacPathFileManager fileManager, Path path) { aoqi@0: fileManager.getClass(); // null check aoqi@0: path.getClass(); // null check aoqi@0: this.fileManager = fileManager; aoqi@0: this.path = path; aoqi@0: } aoqi@0: aoqi@0: abstract String inferBinaryName(Iterable paths); aoqi@0: aoqi@0: /** aoqi@0: * Return the Path for this object. aoqi@0: * @return the Path for this object. aoqi@0: */ aoqi@0: Path getPath() { aoqi@0: return path; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public Kind getKind() { aoqi@0: return BaseFileManager.getKind(path.getFileName().toString()); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public boolean isNameCompatible(String simpleName, Kind kind) { aoqi@0: simpleName.getClass(); aoqi@0: // null check aoqi@0: if (kind == Kind.OTHER && getKind() != kind) { aoqi@0: return false; aoqi@0: } aoqi@0: String sn = simpleName + kind.extension; aoqi@0: String pn = path.getFileName().toString(); aoqi@0: if (pn.equals(sn)) { aoqi@0: return true; aoqi@0: } aoqi@0: if (pn.equalsIgnoreCase(sn)) { aoqi@0: try { aoqi@0: // allow for Windows aoqi@0: return path.toRealPath(LinkOption.NOFOLLOW_LINKS).getFileName().toString().equals(sn); aoqi@0: } catch (IOException e) { aoqi@0: } aoqi@0: } aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public NestingKind getNestingKind() { aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public Modifier getAccessLevel() { aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public URI toUri() { aoqi@0: return path.toUri(); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public String getName() { aoqi@0: return path.toString(); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public InputStream openInputStream() throws IOException { aoqi@0: return Files.newInputStream(path); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public OutputStream openOutputStream() throws IOException { aoqi@0: fileManager.flushCache(this); aoqi@0: ensureParentDirectoriesExist(); aoqi@0: return Files.newOutputStream(path); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public Reader openReader(boolean ignoreEncodingErrors) throws IOException { aoqi@0: CharsetDecoder decoder = fileManager.getDecoder(fileManager.getEncodingName(), ignoreEncodingErrors); aoqi@0: return new InputStreamReader(openInputStream(), decoder); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { aoqi@0: CharBuffer cb = fileManager.getCachedContent(this); aoqi@0: if (cb == null) { aoqi@0: InputStream in = openInputStream(); aoqi@0: try { aoqi@0: ByteBuffer bb = fileManager.makeByteBuffer(in); aoqi@0: JavaFileObject prev = fileManager.log.useSource(this); aoqi@0: try { aoqi@0: cb = fileManager.decode(bb, ignoreEncodingErrors); aoqi@0: } finally { aoqi@0: fileManager.log.useSource(prev); aoqi@0: } aoqi@0: fileManager.recycleByteBuffer(bb); aoqi@0: if (!ignoreEncodingErrors) { aoqi@0: fileManager.cache(this, cb); aoqi@0: } aoqi@0: } finally { aoqi@0: in.close(); aoqi@0: } aoqi@0: } aoqi@0: return cb; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public Writer openWriter() throws IOException { aoqi@0: fileManager.flushCache(this); aoqi@0: ensureParentDirectoriesExist(); aoqi@0: return new OutputStreamWriter(Files.newOutputStream(path), fileManager.getEncodingName()); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public long getLastModified() { aoqi@0: try { aoqi@0: return Files.getLastModifiedTime(path).toMillis(); aoqi@0: } catch (IOException e) { aoqi@0: return -1; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public boolean delete() { aoqi@0: try { aoqi@0: Files.delete(path); aoqi@0: return true; aoqi@0: } catch (IOException e) { aoqi@0: return false; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public boolean isSameFile(PathFileObject other) { aoqi@0: try { aoqi@0: return Files.isSameFile(path, other.path); aoqi@0: } catch (IOException e) { aoqi@0: return false; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public boolean equals(Object other) { aoqi@0: return (other instanceof PathFileObject && path.equals(((PathFileObject) other).path)); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public int hashCode() { aoqi@0: return path.hashCode(); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public String toString() { aoqi@0: return getClass().getSimpleName() + "[" + path + "]"; aoqi@0: } aoqi@0: aoqi@0: private void ensureParentDirectoriesExist() throws IOException { aoqi@0: Path parent = path.getParent(); aoqi@0: if (parent != null) aoqi@0: Files.createDirectories(parent); aoqi@0: } aoqi@0: aoqi@0: private long size() { aoqi@0: try { aoqi@0: return Files.size(path); aoqi@0: } catch (IOException e) { aoqi@0: return -1; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: protected static String toBinaryName(Path relativePath) { aoqi@0: return toBinaryName(relativePath.toString(), aoqi@0: relativePath.getFileSystem().getSeparator()); aoqi@0: } aoqi@0: aoqi@0: protected static String toBinaryName(String relativePath, String sep) { aoqi@0: return removeExtension(relativePath).replace(sep, "."); aoqi@0: } aoqi@0: aoqi@0: protected static String removeExtension(String fileName) { aoqi@0: int lastDot = fileName.lastIndexOf("."); aoqi@0: return (lastDot == -1 ? fileName : fileName.substring(0, lastDot)); aoqi@0: } aoqi@0: }