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

Wed, 14 Oct 2009 15:41:28 -0700

author
jjg
date
Wed, 14 Oct 2009 15:41:28 -0700
changeset 424
86b773b7cb40
parent 418
4776a869fdfa
child 554
9d9f26857129
permissions
-rw-r--r--

6838467: JSR199 FileObjects don't obey general contract of equals.
Reviewed-by: darcy

jjg@57 1 /*
xdono@404 2 * Copyright 2005-2009 Sun Microsystems, Inc. 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
jjg@57 7 * published by the Free Software Foundation. Sun designates this
jjg@57 8 * particular file as subject to the "Classpath" exception as provided
jjg@57 9 * by Sun 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 *
jjg@57 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
jjg@57 22 * CA 95054 USA or visit www.sun.com if you need additional information or
jjg@57 23 * have any 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;
jjg@57 43
jjg@57 44 /**
jjg@57 45 * A subclass of JavaFileObject representing regular files.
jjg@333 46 *
jjg@333 47 * <p><b>This is NOT part of any API supported by Sun Microsystems.
jjg@333 48 * If you write code that depends on this, you do so at your own risk.
jjg@333 49 * This code and its internal interfaces are subject to change or
jjg@333 50 * deletion without notice.</b>
jjg@57 51 */
jjg@57 52 class RegularFileObject extends BaseFileObject {
jjg@57 53
jjg@57 54 /** Have the parent directories been created?
jjg@57 55 */
jjg@57 56 private boolean hasParents = false;
jjg@57 57 private String name;
jjg@424 58 final File file;
jjg@424 59 private Reference<File> absFileRef;
jjg@57 60
jjg@57 61 public RegularFileObject(JavacFileManager fileManager, File f) {
jjg@57 62 this(fileManager, f.getName(), f);
jjg@57 63 }
jjg@57 64
jjg@57 65 public RegularFileObject(JavacFileManager fileManager, String name, File f) {
jjg@57 66 super(fileManager);
jjg@57 67 if (f.isDirectory()) {
jjg@57 68 throw new IllegalArgumentException("directories not supported");
jjg@57 69 }
jjg@57 70 this.name = name;
jjg@424 71 this.file = f;
jjg@57 72 }
jjg@57 73
jjg@415 74 @Override
jjg@415 75 public URI toUri() {
jjg@424 76 return file.toURI().normalize();
jjg@415 77 }
jjg@415 78
jjg@415 79 @Override
jjg@415 80 public String getName() {
jjg@424 81 return file.getPath();
jjg@415 82 }
jjg@415 83
jjg@415 84 @Override
jjg@415 85 public String getShortName() {
jjg@415 86 return name;
jjg@415 87 }
jjg@415 88
jjg@415 89 @Override
jjg@415 90 public JavaFileObject.Kind getKind() {
jjg@415 91 return getKind(name);
jjg@415 92 }
jjg@415 93
jjg@415 94 @Override
jjg@57 95 public InputStream openInputStream() throws IOException {
jjg@424 96 return new FileInputStream(file);
jjg@57 97 }
jjg@57 98
jjg@400 99 @Override
jjg@57 100 public OutputStream openOutputStream() throws IOException {
jjg@57 101 ensureParentDirectoriesExist();
jjg@424 102 return new FileOutputStream(file);
jjg@57 103 }
jjg@57 104
jjg@57 105 @Override
jjg@57 106 public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException {
jjg@57 107 CharBuffer cb = fileManager.getCachedContent(this);
jjg@57 108 if (cb == null) {
jjg@424 109 InputStream in = new FileInputStream(file);
jjg@57 110 try {
jjg@57 111 ByteBuffer bb = fileManager.makeByteBuffer(in);
jjg@57 112 JavaFileObject prev = fileManager.log.useSource(this);
jjg@57 113 try {
jjg@57 114 cb = fileManager.decode(bb, ignoreEncodingErrors);
jjg@57 115 } finally {
jjg@57 116 fileManager.log.useSource(prev);
jjg@57 117 }
jjg@57 118 fileManager.recycleByteBuffer(bb);
jjg@57 119 if (!ignoreEncodingErrors) {
jjg@57 120 fileManager.cache(this, cb);
jjg@57 121 }
jjg@57 122 } finally {
jjg@57 123 in.close();
jjg@57 124 }
jjg@57 125 }
jjg@57 126 return cb;
jjg@57 127 }
jjg@57 128
jjg@57 129 @Override
jjg@415 130 public Writer openWriter() throws IOException {
jjg@415 131 ensureParentDirectoriesExist();
jjg@424 132 return new OutputStreamWriter(new FileOutputStream(file), fileManager.getEncodingName());
jjg@415 133 }
jjg@415 134
jjg@415 135 @Override
jjg@415 136 public long getLastModified() {
jjg@424 137 return file.lastModified();
jjg@415 138 }
jjg@415 139
jjg@415 140 @Override
jjg@415 141 public boolean delete() {
jjg@424 142 return file.delete();
jjg@415 143 }
jjg@415 144
jjg@415 145 @Override
jjg@415 146 protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) {
jjg@415 147 return fileManager.getDecoder(fileManager.getEncodingName(), ignoreEncodingErrors);
jjg@415 148 }
jjg@415 149
jjg@415 150 @Override
jjg@415 151 protected String inferBinaryName(Iterable<? extends File> path) {
jjg@424 152 String fPath = file.getPath();
jjg@415 153 //System.err.println("RegularFileObject " + file + " " +r.getPath());
jjg@415 154 for (File dir: path) {
jjg@415 155 //System.err.println("dir: " + dir);
jjg@415 156 String dPath = dir.getPath();
jjg@415 157 if (dPath.length() == 0)
jjg@415 158 dPath = System.getProperty("user.dir");
jjg@415 159 if (!dPath.endsWith(File.separator))
jjg@415 160 dPath += File.separator;
jjg@415 161 if (fPath.regionMatches(true, 0, dPath, 0, dPath.length())
jjg@415 162 && new File(fPath.substring(0, dPath.length())).equals(new File(dPath))) {
jjg@415 163 String relativeName = fPath.substring(dPath.length());
jjg@415 164 return removeExtension(relativeName).replace(File.separatorChar, '.');
jjg@415 165 }
jjg@415 166 }
jjg@415 167 return null;
jjg@415 168 }
jjg@415 169
jjg@415 170 @Override
jjg@415 171 public boolean isNameCompatible(String cn, JavaFileObject.Kind kind) {
jjg@415 172 cn.getClass();
jjg@415 173 // null check
jjg@415 174 if (kind == Kind.OTHER && getKind() != kind) {
jjg@415 175 return false;
jjg@415 176 }
jjg@415 177 String n = cn + kind.extension;
jjg@415 178 if (name.equals(n)) {
jjg@415 179 return true;
jjg@415 180 }
jjg@415 181 if (name.equalsIgnoreCase(n)) {
jjg@415 182 try {
jjg@415 183 // allow for Windows
jjg@424 184 return file.getCanonicalFile().getName().equals(n);
jjg@415 185 } catch (IOException e) {
jjg@415 186 }
jjg@415 187 }
jjg@415 188 return false;
jjg@415 189 }
jjg@415 190
jjg@415 191 private void ensureParentDirectoriesExist() throws IOException {
jjg@415 192 if (!hasParents) {
jjg@424 193 File parent = file.getParentFile();
jjg@415 194 if (parent != null && !parent.exists()) {
jjg@415 195 if (!parent.mkdirs()) {
jjg@415 196 if (!parent.exists() || !parent.isDirectory()) {
jjg@415 197 throw new IOException("could not create parent directories");
jjg@415 198 }
jjg@415 199 }
jjg@415 200 }
jjg@415 201 hasParents = true;
jjg@415 202 }
jjg@415 203 }
jjg@415 204
jjg@424 205 /**
jjg@424 206 * Check if two file objects are equal.
jjg@424 207 * Two RegularFileObjects are equal if the absolute paths of the underlying
jjg@424 208 * files are equal.
jjg@424 209 */
jjg@415 210 @Override
jjg@57 211 public boolean equals(Object other) {
jjg@424 212 if (this == other)
jjg@424 213 return true;
jjg@424 214
jjg@424 215 if (!(other instanceof RegularFileObject))
jjg@57 216 return false;
jjg@424 217
jjg@57 218 RegularFileObject o = (RegularFileObject) other;
jjg@424 219 return getAbsoluteFile().equals(o.getAbsoluteFile());
jjg@57 220 }
jjg@57 221
jjg@57 222 @Override
jjg@57 223 public int hashCode() {
jjg@424 224 return getAbsoluteFile().hashCode();
jjg@424 225 }
jjg@424 226
jjg@424 227 private File getAbsoluteFile() {
jjg@424 228 File absFile = (absFileRef == null ? null : absFileRef.get());
jjg@424 229 if (absFile == null) {
jjg@424 230 absFile = file.getAbsoluteFile();
jjg@424 231 absFileRef = new SoftReference<File>(absFile);
jjg@424 232 }
jjg@424 233 return absFile;
jjg@57 234 }
jjg@57 235 }

mercurial