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

changeset 450
4011f49b4af8
child 466
ca6bc36b2305
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javac/nio/PathFileObject.java	Fri Dec 11 14:26:27 2009 -0800
     1.3 @@ -0,0 +1,319 @@
     1.4 +/*
     1.5 + * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Sun designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Sun in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.25 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.26 + * have any questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.javac.nio;
    1.30 +
    1.31 +import java.io.IOException;
    1.32 +import java.io.InputStream;
    1.33 +import java.io.InputStreamReader;
    1.34 +import java.io.OutputStream;
    1.35 +import java.io.OutputStreamWriter;
    1.36 +import java.io.Reader;
    1.37 +import java.io.Writer;
    1.38 +import java.net.URI;
    1.39 +import java.nio.ByteBuffer;
    1.40 +import java.nio.CharBuffer;
    1.41 +import java.nio.charset.CharsetDecoder;
    1.42 +import java.nio.file.Files;
    1.43 +import java.nio.file.Path;
    1.44 +import java.nio.file.attribute.Attributes;
    1.45 +import java.nio.file.attribute.BasicFileAttributes;
    1.46 +import javax.lang.model.element.Modifier;
    1.47 +import javax.lang.model.element.NestingKind;
    1.48 +import javax.tools.JavaFileObject;
    1.49 +
    1.50 +import com.sun.tools.javac.util.BaseFileManager;
    1.51 +
    1.52 +
    1.53 +/**
    1.54 + *  Implementation of JavaFileObject using java.nio.file API.
    1.55 + *
    1.56 + *  <p>PathFileObjects are, for the most part, straightforward wrappers around
    1.57 + *  Path objects. The primary complexity is the support for "inferBinaryName".
    1.58 + *  This is left as an abstract method, implemented by each of a number of
    1.59 + *  different factory methods, which compute the binary name based on
    1.60 + *  information available at the time the file object is created.
    1.61 + *
    1.62 + *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
    1.63 + *  you write code that depends on this, you do so at your own risk.
    1.64 + *  This code and its internal interfaces are subject to change or
    1.65 + *  deletion without notice.</b>
    1.66 + */
    1.67 +abstract class PathFileObject implements JavaFileObject {
    1.68 +    private JavacPathFileManager fileManager;
    1.69 +    private Path path;
    1.70 +
    1.71 +    /**
    1.72 +     * Create a PathFileObject within a directory, such that the binary name
    1.73 +     * can be inferred from the relationship to the parent directory.
    1.74 +     */
    1.75 +    static PathFileObject createDirectoryPathFileObject(JavacPathFileManager fileManager,
    1.76 +            final Path path, final Path dir) {
    1.77 +        return new PathFileObject(fileManager, path) {
    1.78 +            @Override
    1.79 +            String inferBinaryName(Iterable<? extends Path> paths) {
    1.80 +                return toBinaryName(dir.relativize(path));
    1.81 +            }
    1.82 +        };
    1.83 +    }
    1.84 +
    1.85 +    /**
    1.86 +     * Create a PathFileObject in a file system such as a jar file, such that
    1.87 +     * the binary name can be inferred from its position within the filesystem.
    1.88 +     */
    1.89 +    static PathFileObject createJarPathFileObject(JavacPathFileManager fileManager,
    1.90 +            final Path path) {
    1.91 +        return new PathFileObject(fileManager, path) {
    1.92 +            @Override
    1.93 +            String inferBinaryName(Iterable<? extends Path> paths) {
    1.94 +                return toBinaryName(path);
    1.95 +            }
    1.96 +        };
    1.97 +    }
    1.98 +
    1.99 +    /**
   1.100 +     * Create a PathFileObject whose binary name can be inferred from the
   1.101 +     * relative path to a sibling.
   1.102 +     */
   1.103 +    static PathFileObject createSiblingPathFileObject(JavacPathFileManager fileManager,
   1.104 +            final Path path, final String relativePath) {
   1.105 +        return new PathFileObject(fileManager, path) {
   1.106 +            @Override
   1.107 +            String inferBinaryName(Iterable<? extends Path> paths) {
   1.108 +                return toBinaryName(relativePath, "/");
   1.109 +            }
   1.110 +        };
   1.111 +    }
   1.112 +
   1.113 +    /**
   1.114 +     * Create a PathFileObject whose binary name might be inferred from its
   1.115 +     * position on a search path.
   1.116 +     */
   1.117 +    static PathFileObject createSimplePathFileObject(JavacPathFileManager fileManager,
   1.118 +            final Path path) {
   1.119 +        return new PathFileObject(fileManager, path) {
   1.120 +            @Override
   1.121 +            String inferBinaryName(Iterable<? extends Path> paths) {
   1.122 +                Path absPath = path.toAbsolutePath();
   1.123 +                for (Path p: paths) {
   1.124 +                    Path ap = p.toAbsolutePath();
   1.125 +                    if (absPath.startsWith(ap)) {
   1.126 +                        try {
   1.127 +                            Path rp = ap.relativize(absPath);
   1.128 +                            if (rp != null) // maybe null if absPath same as ap
   1.129 +                                return toBinaryName(rp);
   1.130 +                        } catch (IllegalArgumentException e) {
   1.131 +                            // ignore this p if cannot relativize path to p
   1.132 +                        }
   1.133 +                    }
   1.134 +                }
   1.135 +                return null;
   1.136 +            }
   1.137 +        };
   1.138 +    }
   1.139 +
   1.140 +    protected PathFileObject(JavacPathFileManager fileManager, Path path) {
   1.141 +        fileManager.getClass(); // null check
   1.142 +        path.getClass();        // null check
   1.143 +        this.fileManager = fileManager;
   1.144 +        this.path = path;
   1.145 +    }
   1.146 +
   1.147 +    abstract String inferBinaryName(Iterable<? extends Path> paths);
   1.148 +
   1.149 +    /**
   1.150 +     * Return the Path for this object.
   1.151 +     * @return the Path for this object.
   1.152 +     */
   1.153 +    Path getPath() {
   1.154 +        return path;
   1.155 +    }
   1.156 +
   1.157 +    @Override
   1.158 +    public Kind getKind() {
   1.159 +        return BaseFileManager.getKind(path.getName().toString());
   1.160 +    }
   1.161 +
   1.162 +    @Override
   1.163 +    public boolean isNameCompatible(String simpleName, Kind kind) {
   1.164 +        simpleName.getClass();
   1.165 +        // null check
   1.166 +        if (kind == Kind.OTHER && getKind() != kind) {
   1.167 +            return false;
   1.168 +        }
   1.169 +        String sn = simpleName + kind.extension;
   1.170 +        String pn = path.getName().toString();
   1.171 +        if (pn.equals(sn)) {
   1.172 +            return true;
   1.173 +        }
   1.174 +        if (pn.equalsIgnoreCase(sn)) {
   1.175 +            try {
   1.176 +                // allow for Windows
   1.177 +                return path.toRealPath(false).getName().toString().equals(sn);
   1.178 +            } catch (IOException e) {
   1.179 +            }
   1.180 +        }
   1.181 +        return false;
   1.182 +    }
   1.183 +
   1.184 +    @Override
   1.185 +    public NestingKind getNestingKind() {
   1.186 +        return null;
   1.187 +    }
   1.188 +
   1.189 +    @Override
   1.190 +    public Modifier getAccessLevel() {
   1.191 +        return null;
   1.192 +    }
   1.193 +
   1.194 +    @Override
   1.195 +    public URI toUri() {
   1.196 +        return path.toUri();
   1.197 +    }
   1.198 +
   1.199 +    @Override
   1.200 +    public String getName() {
   1.201 +        return path.toString();
   1.202 +    }
   1.203 +
   1.204 +    @Override
   1.205 +    public InputStream openInputStream() throws IOException {
   1.206 +        return path.newInputStream();
   1.207 +    }
   1.208 +
   1.209 +    @Override
   1.210 +    public OutputStream openOutputStream() throws IOException {
   1.211 +        ensureParentDirectoriesExist();
   1.212 +        return path.newOutputStream();
   1.213 +    }
   1.214 +
   1.215 +    @Override
   1.216 +    public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
   1.217 +        CharsetDecoder decoder = fileManager.getDecoder(fileManager.getEncodingName(), ignoreEncodingErrors);
   1.218 +        return new InputStreamReader(openInputStream(), decoder);
   1.219 +    }
   1.220 +
   1.221 +    @Override
   1.222 +    public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
   1.223 +        CharBuffer cb = fileManager.getCachedContent(this);
   1.224 +        if (cb == null) {
   1.225 +            InputStream in = openInputStream();
   1.226 +            try {
   1.227 +                ByteBuffer bb = fileManager.makeByteBuffer(in);
   1.228 +                JavaFileObject prev = fileManager.log.useSource(this);
   1.229 +                try {
   1.230 +                    cb = fileManager.decode(bb, ignoreEncodingErrors);
   1.231 +                } finally {
   1.232 +                    fileManager.log.useSource(prev);
   1.233 +                }
   1.234 +                fileManager.recycleByteBuffer(bb);
   1.235 +                if (!ignoreEncodingErrors) {
   1.236 +                    fileManager.cache(this, cb);
   1.237 +                }
   1.238 +            } finally {
   1.239 +                in.close();
   1.240 +            }
   1.241 +        }
   1.242 +        return cb;
   1.243 +    }
   1.244 +
   1.245 +    @Override
   1.246 +    public Writer openWriter() throws IOException {
   1.247 +        ensureParentDirectoriesExist();
   1.248 +        return new OutputStreamWriter(path.newOutputStream(), fileManager.getEncodingName());
   1.249 +    }
   1.250 +
   1.251 +    @Override
   1.252 +    public long getLastModified() {
   1.253 +        try {
   1.254 +            BasicFileAttributes attrs = Attributes.readBasicFileAttributes(path);
   1.255 +            return attrs.lastModifiedTime().toMillis();
   1.256 +        } catch (IOException e) {
   1.257 +            return -1;
   1.258 +        }
   1.259 +    }
   1.260 +
   1.261 +    @Override
   1.262 +    public boolean delete() {
   1.263 +        try {
   1.264 +            path.delete();
   1.265 +            return true;
   1.266 +        } catch (IOException e) {
   1.267 +            return false;
   1.268 +        }
   1.269 +    }
   1.270 +
   1.271 +    public boolean isSameFile(PathFileObject other) {
   1.272 +        try {
   1.273 +            return path.isSameFile(other.path);
   1.274 +        } catch (IOException e) {
   1.275 +            return false;
   1.276 +        }
   1.277 +    }
   1.278 +
   1.279 +    @Override
   1.280 +    public boolean equals(Object other) {
   1.281 +        return (other instanceof PathFileObject && path.equals(((PathFileObject) other).path));
   1.282 +    }
   1.283 +
   1.284 +    @Override
   1.285 +    public int hashCode() {
   1.286 +        return path.hashCode();
   1.287 +    }
   1.288 +
   1.289 +    @Override
   1.290 +    public String toString() {
   1.291 +        return getClass().getSimpleName() + "[" + path + "]";
   1.292 +    }
   1.293 +
   1.294 +    private void ensureParentDirectoriesExist() throws IOException {
   1.295 +        Path parent = path.getParent();
   1.296 +        if (parent != null)
   1.297 +            Files.createDirectories(parent);
   1.298 +    }
   1.299 +
   1.300 +    private long size() {
   1.301 +        try {
   1.302 +            BasicFileAttributes attrs = Attributes.readBasicFileAttributes(path);
   1.303 +            return attrs.size();
   1.304 +        } catch (IOException e) {
   1.305 +            return -1;
   1.306 +        }
   1.307 +    }
   1.308 +
   1.309 +    protected static String toBinaryName(Path relativePath) {
   1.310 +        return toBinaryName(relativePath.toString(),
   1.311 +                relativePath.getFileSystem().getSeparator());
   1.312 +    }
   1.313 +
   1.314 +    protected static String toBinaryName(String relativePath, String sep) {
   1.315 +        return removeExtension(relativePath).replaceAll(sep, ".");
   1.316 +    }
   1.317 +
   1.318 +    protected static String removeExtension(String fileName) {
   1.319 +        int lastDot = fileName.lastIndexOf(".");
   1.320 +        return (lastDot == -1 ? fileName : fileName.substring(0, lastDot));
   1.321 +    }
   1.322 +}

mercurial