src/share/classes/com/sun/tools/javac/file/RegularFileObject.java

Tue, 24 Dec 2013 09:17:37 -0800

author
ksrini
date
Tue, 24 Dec 2013 09:17:37 -0800
changeset 2227
998b10c43157
parent 2037
36e342dd57e2
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8029230: Update copyright year to match last edit in jdk8 langtools repository for 2013
Reviewed-by: ksrini
Contributed-by: steve.sides@oracle.com

jjg@57 1 /*
ksrini@2227 2 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
jjg@57 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@57 4 *
jjg@57 5 * This code is free software; you can redistribute it and/or modify it
jjg@57 6 * under the terms of the GNU General Public License version 2 only, as
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
jjg@57 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
jjg@57 10 *
jjg@57 11 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@57 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@57 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@57 14 * version 2 for more details (a copy is included in the LICENSE file that
jjg@57 15 * accompanied this code).
jjg@57 16 *
jjg@57 17 * You should have received a copy of the GNU General Public License version
jjg@57 18 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@57 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@57 20 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
jjg@57 24 */
jjg@57 25
jjg@57 26 package com.sun.tools.javac.file;
jjg@57 27
jjg@57 28 import java.io.File;
jjg@57 29 import java.io.FileInputStream;
jjg@57 30 import java.io.FileOutputStream;
jjg@57 31 import java.io.IOException;
jjg@57 32 import java.io.InputStream;
jjg@57 33 import java.io.OutputStream;
jjg@57 34 import java.io.OutputStreamWriter;
jjg@57 35 import java.io.Writer;
jjg@424 36 import java.lang.ref.Reference;
jjg@424 37 import java.lang.ref.SoftReference;
jjg@57 38 import java.net.URI;
jjg@57 39 import java.nio.ByteBuffer;
jjg@57 40 import java.nio.CharBuffer;
jjg@57 41 import java.nio.charset.CharsetDecoder;
jjg@57 42 import javax.tools.JavaFileObject;
kizune@2037 43 import java.text.Normalizer;
jjg@57 44
jjg@57 45 /**
jjg@57 46 * A subclass of JavaFileObject representing regular files.
jjg@333 47 *
jjg@581 48 * <p><b>This is NOT part of any supported API.
jjg@333 49 * If you write code that depends on this, you do so at your own risk.
jjg@333 50 * This code and its internal interfaces are subject to change or
jjg@333 51 * deletion without notice.</b>
jjg@57 52 */
jjg@57 53 class RegularFileObject extends BaseFileObject {
jjg@57 54
jjg@57 55 /** Have the parent directories been created?
jjg@57 56 */
jjg@57 57 private boolean hasParents = false;
jjg@57 58 private String name;
jjg@424 59 final File file;
jjg@424 60 private Reference<File> absFileRef;
kizune@2037 61 final static boolean isMacOS = System.getProperty("os.name", "").contains("OS X");
jjg@57 62
jjg@57 63 public RegularFileObject(JavacFileManager fileManager, File f) {
jjg@57 64 this(fileManager, f.getName(), f);
jjg@57 65 }
jjg@57 66
jjg@57 67 public RegularFileObject(JavacFileManager fileManager, String name, File f) {
jjg@57 68 super(fileManager);
jjg@57 69 if (f.isDirectory()) {
jjg@57 70 throw new IllegalArgumentException("directories not supported");
jjg@57 71 }
jjg@57 72 this.name = name;
jjg@424 73 this.file = f;
jjg@57 74 }
jjg@57 75
jjg@415 76 @Override
jjg@415 77 public URI toUri() {
jjg@424 78 return file.toURI().normalize();
jjg@415 79 }
jjg@415 80
jjg@415 81 @Override
jjg@415 82 public String getName() {
jjg@424 83 return file.getPath();
jjg@415 84 }
jjg@415 85
jjg@415 86 @Override
jjg@415 87 public String getShortName() {
jjg@415 88 return name;
jjg@415 89 }
jjg@415 90
jjg@415 91 @Override
jjg@415 92 public JavaFileObject.Kind getKind() {
jjg@415 93 return getKind(name);
jjg@415 94 }
jjg@415 95
jjg@415 96 @Override
jjg@57 97 public InputStream openInputStream() throws IOException {
jjg@424 98 return new FileInputStream(file);
jjg@57 99 }
jjg@57 100
jjg@400 101 @Override
jjg@57 102 public OutputStream openOutputStream() throws IOException {
jjg@1080 103 fileManager.flushCache(this);
jjg@57 104 ensureParentDirectoriesExist();
jjg@424 105 return new FileOutputStream(file);
jjg@57 106 }
jjg@57 107
jjg@57 108 @Override
jjg@57 109 public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException {
jjg@57 110 CharBuffer cb = fileManager.getCachedContent(this);
jjg@57 111 if (cb == null) {
jjg@424 112 InputStream in = new FileInputStream(file);
jjg@57 113 try {
jjg@57 114 ByteBuffer bb = fileManager.makeByteBuffer(in);
jjg@57 115 JavaFileObject prev = fileManager.log.useSource(this);
jjg@57 116 try {
jjg@57 117 cb = fileManager.decode(bb, ignoreEncodingErrors);
jjg@57 118 } finally {
jjg@57 119 fileManager.log.useSource(prev);
jjg@57 120 }
jjg@57 121 fileManager.recycleByteBuffer(bb);
jjg@57 122 if (!ignoreEncodingErrors) {
jjg@57 123 fileManager.cache(this, cb);
jjg@57 124 }
jjg@57 125 } finally {
jjg@57 126 in.close();
jjg@57 127 }
jjg@57 128 }
jjg@57 129 return cb;
jjg@57 130 }
jjg@57 131
jjg@57 132 @Override
jjg@415 133 public Writer openWriter() throws IOException {
jjg@1080 134 fileManager.flushCache(this);
jjg@415 135 ensureParentDirectoriesExist();
jjg@424 136 return new OutputStreamWriter(new FileOutputStream(file), fileManager.getEncodingName());
jjg@415 137 }
jjg@415 138
jjg@415 139 @Override
jjg@415 140 public long getLastModified() {
jjg@424 141 return file.lastModified();
jjg@415 142 }
jjg@415 143
jjg@415 144 @Override
jjg@415 145 public boolean delete() {
jjg@424 146 return file.delete();
jjg@415 147 }
jjg@415 148
jjg@415 149 @Override
jjg@415 150 protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) {
jjg@415 151 return fileManager.getDecoder(fileManager.getEncodingName(), ignoreEncodingErrors);
jjg@415 152 }
jjg@415 153
jjg@415 154 @Override
jjg@415 155 protected String inferBinaryName(Iterable<? extends File> path) {
jjg@424 156 String fPath = file.getPath();
jjg@415 157 //System.err.println("RegularFileObject " + file + " " +r.getPath());
jjg@415 158 for (File dir: path) {
jjg@415 159 //System.err.println("dir: " + dir);
jjg@415 160 String dPath = dir.getPath();
jjg@415 161 if (dPath.length() == 0)
jjg@415 162 dPath = System.getProperty("user.dir");
jjg@415 163 if (!dPath.endsWith(File.separator))
jjg@415 164 dPath += File.separator;
jjg@415 165 if (fPath.regionMatches(true, 0, dPath, 0, dPath.length())
jjg@415 166 && new File(fPath.substring(0, dPath.length())).equals(new File(dPath))) {
jjg@415 167 String relativeName = fPath.substring(dPath.length());
jjg@415 168 return removeExtension(relativeName).replace(File.separatorChar, '.');
jjg@415 169 }
jjg@415 170 }
jjg@415 171 return null;
jjg@415 172 }
jjg@415 173
jjg@415 174 @Override
jjg@415 175 public boolean isNameCompatible(String cn, JavaFileObject.Kind kind) {
jjg@415 176 cn.getClass();
jjg@415 177 // null check
jjg@415 178 if (kind == Kind.OTHER && getKind() != kind) {
jjg@415 179 return false;
jjg@415 180 }
jjg@415 181 String n = cn + kind.extension;
jjg@415 182 if (name.equals(n)) {
jjg@415 183 return true;
jjg@415 184 }
kizune@2037 185 if (isMacOS && Normalizer.isNormalized(name, Normalizer.Form.NFD)
kizune@2037 186 && Normalizer.isNormalized(n, Normalizer.Form.NFC)) {
kizune@2037 187 // On Mac OS X it is quite possible to file name and class
kizune@2037 188 // name normalized in a different way - in that case we have to normalize file name
kizune@2037 189 // to the Normal Form Compised (NFC)
kizune@2037 190 String normName = Normalizer.normalize(name, Normalizer.Form.NFC);
kizune@2037 191 if (normName.equals(n)) {
kizune@2037 192 this.name = normName;
kizune@2037 193 return true;
kizune@2037 194 }
kizune@2037 195 }
kizune@2037 196
kizune@2037 197 if (name.equalsIgnoreCase(n)) {
jjg@415 198 try {
jjg@415 199 // allow for Windows
jjg@424 200 return file.getCanonicalFile().getName().equals(n);
jjg@415 201 } catch (IOException e) {
jjg@415 202 }
jjg@415 203 }
jjg@415 204 return false;
jjg@415 205 }
jjg@415 206
jjg@415 207 private void ensureParentDirectoriesExist() throws IOException {
jjg@415 208 if (!hasParents) {
jjg@424 209 File parent = file.getParentFile();
jjg@415 210 if (parent != null && !parent.exists()) {
jjg@415 211 if (!parent.mkdirs()) {
jjg@415 212 if (!parent.exists() || !parent.isDirectory()) {
jjg@415 213 throw new IOException("could not create parent directories");
jjg@415 214 }
jjg@415 215 }
jjg@415 216 }
jjg@415 217 hasParents = true;
jjg@415 218 }
jjg@415 219 }
jjg@415 220
jjg@424 221 /**
jjg@424 222 * Check if two file objects are equal.
jjg@424 223 * Two RegularFileObjects are equal if the absolute paths of the underlying
jjg@424 224 * files are equal.
jjg@424 225 */
jjg@415 226 @Override
jjg@57 227 public boolean equals(Object other) {
jjg@424 228 if (this == other)
jjg@424 229 return true;
jjg@424 230
jjg@424 231 if (!(other instanceof RegularFileObject))
jjg@57 232 return false;
jjg@424 233
jjg@57 234 RegularFileObject o = (RegularFileObject) other;
jjg@424 235 return getAbsoluteFile().equals(o.getAbsoluteFile());
jjg@57 236 }
jjg@57 237
jjg@57 238 @Override
jjg@57 239 public int hashCode() {
jjg@424 240 return getAbsoluteFile().hashCode();
jjg@424 241 }
jjg@424 242
jjg@424 243 private File getAbsoluteFile() {
jjg@424 244 File absFile = (absFileRef == null ? null : absFileRef.get());
jjg@424 245 if (absFile == null) {
jjg@424 246 absFile = file.getAbsoluteFile();
jjg@424 247 absFileRef = new SoftReference<File>(absFile);
jjg@424 248 }
jjg@424 249 return absFile;
jjg@57 250 }
jjg@57 251 }

mercurial