src/share/classes/com/sun/tools/javac/nio/PathFileObject.java

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 1357
c75be5bc5283
parent 0
959103a6100f
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.tools.javac.nio;
aoqi@0 27
aoqi@0 28 import java.io.IOException;
aoqi@0 29 import java.io.InputStream;
aoqi@0 30 import java.io.InputStreamReader;
aoqi@0 31 import java.io.OutputStream;
aoqi@0 32 import java.io.OutputStreamWriter;
aoqi@0 33 import java.io.Reader;
aoqi@0 34 import java.io.Writer;
aoqi@0 35 import java.net.URI;
aoqi@0 36 import java.nio.ByteBuffer;
aoqi@0 37 import java.nio.CharBuffer;
aoqi@0 38 import java.nio.charset.CharsetDecoder;
aoqi@0 39 import java.nio.file.Files;
aoqi@0 40 import java.nio.file.LinkOption;
aoqi@0 41 import java.nio.file.Path;
aoqi@0 42 import javax.lang.model.element.Modifier;
aoqi@0 43 import javax.lang.model.element.NestingKind;
aoqi@0 44 import javax.tools.JavaFileObject;
aoqi@0 45
aoqi@0 46 import com.sun.tools.javac.util.BaseFileManager;
aoqi@0 47
aoqi@0 48
aoqi@0 49 /**
aoqi@0 50 * Implementation of JavaFileObject using java.nio.file API.
aoqi@0 51 *
aoqi@0 52 * <p>PathFileObjects are, for the most part, straightforward wrappers around
aoqi@0 53 * Path objects. The primary complexity is the support for "inferBinaryName".
aoqi@0 54 * This is left as an abstract method, implemented by each of a number of
aoqi@0 55 * different factory methods, which compute the binary name based on
aoqi@0 56 * information available at the time the file object is created.
aoqi@0 57 *
aoqi@0 58 * <p><b>This is NOT part of any supported API.
aoqi@0 59 * If you write code that depends on this, you do so at your own risk.
aoqi@0 60 * This code and its internal interfaces are subject to change or
aoqi@0 61 * deletion without notice.</b>
aoqi@0 62 */
aoqi@0 63 abstract class PathFileObject implements JavaFileObject {
aoqi@0 64 private JavacPathFileManager fileManager;
aoqi@0 65 private Path path;
aoqi@0 66
aoqi@0 67 /**
aoqi@0 68 * Create a PathFileObject within a directory, such that the binary name
aoqi@0 69 * can be inferred from the relationship to the parent directory.
aoqi@0 70 */
aoqi@0 71 static PathFileObject createDirectoryPathFileObject(JavacPathFileManager fileManager,
aoqi@0 72 final Path path, final Path dir) {
aoqi@0 73 return new PathFileObject(fileManager, path) {
aoqi@0 74 @Override
aoqi@0 75 String inferBinaryName(Iterable<? extends Path> paths) {
aoqi@0 76 return toBinaryName(dir.relativize(path));
aoqi@0 77 }
aoqi@0 78 };
aoqi@0 79 }
aoqi@0 80
aoqi@0 81 /**
aoqi@0 82 * Create a PathFileObject in a file system such as a jar file, such that
aoqi@0 83 * the binary name can be inferred from its position within the filesystem.
aoqi@0 84 */
aoqi@0 85 static PathFileObject createJarPathFileObject(JavacPathFileManager fileManager,
aoqi@0 86 final Path path) {
aoqi@0 87 return new PathFileObject(fileManager, path) {
aoqi@0 88 @Override
aoqi@0 89 String inferBinaryName(Iterable<? extends Path> paths) {
aoqi@0 90 return toBinaryName(path);
aoqi@0 91 }
aoqi@0 92 };
aoqi@0 93 }
aoqi@0 94
aoqi@0 95 /**
aoqi@0 96 * Create a PathFileObject whose binary name can be inferred from the
aoqi@0 97 * relative path to a sibling.
aoqi@0 98 */
aoqi@0 99 static PathFileObject createSiblingPathFileObject(JavacPathFileManager fileManager,
aoqi@0 100 final Path path, final String relativePath) {
aoqi@0 101 return new PathFileObject(fileManager, path) {
aoqi@0 102 @Override
aoqi@0 103 String inferBinaryName(Iterable<? extends Path> paths) {
aoqi@0 104 return toBinaryName(relativePath, "/");
aoqi@0 105 }
aoqi@0 106 };
aoqi@0 107 }
aoqi@0 108
aoqi@0 109 /**
aoqi@0 110 * Create a PathFileObject whose binary name might be inferred from its
aoqi@0 111 * position on a search path.
aoqi@0 112 */
aoqi@0 113 static PathFileObject createSimplePathFileObject(JavacPathFileManager fileManager,
aoqi@0 114 final Path path) {
aoqi@0 115 return new PathFileObject(fileManager, path) {
aoqi@0 116 @Override
aoqi@0 117 String inferBinaryName(Iterable<? extends Path> paths) {
aoqi@0 118 Path absPath = path.toAbsolutePath();
aoqi@0 119 for (Path p: paths) {
aoqi@0 120 Path ap = p.toAbsolutePath();
aoqi@0 121 if (absPath.startsWith(ap)) {
aoqi@0 122 try {
aoqi@0 123 Path rp = ap.relativize(absPath);
aoqi@0 124 if (rp != null) // maybe null if absPath same as ap
aoqi@0 125 return toBinaryName(rp);
aoqi@0 126 } catch (IllegalArgumentException e) {
aoqi@0 127 // ignore this p if cannot relativize path to p
aoqi@0 128 }
aoqi@0 129 }
aoqi@0 130 }
aoqi@0 131 return null;
aoqi@0 132 }
aoqi@0 133 };
aoqi@0 134 }
aoqi@0 135
aoqi@0 136 protected PathFileObject(JavacPathFileManager fileManager, Path path) {
aoqi@0 137 fileManager.getClass(); // null check
aoqi@0 138 path.getClass(); // null check
aoqi@0 139 this.fileManager = fileManager;
aoqi@0 140 this.path = path;
aoqi@0 141 }
aoqi@0 142
aoqi@0 143 abstract String inferBinaryName(Iterable<? extends Path> paths);
aoqi@0 144
aoqi@0 145 /**
aoqi@0 146 * Return the Path for this object.
aoqi@0 147 * @return the Path for this object.
aoqi@0 148 */
aoqi@0 149 Path getPath() {
aoqi@0 150 return path;
aoqi@0 151 }
aoqi@0 152
aoqi@0 153 @Override
aoqi@0 154 public Kind getKind() {
aoqi@0 155 return BaseFileManager.getKind(path.getFileName().toString());
aoqi@0 156 }
aoqi@0 157
aoqi@0 158 @Override
aoqi@0 159 public boolean isNameCompatible(String simpleName, Kind kind) {
aoqi@0 160 simpleName.getClass();
aoqi@0 161 // null check
aoqi@0 162 if (kind == Kind.OTHER && getKind() != kind) {
aoqi@0 163 return false;
aoqi@0 164 }
aoqi@0 165 String sn = simpleName + kind.extension;
aoqi@0 166 String pn = path.getFileName().toString();
aoqi@0 167 if (pn.equals(sn)) {
aoqi@0 168 return true;
aoqi@0 169 }
aoqi@0 170 if (pn.equalsIgnoreCase(sn)) {
aoqi@0 171 try {
aoqi@0 172 // allow for Windows
aoqi@0 173 return path.toRealPath(LinkOption.NOFOLLOW_LINKS).getFileName().toString().equals(sn);
aoqi@0 174 } catch (IOException e) {
aoqi@0 175 }
aoqi@0 176 }
aoqi@0 177 return false;
aoqi@0 178 }
aoqi@0 179
aoqi@0 180 @Override
aoqi@0 181 public NestingKind getNestingKind() {
aoqi@0 182 return null;
aoqi@0 183 }
aoqi@0 184
aoqi@0 185 @Override
aoqi@0 186 public Modifier getAccessLevel() {
aoqi@0 187 return null;
aoqi@0 188 }
aoqi@0 189
aoqi@0 190 @Override
aoqi@0 191 public URI toUri() {
aoqi@0 192 return path.toUri();
aoqi@0 193 }
aoqi@0 194
aoqi@0 195 @Override
aoqi@0 196 public String getName() {
aoqi@0 197 return path.toString();
aoqi@0 198 }
aoqi@0 199
aoqi@0 200 @Override
aoqi@0 201 public InputStream openInputStream() throws IOException {
aoqi@0 202 return Files.newInputStream(path);
aoqi@0 203 }
aoqi@0 204
aoqi@0 205 @Override
aoqi@0 206 public OutputStream openOutputStream() throws IOException {
aoqi@0 207 fileManager.flushCache(this);
aoqi@0 208 ensureParentDirectoriesExist();
aoqi@0 209 return Files.newOutputStream(path);
aoqi@0 210 }
aoqi@0 211
aoqi@0 212 @Override
aoqi@0 213 public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
aoqi@0 214 CharsetDecoder decoder = fileManager.getDecoder(fileManager.getEncodingName(), ignoreEncodingErrors);
aoqi@0 215 return new InputStreamReader(openInputStream(), decoder);
aoqi@0 216 }
aoqi@0 217
aoqi@0 218 @Override
aoqi@0 219 public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
aoqi@0 220 CharBuffer cb = fileManager.getCachedContent(this);
aoqi@0 221 if (cb == null) {
aoqi@0 222 InputStream in = openInputStream();
aoqi@0 223 try {
aoqi@0 224 ByteBuffer bb = fileManager.makeByteBuffer(in);
aoqi@0 225 JavaFileObject prev = fileManager.log.useSource(this);
aoqi@0 226 try {
aoqi@0 227 cb = fileManager.decode(bb, ignoreEncodingErrors);
aoqi@0 228 } finally {
aoqi@0 229 fileManager.log.useSource(prev);
aoqi@0 230 }
aoqi@0 231 fileManager.recycleByteBuffer(bb);
aoqi@0 232 if (!ignoreEncodingErrors) {
aoqi@0 233 fileManager.cache(this, cb);
aoqi@0 234 }
aoqi@0 235 } finally {
aoqi@0 236 in.close();
aoqi@0 237 }
aoqi@0 238 }
aoqi@0 239 return cb;
aoqi@0 240 }
aoqi@0 241
aoqi@0 242 @Override
aoqi@0 243 public Writer openWriter() throws IOException {
aoqi@0 244 fileManager.flushCache(this);
aoqi@0 245 ensureParentDirectoriesExist();
aoqi@0 246 return new OutputStreamWriter(Files.newOutputStream(path), fileManager.getEncodingName());
aoqi@0 247 }
aoqi@0 248
aoqi@0 249 @Override
aoqi@0 250 public long getLastModified() {
aoqi@0 251 try {
aoqi@0 252 return Files.getLastModifiedTime(path).toMillis();
aoqi@0 253 } catch (IOException e) {
aoqi@0 254 return -1;
aoqi@0 255 }
aoqi@0 256 }
aoqi@0 257
aoqi@0 258 @Override
aoqi@0 259 public boolean delete() {
aoqi@0 260 try {
aoqi@0 261 Files.delete(path);
aoqi@0 262 return true;
aoqi@0 263 } catch (IOException e) {
aoqi@0 264 return false;
aoqi@0 265 }
aoqi@0 266 }
aoqi@0 267
aoqi@0 268 public boolean isSameFile(PathFileObject other) {
aoqi@0 269 try {
aoqi@0 270 return Files.isSameFile(path, other.path);
aoqi@0 271 } catch (IOException e) {
aoqi@0 272 return false;
aoqi@0 273 }
aoqi@0 274 }
aoqi@0 275
aoqi@0 276 @Override
aoqi@0 277 public boolean equals(Object other) {
aoqi@0 278 return (other instanceof PathFileObject && path.equals(((PathFileObject) other).path));
aoqi@0 279 }
aoqi@0 280
aoqi@0 281 @Override
aoqi@0 282 public int hashCode() {
aoqi@0 283 return path.hashCode();
aoqi@0 284 }
aoqi@0 285
aoqi@0 286 @Override
aoqi@0 287 public String toString() {
aoqi@0 288 return getClass().getSimpleName() + "[" + path + "]";
aoqi@0 289 }
aoqi@0 290
aoqi@0 291 private void ensureParentDirectoriesExist() throws IOException {
aoqi@0 292 Path parent = path.getParent();
aoqi@0 293 if (parent != null)
aoqi@0 294 Files.createDirectories(parent);
aoqi@0 295 }
aoqi@0 296
aoqi@0 297 private long size() {
aoqi@0 298 try {
aoqi@0 299 return Files.size(path);
aoqi@0 300 } catch (IOException e) {
aoqi@0 301 return -1;
aoqi@0 302 }
aoqi@0 303 }
aoqi@0 304
aoqi@0 305 protected static String toBinaryName(Path relativePath) {
aoqi@0 306 return toBinaryName(relativePath.toString(),
aoqi@0 307 relativePath.getFileSystem().getSeparator());
aoqi@0 308 }
aoqi@0 309
aoqi@0 310 protected static String toBinaryName(String relativePath, String sep) {
aoqi@0 311 return removeExtension(relativePath).replace(sep, ".");
aoqi@0 312 }
aoqi@0 313
aoqi@0 314 protected static String removeExtension(String fileName) {
aoqi@0 315 int lastDot = fileName.lastIndexOf(".");
aoqi@0 316 return (lastDot == -1 ? fileName : fileName.substring(0, lastDot));
aoqi@0 317 }
aoqi@0 318 }

mercurial