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

Thu, 30 Jul 2009 10:29:53 +0100

author
mcimadamore
date
Thu, 30 Jul 2009 10:29:53 +0100
changeset 341
85fecace920b
parent 333
7c2d6da61646
child 400
35e29f51a7c3
permissions
-rw-r--r--

6827648: Extremely slow compilation time for visitor pattern code + generics
Summary: Javac unnecessarily recomputates type-substitutions multiple times
Reviewed-by: jjg

jjg@57 1 /*
jjg@57 2 * Copyright 2005-2008 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@57 36 import java.net.URI;
jjg@57 37 import java.net.URISyntaxException;
jjg@57 38 import java.nio.ByteBuffer;
jjg@57 39 import java.nio.CharBuffer;
jjg@57 40 import java.nio.charset.CharsetDecoder;
jjg@57 41 import javax.tools.JavaFileObject;
jjg@57 42
jjg@57 43 /**
jjg@57 44 * A subclass of JavaFileObject representing regular files.
jjg@333 45 *
jjg@333 46 * <p><b>This is NOT part of any API supported by Sun Microsystems.
jjg@333 47 * If you write code that depends on this, you do so at your own risk.
jjg@333 48 * This code and its internal interfaces are subject to change or
jjg@333 49 * deletion without notice.</b>
jjg@57 50 */
jjg@57 51 class RegularFileObject extends BaseFileObject {
jjg@57 52
jjg@57 53 /** Have the parent directories been created?
jjg@57 54 */
jjg@57 55 private boolean hasParents = false;
jjg@57 56 private String name;
jjg@57 57 final File f;
jjg@57 58
jjg@57 59 public RegularFileObject(JavacFileManager fileManager, File f) {
jjg@57 60 this(fileManager, f.getName(), f);
jjg@57 61 }
jjg@57 62
jjg@57 63 public RegularFileObject(JavacFileManager fileManager, String name, File f) {
jjg@57 64 super(fileManager);
jjg@57 65 if (f.isDirectory()) {
jjg@57 66 throw new IllegalArgumentException("directories not supported");
jjg@57 67 }
jjg@57 68 this.name = name;
jjg@57 69 this.f = f;
jjg@57 70 }
jjg@57 71
jjg@57 72 public InputStream openInputStream() throws IOException {
jjg@57 73 return new FileInputStream(f);
jjg@57 74 }
jjg@57 75
jjg@57 76 protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) {
jjg@57 77 return fileManager.getDecoder(fileManager.getEncodingName(), ignoreEncodingErrors);
jjg@57 78 }
jjg@57 79
jjg@57 80 public OutputStream openOutputStream() throws IOException {
jjg@57 81 ensureParentDirectoriesExist();
jjg@57 82 return new FileOutputStream(f);
jjg@57 83 }
jjg@57 84
jjg@57 85 public Writer openWriter() throws IOException {
jjg@57 86 ensureParentDirectoriesExist();
jjg@57 87 return new OutputStreamWriter(new FileOutputStream(f), fileManager.getEncodingName());
jjg@57 88 }
jjg@57 89
jjg@57 90 @Override
jjg@57 91 protected String inferBinaryName(Iterable<? extends File> path) {
jjg@57 92 String fPath = f.getPath();
jjg@57 93 //System.err.println("RegularFileObject " + file + " " +r.getPath());
jjg@57 94 for (File dir: path) {
jjg@57 95 //System.err.println("dir: " + dir);
jjg@57 96 String dPath = dir.getPath();
jjg@144 97 if (dPath.length() == 0)
jjg@144 98 dPath = System.getProperty("user.dir");
jjg@57 99 if (!dPath.endsWith(File.separator))
jjg@57 100 dPath += File.separator;
jjg@57 101 if (fPath.regionMatches(true, 0, dPath, 0, dPath.length())
jjg@57 102 && new File(fPath.substring(0, dPath.length())).equals(new File(dPath))) {
jjg@57 103 String relativeName = fPath.substring(dPath.length());
jjg@57 104 return removeExtension(relativeName).replace(File.separatorChar, '.');
jjg@57 105 }
jjg@57 106 }
jjg@57 107 return null;
jjg@57 108 }
jjg@57 109
jjg@57 110 private void ensureParentDirectoriesExist() throws IOException {
jjg@57 111 if (!hasParents) {
jjg@57 112 File parent = f.getParentFile();
jjg@57 113 if (parent != null && !parent.exists()) {
jjg@57 114 if (!parent.mkdirs()) {
jjg@57 115 if (!parent.exists() || !parent.isDirectory()) {
jjg@57 116 throw new IOException("could not create parent directories");
jjg@57 117 }
jjg@57 118 }
jjg@57 119 }
jjg@57 120 hasParents = true;
jjg@57 121 }
jjg@57 122 }
jjg@57 123
jjg@57 124 @Deprecated
jjg@57 125 public String getName() {
jjg@57 126 return name;
jjg@57 127 }
jjg@57 128
jjg@57 129 public boolean isNameCompatible(String cn, JavaFileObject.Kind kind) {
jjg@57 130 cn.getClass();
jjg@57 131 // null check
jjg@57 132 if (kind == Kind.OTHER && getKind() != kind) {
jjg@57 133 return false;
jjg@57 134 }
jjg@57 135 String n = cn + kind.extension;
jjg@57 136 if (name.equals(n)) {
jjg@57 137 return true;
jjg@57 138 }
jjg@57 139 if (name.equalsIgnoreCase(n)) {
jjg@57 140 try {
jjg@57 141 // allow for Windows
jjg@57 142 return f.getCanonicalFile().getName().equals(n);
jjg@57 143 } catch (IOException e) {
jjg@57 144 }
jjg@57 145 }
jjg@57 146 return false;
jjg@57 147 }
jjg@57 148
jjg@57 149 @Deprecated
jjg@57 150 public String getPath() {
jjg@57 151 return f.getPath();
jjg@57 152 }
jjg@57 153
jjg@57 154 public long getLastModified() {
jjg@57 155 return f.lastModified();
jjg@57 156 }
jjg@57 157
jjg@57 158 public boolean delete() {
jjg@57 159 return f.delete();
jjg@57 160 }
jjg@57 161
jjg@57 162 public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException {
jjg@57 163 CharBuffer cb = fileManager.getCachedContent(this);
jjg@57 164 if (cb == null) {
jjg@57 165 InputStream in = new FileInputStream(f);
jjg@57 166 try {
jjg@57 167 ByteBuffer bb = fileManager.makeByteBuffer(in);
jjg@57 168 JavaFileObject prev = fileManager.log.useSource(this);
jjg@57 169 try {
jjg@57 170 cb = fileManager.decode(bb, ignoreEncodingErrors);
jjg@57 171 } finally {
jjg@57 172 fileManager.log.useSource(prev);
jjg@57 173 }
jjg@57 174 fileManager.recycleByteBuffer(bb);
jjg@57 175 if (!ignoreEncodingErrors) {
jjg@57 176 fileManager.cache(this, cb);
jjg@57 177 }
jjg@57 178 } finally {
jjg@57 179 in.close();
jjg@57 180 }
jjg@57 181 }
jjg@57 182 return cb;
jjg@57 183 }
jjg@57 184
jjg@57 185 @Override
jjg@57 186 public boolean equals(Object other) {
jjg@57 187 if (!(other instanceof RegularFileObject)) {
jjg@57 188 return false;
jjg@57 189 }
jjg@57 190 RegularFileObject o = (RegularFileObject) other;
jjg@57 191 try {
jjg@57 192 return f.equals(o.f) || f.getCanonicalFile().equals(o.f.getCanonicalFile());
jjg@57 193 } catch (IOException e) {
jjg@57 194 return false;
jjg@57 195 }
jjg@57 196 }
jjg@57 197
jjg@57 198 @Override
jjg@57 199 public int hashCode() {
jjg@57 200 return f.hashCode();
jjg@57 201 }
jjg@57 202
jjg@57 203 public URI toUri() {
jjg@57 204 try {
jjg@57 205 String path = f.getAbsolutePath().replace(File.separatorChar, '/');
jjg@57 206 return new URI("file://" + path).normalize();
jjg@57 207 } catch (URISyntaxException ex) {
jjg@57 208 return f.toURI();
jjg@57 209 }
jjg@57 210 }
jjg@57 211 }

mercurial