Wed, 18 Jun 2008 07:23:25 -0700
6714365: refactor JavacFileManager to move nested classes to top level
Reviewed-by: mcimadamore
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.IOException; |
jjg@57 | 29 | import java.util.Set; |
jjg@57 | 30 | import javax.tools.JavaFileObject; |
jjg@57 | 31 | |
jjg@57 | 32 | import com.sun.tools.javac.file.JavacFileManager.Archive; |
jjg@57 | 33 | import com.sun.tools.javac.util.List; |
jjg@57 | 34 | import java.io.ByteArrayInputStream; |
jjg@57 | 35 | import java.io.File; |
jjg@57 | 36 | import java.io.FileNotFoundException; |
jjg@57 | 37 | import java.io.InputStream; |
jjg@57 | 38 | import java.io.OutputStream; |
jjg@57 | 39 | import java.io.Writer; |
jjg@57 | 40 | import java.net.URI; |
jjg@57 | 41 | import java.nio.ByteBuffer; |
jjg@57 | 42 | import java.nio.CharBuffer; |
jjg@57 | 43 | import java.nio.charset.CharsetDecoder; |
jjg@57 | 44 | |
jjg@57 | 45 | public class ZipFileIndexArchive implements Archive { |
jjg@57 | 46 | |
jjg@57 | 47 | private final ZipFileIndex zfIndex; |
jjg@57 | 48 | private JavacFileManager fileManager; |
jjg@57 | 49 | |
jjg@57 | 50 | public ZipFileIndexArchive(JavacFileManager fileManager, ZipFileIndex zdir) throws IOException { |
jjg@57 | 51 | super(); |
jjg@57 | 52 | this.fileManager = fileManager; |
jjg@57 | 53 | this.zfIndex = zdir; |
jjg@57 | 54 | } |
jjg@57 | 55 | |
jjg@57 | 56 | public boolean contains(String name) { |
jjg@57 | 57 | return zfIndex.contains(name); |
jjg@57 | 58 | } |
jjg@57 | 59 | |
jjg@57 | 60 | public List<String> getFiles(String subdirectory) { |
jjg@57 | 61 | return zfIndex.getFiles((subdirectory.endsWith("/") || subdirectory.endsWith("\\")) ? subdirectory.substring(0, subdirectory.length() - 1) : subdirectory); |
jjg@57 | 62 | } |
jjg@57 | 63 | |
jjg@57 | 64 | public JavaFileObject getFileObject(String subdirectory, String file) { |
jjg@57 | 65 | String fullZipFileName = subdirectory + file; |
jjg@57 | 66 | ZipFileIndex.Entry entry = zfIndex.getZipIndexEntry(fullZipFileName); |
jjg@57 | 67 | JavaFileObject ret = new ZipFileIndexFileObject(fileManager, zfIndex, entry, zfIndex.getZipFile().getPath()); |
jjg@57 | 68 | return ret; |
jjg@57 | 69 | } |
jjg@57 | 70 | |
jjg@57 | 71 | public Set<String> getSubdirectories() { |
jjg@57 | 72 | return zfIndex.getAllDirectories(); |
jjg@57 | 73 | } |
jjg@57 | 74 | |
jjg@57 | 75 | public void close() throws IOException { |
jjg@57 | 76 | zfIndex.close(); |
jjg@57 | 77 | } |
jjg@57 | 78 | |
jjg@57 | 79 | /** |
jjg@57 | 80 | * A subclass of JavaFileObject representing zip entries using the com.sun.tools.javac.file.ZipFileIndex implementation. |
jjg@57 | 81 | */ |
jjg@57 | 82 | public static class ZipFileIndexFileObject extends BaseFileObject { |
jjg@57 | 83 | |
jjg@57 | 84 | /** The entry's name. |
jjg@57 | 85 | */ |
jjg@57 | 86 | private String name; |
jjg@57 | 87 | |
jjg@57 | 88 | /** The zipfile containing the entry. |
jjg@57 | 89 | */ |
jjg@57 | 90 | ZipFileIndex zfIndex; |
jjg@57 | 91 | |
jjg@57 | 92 | /** The underlying zip entry object. |
jjg@57 | 93 | */ |
jjg@57 | 94 | ZipFileIndex.Entry entry; |
jjg@57 | 95 | |
jjg@57 | 96 | /** The InputStream for this zip entry (file.) |
jjg@57 | 97 | */ |
jjg@57 | 98 | InputStream inputStream = null; |
jjg@57 | 99 | |
jjg@57 | 100 | /** The name of the zip file where this entry resides. |
jjg@57 | 101 | */ |
jjg@57 | 102 | String zipName; |
jjg@57 | 103 | |
jjg@57 | 104 | |
jjg@57 | 105 | ZipFileIndexFileObject(JavacFileManager fileManager, ZipFileIndex zfIndex, ZipFileIndex.Entry entry, String zipFileName) { |
jjg@57 | 106 | super(fileManager); |
jjg@57 | 107 | this.name = entry.getFileName(); |
jjg@57 | 108 | this.zfIndex = zfIndex; |
jjg@57 | 109 | this.entry = entry; |
jjg@57 | 110 | this.zipName = zipFileName; |
jjg@57 | 111 | } |
jjg@57 | 112 | |
jjg@57 | 113 | public InputStream openInputStream() throws IOException { |
jjg@57 | 114 | |
jjg@57 | 115 | if (inputStream == null) { |
jjg@57 | 116 | inputStream = new ByteArrayInputStream(read()); |
jjg@57 | 117 | } |
jjg@57 | 118 | return inputStream; |
jjg@57 | 119 | } |
jjg@57 | 120 | |
jjg@57 | 121 | protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) { |
jjg@57 | 122 | return fileManager.getDecoder(fileManager.getEncodingName(), ignoreEncodingErrors); |
jjg@57 | 123 | } |
jjg@57 | 124 | |
jjg@57 | 125 | public OutputStream openOutputStream() throws IOException { |
jjg@57 | 126 | throw new UnsupportedOperationException(); |
jjg@57 | 127 | } |
jjg@57 | 128 | |
jjg@57 | 129 | public Writer openWriter() throws IOException { |
jjg@57 | 130 | throw new UnsupportedOperationException(); |
jjg@57 | 131 | } |
jjg@57 | 132 | |
jjg@57 | 133 | /** @deprecated see bug 6410637 */ |
jjg@57 | 134 | @Deprecated |
jjg@57 | 135 | public String getName() { |
jjg@57 | 136 | return name; |
jjg@57 | 137 | } |
jjg@57 | 138 | |
jjg@57 | 139 | public boolean isNameCompatible(String cn, JavaFileObject.Kind k) { |
jjg@57 | 140 | cn.getClass(); // null check |
jjg@57 | 141 | if (k == Kind.OTHER && getKind() != k) |
jjg@57 | 142 | return false; |
jjg@57 | 143 | return name.equals(cn + k.extension); |
jjg@57 | 144 | } |
jjg@57 | 145 | |
jjg@57 | 146 | /** @deprecated see bug 6410637 */ |
jjg@57 | 147 | @Deprecated |
jjg@57 | 148 | public String getPath() { |
jjg@57 | 149 | return zipName + "(" + entry.getName() + ")"; |
jjg@57 | 150 | } |
jjg@57 | 151 | |
jjg@57 | 152 | public long getLastModified() { |
jjg@57 | 153 | return entry.getLastModified(); |
jjg@57 | 154 | } |
jjg@57 | 155 | |
jjg@57 | 156 | public boolean delete() { |
jjg@57 | 157 | throw new UnsupportedOperationException(); |
jjg@57 | 158 | } |
jjg@57 | 159 | |
jjg@57 | 160 | @Override |
jjg@57 | 161 | public boolean equals(Object other) { |
jjg@57 | 162 | if (!(other instanceof ZipFileIndexFileObject)) |
jjg@57 | 163 | return false; |
jjg@57 | 164 | ZipFileIndexFileObject o = (ZipFileIndexFileObject) other; |
jjg@57 | 165 | return entry.equals(o.entry); |
jjg@57 | 166 | } |
jjg@57 | 167 | |
jjg@57 | 168 | @Override |
jjg@57 | 169 | public int hashCode() { |
jjg@57 | 170 | return zipName.hashCode() + (name.hashCode() << 10); |
jjg@57 | 171 | } |
jjg@57 | 172 | |
jjg@57 | 173 | public String getZipName() { |
jjg@57 | 174 | return zipName; |
jjg@57 | 175 | } |
jjg@57 | 176 | |
jjg@57 | 177 | public String getZipEntryName() { |
jjg@57 | 178 | return entry.getName(); |
jjg@57 | 179 | } |
jjg@57 | 180 | |
jjg@57 | 181 | public URI toUri() { |
jjg@57 | 182 | String zipName = new File(getZipName()).toURI().normalize().getPath(); |
jjg@57 | 183 | String entryName = getZipEntryName(); |
jjg@57 | 184 | if (File.separatorChar != '/') { |
jjg@57 | 185 | entryName = entryName.replace(File.separatorChar, '/'); |
jjg@57 | 186 | } |
jjg@57 | 187 | return URI.create("jar:" + zipName + "!" + entryName); |
jjg@57 | 188 | } |
jjg@57 | 189 | |
jjg@57 | 190 | private byte[] read() throws IOException { |
jjg@57 | 191 | if (entry == null) { |
jjg@57 | 192 | entry = zfIndex.getZipIndexEntry(name); |
jjg@57 | 193 | if (entry == null) |
jjg@57 | 194 | throw new FileNotFoundException(); |
jjg@57 | 195 | } |
jjg@57 | 196 | return zfIndex.read(entry); |
jjg@57 | 197 | } |
jjg@57 | 198 | |
jjg@57 | 199 | public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException { |
jjg@57 | 200 | CharBuffer cb = fileManager.getCachedContent(this); |
jjg@57 | 201 | if (cb == null) { |
jjg@57 | 202 | InputStream in = new ByteArrayInputStream(zfIndex.read(entry)); |
jjg@57 | 203 | try { |
jjg@57 | 204 | ByteBuffer bb = fileManager.makeByteBuffer(in); |
jjg@57 | 205 | JavaFileObject prev = fileManager.log.useSource(this); |
jjg@57 | 206 | try { |
jjg@57 | 207 | cb = fileManager.decode(bb, ignoreEncodingErrors); |
jjg@57 | 208 | } finally { |
jjg@57 | 209 | fileManager.log.useSource(prev); |
jjg@57 | 210 | } |
jjg@57 | 211 | fileManager.recycleByteBuffer(bb); // save for next time |
jjg@57 | 212 | if (!ignoreEncodingErrors) |
jjg@57 | 213 | fileManager.cache(this, cb); |
jjg@57 | 214 | } finally { |
jjg@57 | 215 | in.close(); |
jjg@57 | 216 | } |
jjg@57 | 217 | } |
jjg@57 | 218 | return cb; |
jjg@57 | 219 | } |
jjg@57 | 220 | |
jjg@57 | 221 | @Override |
jjg@57 | 222 | protected String inferBinaryName(Iterable<? extends File> path) { |
jjg@57 | 223 | String entryName = getZipEntryName(); |
jjg@57 | 224 | if (zfIndex.symbolFilePrefix != null) { |
jjg@57 | 225 | String prefix = zfIndex.symbolFilePrefix; |
jjg@57 | 226 | if (entryName.startsWith(prefix)) |
jjg@57 | 227 | entryName = entryName.substring(prefix.length()); |
jjg@57 | 228 | } |
jjg@57 | 229 | return removeExtension(entryName).replace(File.separatorChar, '.'); |
jjg@57 | 230 | } |
jjg@57 | 231 | } |
jjg@57 | 232 | |
jjg@57 | 233 | } |