Tue, 25 Sep 2012 13:11:05 -0700
7196464: upgrade JavaCompiler.shouldStopPolicy to accomodate policies in face of error and no error
Reviewed-by: mcimadamore
duke@1 | 1 | /* |
jjg@818 | 2 | * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 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 |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@554 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
duke@1 | 10 | * |
duke@1 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 15 | * accompanied this code). |
duke@1 | 16 | * |
duke@1 | 17 | * You should have received a copy of the GNU General Public License version |
duke@1 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 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. |
duke@1 | 24 | */ |
duke@1 | 25 | |
jjg@50 | 26 | package com.sun.tools.javac.file; |
duke@1 | 27 | |
duke@1 | 28 | import java.io.ByteArrayOutputStream; |
duke@1 | 29 | import java.io.File; |
duke@1 | 30 | import java.io.FileNotFoundException; |
duke@1 | 31 | import java.io.IOException; |
duke@1 | 32 | import java.io.OutputStreamWriter; |
duke@1 | 33 | import java.net.MalformedURLException; |
duke@1 | 34 | import java.net.URI; |
jjg@400 | 35 | import java.net.URISyntaxException; |
duke@1 | 36 | import java.net.URL; |
duke@1 | 37 | import java.nio.CharBuffer; |
duke@1 | 38 | import java.nio.charset.Charset; |
duke@1 | 39 | import java.util.ArrayList; |
duke@1 | 40 | import java.util.Arrays; |
duke@1 | 41 | import java.util.Collection; |
duke@1 | 42 | import java.util.Collections; |
jjg@1111 | 43 | import java.util.Comparator; |
duke@1 | 44 | import java.util.EnumSet; |
duke@1 | 45 | import java.util.HashMap; |
duke@1 | 46 | import java.util.Iterator; |
duke@1 | 47 | import java.util.Map; |
duke@1 | 48 | import java.util.Set; |
duke@1 | 49 | import java.util.zip.ZipFile; |
duke@1 | 50 | |
duke@1 | 51 | import javax.lang.model.SourceVersion; |
duke@1 | 52 | import javax.tools.FileObject; |
duke@1 | 53 | import javax.tools.JavaFileManager; |
duke@1 | 54 | import javax.tools.JavaFileObject; |
jjg@50 | 55 | import javax.tools.StandardJavaFileManager; |
duke@1 | 56 | |
jjg@103 | 57 | import com.sun.tools.javac.file.RelativePath.RelativeFile; |
jjg@103 | 58 | import com.sun.tools.javac.file.RelativePath.RelativeDirectory; |
jjg@450 | 59 | import com.sun.tools.javac.util.BaseFileManager; |
jjg@50 | 60 | import com.sun.tools.javac.util.Context; |
jjg@50 | 61 | import com.sun.tools.javac.util.List; |
jjg@50 | 62 | import com.sun.tools.javac.util.ListBuffer; |
duke@1 | 63 | |
jjg@103 | 64 | import static javax.tools.StandardLocation.*; |
duke@1 | 65 | |
duke@1 | 66 | /** |
duke@1 | 67 | * This class provides access to the source, class and other files |
duke@1 | 68 | * used by the compiler and related tools. |
jjg@333 | 69 | * |
jjg@581 | 70 | * <p><b>This is NOT part of any supported API. |
jjg@333 | 71 | * If you write code that depends on this, you do so at your own risk. |
jjg@333 | 72 | * This code and its internal interfaces are subject to change or |
jjg@333 | 73 | * deletion without notice.</b> |
duke@1 | 74 | */ |
jjg@450 | 75 | public class JavacFileManager extends BaseFileManager implements StandardJavaFileManager { |
duke@1 | 76 | |
duke@1 | 77 | public static char[] toArray(CharBuffer buffer) { |
duke@1 | 78 | if (buffer.hasArray()) |
duke@1 | 79 | return ((CharBuffer)buffer.compact().flip()).array(); |
duke@1 | 80 | else |
duke@1 | 81 | return buffer.toString().toCharArray(); |
duke@1 | 82 | } |
duke@1 | 83 | |
jjg@106 | 84 | private FSInfo fsInfo; |
jjg@106 | 85 | |
ksrini@923 | 86 | private boolean contextUseOptimizedZip; |
jjg@839 | 87 | private ZipFileIndexCache zipFileIndexCache; |
jjg@839 | 88 | |
duke@1 | 89 | private final Set<JavaFileObject.Kind> sourceOrClass = |
duke@1 | 90 | EnumSet.of(JavaFileObject.Kind.SOURCE, JavaFileObject.Kind.CLASS); |
duke@1 | 91 | |
duke@1 | 92 | protected boolean mmappedIO; |
duke@1 | 93 | protected boolean ignoreSymbolFile; |
duke@1 | 94 | |
jjg@756 | 95 | protected enum SortFiles implements Comparator<File> { |
jjg@756 | 96 | FORWARD { |
jjg@756 | 97 | public int compare(File f1, File f2) { |
jjg@756 | 98 | return f1.getName().compareTo(f2.getName()); |
jjg@756 | 99 | } |
jjg@756 | 100 | }, |
jjg@756 | 101 | REVERSE { |
jjg@756 | 102 | public int compare(File f1, File f2) { |
jjg@756 | 103 | return -f1.getName().compareTo(f2.getName()); |
jjg@756 | 104 | } |
jjg@756 | 105 | }; |
jjg@756 | 106 | }; |
jjg@756 | 107 | protected SortFiles sortFiles; |
jjg@756 | 108 | |
duke@1 | 109 | /** |
duke@1 | 110 | * Register a Context.Factory to create a JavacFileManager. |
duke@1 | 111 | */ |
jjg@893 | 112 | public static void preRegister(Context context) { |
duke@1 | 113 | context.put(JavaFileManager.class, new Context.Factory<JavaFileManager>() { |
jjg@893 | 114 | public JavaFileManager make(Context c) { |
jjg@893 | 115 | return new JavacFileManager(c, true, null); |
duke@1 | 116 | } |
duke@1 | 117 | }); |
duke@1 | 118 | } |
duke@1 | 119 | |
duke@1 | 120 | /** |
duke@1 | 121 | * Create a JavacFileManager using a given context, optionally registering |
duke@1 | 122 | * it as the JavaFileManager for that context. |
duke@1 | 123 | */ |
duke@1 | 124 | public JavacFileManager(Context context, boolean register, Charset charset) { |
jjg@450 | 125 | super(charset); |
duke@1 | 126 | if (register) |
duke@1 | 127 | context.put(JavaFileManager.class, this); |
duke@1 | 128 | setContext(context); |
duke@1 | 129 | } |
duke@1 | 130 | |
duke@1 | 131 | /** |
duke@1 | 132 | * Set the context for JavacFileManager. |
duke@1 | 133 | */ |
jjg@450 | 134 | @Override |
duke@1 | 135 | public void setContext(Context context) { |
jjg@450 | 136 | super.setContext(context); |
duke@1 | 137 | |
jjg@106 | 138 | fsInfo = FSInfo.instance(context); |
duke@1 | 139 | |
ksrini@923 | 140 | contextUseOptimizedZip = options.getBoolean("useOptimizedZip", true); |
ksrini@923 | 141 | if (contextUseOptimizedZip) |
jjg@839 | 142 | zipFileIndexCache = ZipFileIndexCache.getSharedInstance(); |
duke@1 | 143 | |
jjg@700 | 144 | mmappedIO = options.isSet("mmappedIO"); |
jjg@700 | 145 | ignoreSymbolFile = options.isSet("ignore.symbol.file"); |
jjg@756 | 146 | |
jjg@756 | 147 | String sf = options.get("sortFiles"); |
jjg@756 | 148 | if (sf != null) { |
jjg@756 | 149 | sortFiles = (sf.equals("reverse") ? SortFiles.REVERSE : SortFiles.FORWARD); |
jjg@756 | 150 | } |
duke@1 | 151 | } |
duke@1 | 152 | |
jjg@757 | 153 | @Override |
jjg@757 | 154 | public boolean isDefaultBootClassPath() { |
jjg@1116 | 155 | return locations.isDefaultBootClassPath(); |
jjg@757 | 156 | } |
jjg@757 | 157 | |
duke@1 | 158 | public JavaFileObject getFileForInput(String name) { |
duke@1 | 159 | return getRegularFile(new File(name)); |
duke@1 | 160 | } |
duke@1 | 161 | |
duke@1 | 162 | public JavaFileObject getRegularFile(File file) { |
jjg@57 | 163 | return new RegularFileObject(this, file); |
duke@1 | 164 | } |
duke@1 | 165 | |
duke@1 | 166 | public JavaFileObject getFileForOutput(String classname, |
duke@1 | 167 | JavaFileObject.Kind kind, |
duke@1 | 168 | JavaFileObject sibling) |
duke@1 | 169 | throws IOException |
duke@1 | 170 | { |
duke@1 | 171 | return getJavaFileForOutput(CLASS_OUTPUT, classname, kind, sibling); |
duke@1 | 172 | } |
duke@1 | 173 | |
duke@1 | 174 | public Iterable<? extends JavaFileObject> getJavaFileObjectsFromStrings(Iterable<String> names) { |
duke@1 | 175 | ListBuffer<File> files = new ListBuffer<File>(); |
duke@1 | 176 | for (String name : names) |
duke@1 | 177 | files.append(new File(nullCheck(name))); |
duke@1 | 178 | return getJavaFileObjectsFromFiles(files.toList()); |
duke@1 | 179 | } |
duke@1 | 180 | |
duke@1 | 181 | public Iterable<? extends JavaFileObject> getJavaFileObjects(String... names) { |
duke@1 | 182 | return getJavaFileObjectsFromStrings(Arrays.asList(nullCheck(names))); |
duke@1 | 183 | } |
duke@1 | 184 | |
duke@1 | 185 | private static boolean isValidName(String name) { |
duke@1 | 186 | // Arguably, isValidName should reject keywords (such as in SourceVersion.isName() ), |
duke@1 | 187 | // but the set of keywords depends on the source level, and we don't want |
duke@1 | 188 | // impls of JavaFileManager to have to be dependent on the source level. |
duke@1 | 189 | // Therefore we simply check that the argument is a sequence of identifiers |
duke@1 | 190 | // separated by ".". |
duke@1 | 191 | for (String s : name.split("\\.", -1)) { |
duke@1 | 192 | if (!SourceVersion.isIdentifier(s)) |
duke@1 | 193 | return false; |
duke@1 | 194 | } |
duke@1 | 195 | return true; |
duke@1 | 196 | } |
duke@1 | 197 | |
duke@1 | 198 | private static void validateClassName(String className) { |
duke@1 | 199 | if (!isValidName(className)) |
duke@1 | 200 | throw new IllegalArgumentException("Invalid class name: " + className); |
duke@1 | 201 | } |
duke@1 | 202 | |
duke@1 | 203 | private static void validatePackageName(String packageName) { |
duke@1 | 204 | if (packageName.length() > 0 && !isValidName(packageName)) |
duke@1 | 205 | throw new IllegalArgumentException("Invalid packageName name: " + packageName); |
duke@1 | 206 | } |
duke@1 | 207 | |
duke@1 | 208 | public static void testName(String name, |
duke@1 | 209 | boolean isValidPackageName, |
duke@1 | 210 | boolean isValidClassName) |
duke@1 | 211 | { |
duke@1 | 212 | try { |
duke@1 | 213 | validatePackageName(name); |
duke@1 | 214 | if (!isValidPackageName) |
duke@1 | 215 | throw new AssertionError("Invalid package name accepted: " + name); |
duke@1 | 216 | printAscii("Valid package name: \"%s\"", name); |
duke@1 | 217 | } catch (IllegalArgumentException e) { |
duke@1 | 218 | if (isValidPackageName) |
duke@1 | 219 | throw new AssertionError("Valid package name rejected: " + name); |
duke@1 | 220 | printAscii("Invalid package name: \"%s\"", name); |
duke@1 | 221 | } |
duke@1 | 222 | try { |
duke@1 | 223 | validateClassName(name); |
duke@1 | 224 | if (!isValidClassName) |
duke@1 | 225 | throw new AssertionError("Invalid class name accepted: " + name); |
duke@1 | 226 | printAscii("Valid class name: \"%s\"", name); |
duke@1 | 227 | } catch (IllegalArgumentException e) { |
duke@1 | 228 | if (isValidClassName) |
duke@1 | 229 | throw new AssertionError("Valid class name rejected: " + name); |
duke@1 | 230 | printAscii("Invalid class name: \"%s\"", name); |
duke@1 | 231 | } |
duke@1 | 232 | } |
jjg@103 | 233 | |
duke@1 | 234 | private static void printAscii(String format, Object... args) { |
duke@1 | 235 | String message; |
duke@1 | 236 | try { |
duke@1 | 237 | final String ascii = "US-ASCII"; |
duke@1 | 238 | message = new String(String.format(null, format, args).getBytes(ascii), ascii); |
duke@1 | 239 | } catch (java.io.UnsupportedEncodingException ex) { |
duke@1 | 240 | throw new AssertionError(ex); |
duke@1 | 241 | } |
duke@1 | 242 | System.out.println(message); |
duke@1 | 243 | } |
duke@1 | 244 | |
jjh@801 | 245 | |
duke@1 | 246 | /** |
jjh@801 | 247 | * Insert all files in subdirectory subdirectory of directory directory |
jjh@801 | 248 | * which match fileKinds into resultList |
duke@1 | 249 | */ |
duke@1 | 250 | private void listDirectory(File directory, |
jjg@103 | 251 | RelativeDirectory subdirectory, |
duke@1 | 252 | Set<JavaFileObject.Kind> fileKinds, |
duke@1 | 253 | boolean recurse, |
jjh@801 | 254 | ListBuffer<JavaFileObject> resultList) { |
jjh@801 | 255 | File d = subdirectory.getFile(directory); |
jjh@801 | 256 | if (!caseMapCheck(d, subdirectory)) |
jjh@801 | 257 | return; |
duke@1 | 258 | |
jjh@801 | 259 | File[] files = d.listFiles(); |
jjh@801 | 260 | if (files == null) |
jjh@801 | 261 | return; |
duke@1 | 262 | |
jjh@801 | 263 | if (sortFiles != null) |
jjh@801 | 264 | Arrays.sort(files, sortFiles); |
jjh@801 | 265 | |
jjh@801 | 266 | for (File f: files) { |
jjh@801 | 267 | String fname = f.getName(); |
jjh@801 | 268 | if (f.isDirectory()) { |
jjh@801 | 269 | if (recurse && SourceVersion.isIdentifier(fname)) { |
jjh@801 | 270 | listDirectory(directory, |
jjh@801 | 271 | new RelativeDirectory(subdirectory, fname), |
jjh@801 | 272 | fileKinds, |
jjh@801 | 273 | recurse, |
jjh@801 | 274 | resultList); |
duke@1 | 275 | } |
jjh@801 | 276 | } else { |
jjh@801 | 277 | if (isValidFile(fname, fileKinds)) { |
jjh@801 | 278 | JavaFileObject fe = |
jjh@801 | 279 | new RegularFileObject(this, fname, new File(d, fname)); |
jjh@801 | 280 | resultList.append(fe); |
duke@1 | 281 | } |
duke@1 | 282 | } |
duke@1 | 283 | } |
duke@1 | 284 | } |
duke@1 | 285 | |
jjh@801 | 286 | /** |
jjh@801 | 287 | * Insert all files in subdirectory subdirectory of archive archive |
jjh@801 | 288 | * which match fileKinds into resultList |
jjh@801 | 289 | */ |
jjh@801 | 290 | private void listArchive(Archive archive, |
jjh@801 | 291 | RelativeDirectory subdirectory, |
jjh@801 | 292 | Set<JavaFileObject.Kind> fileKinds, |
jjh@801 | 293 | boolean recurse, |
jjh@801 | 294 | ListBuffer<JavaFileObject> resultList) { |
jjh@801 | 295 | // Get the files directly in the subdir |
jjh@801 | 296 | List<String> files = archive.getFiles(subdirectory); |
jjh@801 | 297 | if (files != null) { |
jjh@801 | 298 | for (; !files.isEmpty(); files = files.tail) { |
jjh@801 | 299 | String file = files.head; |
jjh@801 | 300 | if (isValidFile(file, fileKinds)) { |
jjh@801 | 301 | resultList.append(archive.getFileObject(subdirectory, file)); |
jjh@801 | 302 | } |
jjh@801 | 303 | } |
jjh@801 | 304 | } |
jjh@801 | 305 | if (recurse) { |
jjh@801 | 306 | for (RelativeDirectory s: archive.getSubdirectories()) { |
jjh@801 | 307 | if (subdirectory.contains(s)) { |
jjh@801 | 308 | // Because the archive map is a flat list of directories, |
jjh@801 | 309 | // the enclosing loop will pick up all child subdirectories. |
jjh@801 | 310 | // Therefore, there is no need to recurse deeper. |
jjh@801 | 311 | listArchive(archive, s, fileKinds, false, resultList); |
jjh@801 | 312 | } |
jjh@801 | 313 | } |
jjh@801 | 314 | } |
jjh@801 | 315 | } |
jjh@801 | 316 | |
jjh@801 | 317 | /** |
jjh@801 | 318 | * container is a directory, a zip file, or a non-existant path. |
jjh@801 | 319 | * Insert all files in subdirectory subdirectory of container which |
jjh@801 | 320 | * match fileKinds into resultList |
jjh@801 | 321 | */ |
jjh@801 | 322 | private void listContainer(File container, |
jjh@801 | 323 | RelativeDirectory subdirectory, |
jjh@801 | 324 | Set<JavaFileObject.Kind> fileKinds, |
jjh@801 | 325 | boolean recurse, |
jjh@801 | 326 | ListBuffer<JavaFileObject> resultList) { |
jjh@801 | 327 | Archive archive = archives.get(container); |
jjh@801 | 328 | if (archive == null) { |
jjh@801 | 329 | // archives are not created for directories. |
jjh@801 | 330 | if (fsInfo.isDirectory(container)) { |
jjh@801 | 331 | listDirectory(container, |
jjh@801 | 332 | subdirectory, |
jjh@801 | 333 | fileKinds, |
jjh@801 | 334 | recurse, |
jjh@801 | 335 | resultList); |
jjh@801 | 336 | return; |
jjh@801 | 337 | } |
jjh@801 | 338 | |
jjh@801 | 339 | // Not a directory; either a file or non-existant, create the archive |
jjh@801 | 340 | try { |
jjh@801 | 341 | archive = openArchive(container); |
jjh@801 | 342 | } catch (IOException ex) { |
jjh@801 | 343 | log.error("error.reading.file", |
jjh@801 | 344 | container, getMessage(ex)); |
jjh@801 | 345 | return; |
jjh@801 | 346 | } |
jjh@801 | 347 | } |
jjh@801 | 348 | listArchive(archive, |
jjh@801 | 349 | subdirectory, |
jjh@801 | 350 | fileKinds, |
jjh@801 | 351 | recurse, |
jjh@801 | 352 | resultList); |
jjh@801 | 353 | } |
jjh@801 | 354 | |
duke@1 | 355 | private boolean isValidFile(String s, Set<JavaFileObject.Kind> fileKinds) { |
jjg@450 | 356 | JavaFileObject.Kind kind = getKind(s); |
duke@1 | 357 | return fileKinds.contains(kind); |
duke@1 | 358 | } |
duke@1 | 359 | |
duke@1 | 360 | private static final boolean fileSystemIsCaseSensitive = |
duke@1 | 361 | File.separatorChar == '/'; |
duke@1 | 362 | |
duke@1 | 363 | /** Hack to make Windows case sensitive. Test whether given path |
duke@1 | 364 | * ends in a string of characters with the same case as given name. |
duke@1 | 365 | * Ignore file separators in both path and name. |
duke@1 | 366 | */ |
jjg@103 | 367 | private boolean caseMapCheck(File f, RelativePath name) { |
duke@1 | 368 | if (fileSystemIsCaseSensitive) return true; |
duke@1 | 369 | // Note that getCanonicalPath() returns the case-sensitive |
duke@1 | 370 | // spelled file name. |
duke@1 | 371 | String path; |
duke@1 | 372 | try { |
duke@1 | 373 | path = f.getCanonicalPath(); |
duke@1 | 374 | } catch (IOException ex) { |
duke@1 | 375 | return false; |
duke@1 | 376 | } |
duke@1 | 377 | char[] pcs = path.toCharArray(); |
jjg@103 | 378 | char[] ncs = name.path.toCharArray(); |
duke@1 | 379 | int i = pcs.length - 1; |
duke@1 | 380 | int j = ncs.length - 1; |
duke@1 | 381 | while (i >= 0 && j >= 0) { |
duke@1 | 382 | while (i >= 0 && pcs[i] == File.separatorChar) i--; |
jjg@103 | 383 | while (j >= 0 && ncs[j] == '/') j--; |
duke@1 | 384 | if (i >= 0 && j >= 0) { |
duke@1 | 385 | if (pcs[i] != ncs[j]) return false; |
duke@1 | 386 | i--; |
duke@1 | 387 | j--; |
duke@1 | 388 | } |
duke@1 | 389 | } |
duke@1 | 390 | return j < 0; |
duke@1 | 391 | } |
duke@1 | 392 | |
duke@1 | 393 | /** |
duke@1 | 394 | * An archive provides a flat directory structure of a ZipFile by |
duke@1 | 395 | * mapping directory names to lists of files (basenames). |
duke@1 | 396 | */ |
duke@1 | 397 | public interface Archive { |
duke@1 | 398 | void close() throws IOException; |
duke@1 | 399 | |
jjg@103 | 400 | boolean contains(RelativePath name); |
duke@1 | 401 | |
jjg@103 | 402 | JavaFileObject getFileObject(RelativeDirectory subdirectory, String file); |
duke@1 | 403 | |
jjg@103 | 404 | List<String> getFiles(RelativeDirectory subdirectory); |
duke@1 | 405 | |
jjg@103 | 406 | Set<RelativeDirectory> getSubdirectories(); |
duke@1 | 407 | } |
duke@1 | 408 | |
duke@1 | 409 | public class MissingArchive implements Archive { |
duke@1 | 410 | final File zipFileName; |
duke@1 | 411 | public MissingArchive(File name) { |
duke@1 | 412 | zipFileName = name; |
duke@1 | 413 | } |
jjg@103 | 414 | public boolean contains(RelativePath name) { |
jjg@57 | 415 | return false; |
duke@1 | 416 | } |
duke@1 | 417 | |
duke@1 | 418 | public void close() { |
duke@1 | 419 | } |
duke@1 | 420 | |
jjg@103 | 421 | public JavaFileObject getFileObject(RelativeDirectory subdirectory, String file) { |
duke@1 | 422 | return null; |
duke@1 | 423 | } |
duke@1 | 424 | |
jjg@103 | 425 | public List<String> getFiles(RelativeDirectory subdirectory) { |
duke@1 | 426 | return List.nil(); |
duke@1 | 427 | } |
duke@1 | 428 | |
jjg@103 | 429 | public Set<RelativeDirectory> getSubdirectories() { |
duke@1 | 430 | return Collections.emptySet(); |
duke@1 | 431 | } |
jjg@103 | 432 | |
jjg@400 | 433 | @Override |
jjg@103 | 434 | public String toString() { |
jjg@103 | 435 | return "MissingArchive[" + zipFileName + "]"; |
jjg@103 | 436 | } |
duke@1 | 437 | } |
duke@1 | 438 | |
duke@1 | 439 | /** A directory of zip files already opened. |
duke@1 | 440 | */ |
duke@1 | 441 | Map<File, Archive> archives = new HashMap<File,Archive>(); |
duke@1 | 442 | |
jjg@103 | 443 | private static final String[] symbolFileLocation = { "lib", "ct.sym" }; |
jjg@103 | 444 | private static final RelativeDirectory symbolFilePrefix |
jjg@103 | 445 | = new RelativeDirectory("META-INF/sym/rt.jar/"); |
jjg@103 | 446 | |
ksrini@923 | 447 | /* |
ksrini@923 | 448 | * This method looks for a ZipFormatException and takes appropriate |
ksrini@923 | 449 | * evasive action. If there is a failure in the fast mode then we |
ksrini@923 | 450 | * fail over to the platform zip, and allow it to deal with a potentially |
ksrini@923 | 451 | * non compliant zip file. |
ksrini@923 | 452 | */ |
ksrini@923 | 453 | protected Archive openArchive(File zipFilename) throws IOException { |
ksrini@923 | 454 | try { |
ksrini@923 | 455 | return openArchive(zipFilename, contextUseOptimizedZip); |
ksrini@923 | 456 | } catch (IOException ioe) { |
ksrini@923 | 457 | if (ioe instanceof ZipFileIndex.ZipFormatException) { |
ksrini@923 | 458 | return openArchive(zipFilename, false); |
ksrini@923 | 459 | } else { |
ksrini@923 | 460 | throw ioe; |
ksrini@923 | 461 | } |
ksrini@923 | 462 | } |
ksrini@923 | 463 | } |
ksrini@923 | 464 | |
jjh@801 | 465 | /** Open a new zip file directory, and cache it. |
duke@1 | 466 | */ |
ksrini@923 | 467 | private Archive openArchive(File zipFileName, boolean useOptimizedZip) throws IOException { |
jjh@801 | 468 | File origZipFileName = zipFileName; |
jjg@1116 | 469 | if (!ignoreSymbolFile && locations.isDefaultBootClassPathRtJar(zipFileName)) { |
jjh@801 | 470 | File file = zipFileName.getParentFile().getParentFile(); // ${java.home} |
jjh@801 | 471 | if (new File(file.getName()).equals(new File("jre"))) |
jjh@801 | 472 | file = file.getParentFile(); |
jjh@801 | 473 | // file == ${jdk.home} |
jjh@801 | 474 | for (String name : symbolFileLocation) |
jjh@801 | 475 | file = new File(file, name); |
jjh@801 | 476 | // file == ${jdk.home}/lib/ct.sym |
jjh@801 | 477 | if (file.exists()) |
jjh@801 | 478 | zipFileName = file; |
jjh@801 | 479 | } |
jjh@801 | 480 | |
jjh@801 | 481 | Archive archive; |
jjh@801 | 482 | try { |
jjh@801 | 483 | |
jjh@801 | 484 | ZipFile zdir = null; |
jjh@801 | 485 | |
jjh@801 | 486 | boolean usePreindexedCache = false; |
jjh@801 | 487 | String preindexCacheLocation = null; |
jjh@801 | 488 | |
ksrini@923 | 489 | if (!useOptimizedZip) { |
jjh@801 | 490 | zdir = new ZipFile(zipFileName); |
ksrini@882 | 491 | } else { |
jjh@801 | 492 | usePreindexedCache = options.isSet("usezipindex"); |
jjh@801 | 493 | preindexCacheLocation = options.get("java.io.tmpdir"); |
jjh@801 | 494 | String optCacheLoc = options.get("cachezipindexdir"); |
duke@1 | 495 | |
jjh@801 | 496 | if (optCacheLoc != null && optCacheLoc.length() != 0) { |
jjh@801 | 497 | if (optCacheLoc.startsWith("\"")) { |
jjh@801 | 498 | if (optCacheLoc.endsWith("\"")) { |
jjh@801 | 499 | optCacheLoc = optCacheLoc.substring(1, optCacheLoc.length() - 1); |
jjh@801 | 500 | } |
jjh@801 | 501 | else { |
jjh@801 | 502 | optCacheLoc = optCacheLoc.substring(1); |
jjh@801 | 503 | } |
jjh@801 | 504 | } |
duke@1 | 505 | |
jjh@801 | 506 | File cacheDir = new File(optCacheLoc); |
jjh@801 | 507 | if (cacheDir.exists() && cacheDir.canWrite()) { |
jjh@801 | 508 | preindexCacheLocation = optCacheLoc; |
jjh@801 | 509 | if (!preindexCacheLocation.endsWith("/") && |
jjh@801 | 510 | !preindexCacheLocation.endsWith(File.separator)) { |
jjh@801 | 511 | preindexCacheLocation += File.separator; |
duke@1 | 512 | } |
duke@1 | 513 | } |
duke@1 | 514 | } |
jjh@801 | 515 | } |
duke@1 | 516 | |
jjh@801 | 517 | if (origZipFileName == zipFileName) { |
ksrini@923 | 518 | if (!useOptimizedZip) { |
jjh@801 | 519 | archive = new ZipArchive(this, zdir); |
jjh@801 | 520 | } else { |
jjh@801 | 521 | archive = new ZipFileIndexArchive(this, |
ksrini@923 | 522 | zipFileIndexCache.getZipFileIndex(zipFileName, |
jjg@103 | 523 | null, |
jjg@103 | 524 | usePreindexedCache, |
jjg@103 | 525 | preindexCacheLocation, |
jjg@700 | 526 | options.isSet("writezipindexfiles"))); |
jjh@801 | 527 | } |
jjh@801 | 528 | } else { |
ksrini@923 | 529 | if (!useOptimizedZip) { |
jjh@801 | 530 | archive = new SymbolArchive(this, origZipFileName, zdir, symbolFilePrefix); |
ksrini@923 | 531 | } else { |
jjh@801 | 532 | archive = new ZipFileIndexArchive(this, |
ksrini@923 | 533 | zipFileIndexCache.getZipFileIndex(zipFileName, |
jjg@103 | 534 | symbolFilePrefix, |
jjg@103 | 535 | usePreindexedCache, |
jjg@103 | 536 | preindexCacheLocation, |
jjg@700 | 537 | options.isSet("writezipindexfiles"))); |
duke@1 | 538 | } |
duke@1 | 539 | } |
jjh@801 | 540 | } catch (FileNotFoundException ex) { |
jjh@801 | 541 | archive = new MissingArchive(zipFileName); |
ksrini@923 | 542 | } catch (ZipFileIndex.ZipFormatException zfe) { |
ksrini@923 | 543 | throw zfe; |
jjh@801 | 544 | } catch (IOException ex) { |
jjh@801 | 545 | if (zipFileName.exists()) |
jjh@801 | 546 | log.error("error.reading.file", zipFileName, getMessage(ex)); |
jjh@801 | 547 | archive = new MissingArchive(zipFileName); |
jjh@801 | 548 | } |
duke@1 | 549 | |
jjh@801 | 550 | archives.put(origZipFileName, archive); |
duke@1 | 551 | return archive; |
duke@1 | 552 | } |
duke@1 | 553 | |
duke@1 | 554 | /** Flush any output resources. |
duke@1 | 555 | */ |
duke@1 | 556 | public void flush() { |
duke@1 | 557 | contentCache.clear(); |
duke@1 | 558 | } |
duke@1 | 559 | |
duke@1 | 560 | /** |
duke@1 | 561 | * Close the JavaFileManager, releasing resources. |
duke@1 | 562 | */ |
duke@1 | 563 | public void close() { |
duke@1 | 564 | for (Iterator<Archive> i = archives.values().iterator(); i.hasNext(); ) { |
duke@1 | 565 | Archive a = i.next(); |
duke@1 | 566 | i.remove(); |
duke@1 | 567 | try { |
duke@1 | 568 | a.close(); |
duke@1 | 569 | } catch (IOException e) { |
duke@1 | 570 | } |
duke@1 | 571 | } |
duke@1 | 572 | } |
duke@1 | 573 | |
duke@1 | 574 | private String defaultEncodingName; |
duke@1 | 575 | private String getDefaultEncodingName() { |
duke@1 | 576 | if (defaultEncodingName == null) { |
duke@1 | 577 | defaultEncodingName = |
duke@1 | 578 | new OutputStreamWriter(new ByteArrayOutputStream()).getEncoding(); |
duke@1 | 579 | } |
duke@1 | 580 | return defaultEncodingName; |
duke@1 | 581 | } |
duke@1 | 582 | |
duke@1 | 583 | public ClassLoader getClassLoader(Location location) { |
duke@1 | 584 | nullCheck(location); |
duke@1 | 585 | Iterable<? extends File> path = getLocation(location); |
duke@1 | 586 | if (path == null) |
duke@1 | 587 | return null; |
duke@1 | 588 | ListBuffer<URL> lb = new ListBuffer<URL>(); |
duke@1 | 589 | for (File f: path) { |
duke@1 | 590 | try { |
duke@1 | 591 | lb.append(f.toURI().toURL()); |
duke@1 | 592 | } catch (MalformedURLException e) { |
duke@1 | 593 | throw new AssertionError(e); |
duke@1 | 594 | } |
duke@1 | 595 | } |
jjg@372 | 596 | |
jjg@450 | 597 | return getClassLoader(lb.toArray(new URL[lb.size()])); |
duke@1 | 598 | } |
duke@1 | 599 | |
duke@1 | 600 | public Iterable<JavaFileObject> list(Location location, |
duke@1 | 601 | String packageName, |
duke@1 | 602 | Set<JavaFileObject.Kind> kinds, |
duke@1 | 603 | boolean recurse) |
duke@1 | 604 | throws IOException |
duke@1 | 605 | { |
duke@1 | 606 | // validatePackageName(packageName); |
duke@1 | 607 | nullCheck(packageName); |
duke@1 | 608 | nullCheck(kinds); |
duke@1 | 609 | |
duke@1 | 610 | Iterable<? extends File> path = getLocation(location); |
duke@1 | 611 | if (path == null) |
duke@1 | 612 | return List.nil(); |
jjg@103 | 613 | RelativeDirectory subdirectory = RelativeDirectory.forPackage(packageName); |
duke@1 | 614 | ListBuffer<JavaFileObject> results = new ListBuffer<JavaFileObject>(); |
duke@1 | 615 | |
duke@1 | 616 | for (File directory : path) |
jjh@801 | 617 | listContainer(directory, subdirectory, kinds, recurse, results); |
duke@1 | 618 | return results.toList(); |
duke@1 | 619 | } |
duke@1 | 620 | |
duke@1 | 621 | public String inferBinaryName(Location location, JavaFileObject file) { |
duke@1 | 622 | file.getClass(); // null check |
duke@1 | 623 | location.getClass(); // null check |
duke@1 | 624 | // Need to match the path semantics of list(location, ...) |
duke@1 | 625 | Iterable<? extends File> path = getLocation(location); |
duke@1 | 626 | if (path == null) { |
duke@1 | 627 | return null; |
duke@1 | 628 | } |
duke@1 | 629 | |
jjg@57 | 630 | if (file instanceof BaseFileObject) { |
jjg@57 | 631 | return ((BaseFileObject) file).inferBinaryName(path); |
duke@1 | 632 | } else |
duke@1 | 633 | throw new IllegalArgumentException(file.getClass().getName()); |
duke@1 | 634 | } |
duke@1 | 635 | |
duke@1 | 636 | public boolean isSameFile(FileObject a, FileObject b) { |
duke@1 | 637 | nullCheck(a); |
duke@1 | 638 | nullCheck(b); |
duke@1 | 639 | if (!(a instanceof BaseFileObject)) |
duke@1 | 640 | throw new IllegalArgumentException("Not supported: " + a); |
duke@1 | 641 | if (!(b instanceof BaseFileObject)) |
duke@1 | 642 | throw new IllegalArgumentException("Not supported: " + b); |
duke@1 | 643 | return a.equals(b); |
duke@1 | 644 | } |
duke@1 | 645 | |
duke@1 | 646 | public boolean hasLocation(Location location) { |
duke@1 | 647 | return getLocation(location) != null; |
duke@1 | 648 | } |
duke@1 | 649 | |
duke@1 | 650 | public JavaFileObject getJavaFileForInput(Location location, |
duke@1 | 651 | String className, |
duke@1 | 652 | JavaFileObject.Kind kind) |
duke@1 | 653 | throws IOException |
duke@1 | 654 | { |
duke@1 | 655 | nullCheck(location); |
duke@1 | 656 | // validateClassName(className); |
duke@1 | 657 | nullCheck(className); |
duke@1 | 658 | nullCheck(kind); |
duke@1 | 659 | if (!sourceOrClass.contains(kind)) |
jjg@698 | 660 | throw new IllegalArgumentException("Invalid kind: " + kind); |
jjg@103 | 661 | return getFileForInput(location, RelativeFile.forClass(className, kind)); |
duke@1 | 662 | } |
duke@1 | 663 | |
duke@1 | 664 | public FileObject getFileForInput(Location location, |
duke@1 | 665 | String packageName, |
duke@1 | 666 | String relativeName) |
duke@1 | 667 | throws IOException |
duke@1 | 668 | { |
duke@1 | 669 | nullCheck(location); |
duke@1 | 670 | // validatePackageName(packageName); |
duke@1 | 671 | nullCheck(packageName); |
jjg@400 | 672 | if (!isRelativeUri(relativeName)) |
duke@1 | 673 | throw new IllegalArgumentException("Invalid relative name: " + relativeName); |
jjg@103 | 674 | RelativeFile name = packageName.length() == 0 |
jjg@103 | 675 | ? new RelativeFile(relativeName) |
jjg@103 | 676 | : new RelativeFile(RelativeDirectory.forPackage(packageName), relativeName); |
duke@1 | 677 | return getFileForInput(location, name); |
duke@1 | 678 | } |
duke@1 | 679 | |
jjg@103 | 680 | private JavaFileObject getFileForInput(Location location, RelativeFile name) throws IOException { |
duke@1 | 681 | Iterable<? extends File> path = getLocation(location); |
duke@1 | 682 | if (path == null) |
duke@1 | 683 | return null; |
duke@1 | 684 | |
duke@1 | 685 | for (File dir: path) { |
jjh@801 | 686 | Archive a = archives.get(dir); |
jjh@801 | 687 | if (a == null) { |
jjh@801 | 688 | if (fsInfo.isDirectory(dir)) { |
jjh@801 | 689 | File f = name.getFile(dir); |
jjh@801 | 690 | if (f.exists()) |
jjh@801 | 691 | return new RegularFileObject(this, f); |
jjh@801 | 692 | continue; |
duke@1 | 693 | } |
jjh@801 | 694 | // Not a directory, create the archive |
jjh@801 | 695 | a = openArchive(dir); |
jjh@801 | 696 | } |
jjh@801 | 697 | // Process the archive |
jjh@801 | 698 | if (a.contains(name)) { |
jjh@801 | 699 | return a.getFileObject(name.dirname(), name.basename()); |
duke@1 | 700 | } |
duke@1 | 701 | } |
duke@1 | 702 | return null; |
duke@1 | 703 | } |
duke@1 | 704 | |
duke@1 | 705 | public JavaFileObject getJavaFileForOutput(Location location, |
duke@1 | 706 | String className, |
duke@1 | 707 | JavaFileObject.Kind kind, |
duke@1 | 708 | FileObject sibling) |
duke@1 | 709 | throws IOException |
duke@1 | 710 | { |
duke@1 | 711 | nullCheck(location); |
duke@1 | 712 | // validateClassName(className); |
duke@1 | 713 | nullCheck(className); |
duke@1 | 714 | nullCheck(kind); |
duke@1 | 715 | if (!sourceOrClass.contains(kind)) |
jjg@698 | 716 | throw new IllegalArgumentException("Invalid kind: " + kind); |
jjg@103 | 717 | return getFileForOutput(location, RelativeFile.forClass(className, kind), sibling); |
duke@1 | 718 | } |
duke@1 | 719 | |
duke@1 | 720 | public FileObject getFileForOutput(Location location, |
duke@1 | 721 | String packageName, |
duke@1 | 722 | String relativeName, |
duke@1 | 723 | FileObject sibling) |
duke@1 | 724 | throws IOException |
duke@1 | 725 | { |
duke@1 | 726 | nullCheck(location); |
duke@1 | 727 | // validatePackageName(packageName); |
duke@1 | 728 | nullCheck(packageName); |
jjg@400 | 729 | if (!isRelativeUri(relativeName)) |
jjg@698 | 730 | throw new IllegalArgumentException("Invalid relative name: " + relativeName); |
jjg@103 | 731 | RelativeFile name = packageName.length() == 0 |
jjg@103 | 732 | ? new RelativeFile(relativeName) |
jjg@103 | 733 | : new RelativeFile(RelativeDirectory.forPackage(packageName), relativeName); |
duke@1 | 734 | return getFileForOutput(location, name, sibling); |
duke@1 | 735 | } |
duke@1 | 736 | |
duke@1 | 737 | private JavaFileObject getFileForOutput(Location location, |
jjg@103 | 738 | RelativeFile fileName, |
duke@1 | 739 | FileObject sibling) |
duke@1 | 740 | throws IOException |
duke@1 | 741 | { |
duke@1 | 742 | File dir; |
duke@1 | 743 | if (location == CLASS_OUTPUT) { |
duke@1 | 744 | if (getClassOutDir() != null) { |
duke@1 | 745 | dir = getClassOutDir(); |
duke@1 | 746 | } else { |
duke@1 | 747 | File siblingDir = null; |
duke@1 | 748 | if (sibling != null && sibling instanceof RegularFileObject) { |
jjg@424 | 749 | siblingDir = ((RegularFileObject)sibling).file.getParentFile(); |
duke@1 | 750 | } |
jjg@103 | 751 | return new RegularFileObject(this, new File(siblingDir, fileName.basename())); |
duke@1 | 752 | } |
duke@1 | 753 | } else if (location == SOURCE_OUTPUT) { |
duke@1 | 754 | dir = (getSourceOutDir() != null ? getSourceOutDir() : getClassOutDir()); |
duke@1 | 755 | } else { |
jjg@1116 | 756 | Iterable<? extends File> path = locations.getLocation(location); |
duke@1 | 757 | dir = null; |
duke@1 | 758 | for (File f: path) { |
duke@1 | 759 | dir = f; |
duke@1 | 760 | break; |
duke@1 | 761 | } |
duke@1 | 762 | } |
duke@1 | 763 | |
jjg@103 | 764 | File file = fileName.getFile(dir); // null-safe |
jjg@57 | 765 | return new RegularFileObject(this, file); |
duke@1 | 766 | |
duke@1 | 767 | } |
duke@1 | 768 | |
duke@1 | 769 | public Iterable<? extends JavaFileObject> getJavaFileObjectsFromFiles( |
duke@1 | 770 | Iterable<? extends File> files) |
duke@1 | 771 | { |
duke@1 | 772 | ArrayList<RegularFileObject> result; |
mcimadamore@184 | 773 | if (files instanceof Collection<?>) |
mcimadamore@184 | 774 | result = new ArrayList<RegularFileObject>(((Collection<?>)files).size()); |
duke@1 | 775 | else |
duke@1 | 776 | result = new ArrayList<RegularFileObject>(); |
duke@1 | 777 | for (File f: files) |
jjg@57 | 778 | result.add(new RegularFileObject(this, nullCheck(f))); |
duke@1 | 779 | return result; |
duke@1 | 780 | } |
duke@1 | 781 | |
duke@1 | 782 | public Iterable<? extends JavaFileObject> getJavaFileObjects(File... files) { |
duke@1 | 783 | return getJavaFileObjectsFromFiles(Arrays.asList(nullCheck(files))); |
duke@1 | 784 | } |
duke@1 | 785 | |
duke@1 | 786 | public void setLocation(Location location, |
duke@1 | 787 | Iterable<? extends File> path) |
duke@1 | 788 | throws IOException |
duke@1 | 789 | { |
duke@1 | 790 | nullCheck(location); |
jjg@1116 | 791 | locations.setLocation(location, path); |
duke@1 | 792 | } |
duke@1 | 793 | |
duke@1 | 794 | public Iterable<? extends File> getLocation(Location location) { |
duke@1 | 795 | nullCheck(location); |
jjg@1116 | 796 | return locations.getLocation(location); |
duke@1 | 797 | } |
duke@1 | 798 | |
duke@1 | 799 | private File getClassOutDir() { |
jjg@1116 | 800 | return locations.getOutputLocation(CLASS_OUTPUT); |
duke@1 | 801 | } |
duke@1 | 802 | |
duke@1 | 803 | private File getSourceOutDir() { |
jjg@1116 | 804 | return locations.getOutputLocation(SOURCE_OUTPUT); |
duke@1 | 805 | } |
duke@1 | 806 | |
duke@1 | 807 | /** |
duke@1 | 808 | * Enforces the specification of a "relative" URI as used in |
duke@1 | 809 | * {@linkplain #getFileForInput(Location,String,URI) |
duke@1 | 810 | * getFileForInput}. This method must follow the rules defined in |
duke@1 | 811 | * that method, do not make any changes without consulting the |
duke@1 | 812 | * specification. |
duke@1 | 813 | */ |
duke@1 | 814 | protected static boolean isRelativeUri(URI uri) { |
duke@1 | 815 | if (uri.isAbsolute()) |
duke@1 | 816 | return false; |
duke@1 | 817 | String path = uri.normalize().getPath(); |
duke@1 | 818 | if (path.length() == 0 /* isEmpty() is mustang API */) |
duke@1 | 819 | return false; |
jjg@698 | 820 | if (!path.equals(uri.getPath())) // implicitly checks for embedded . and .. |
jjg@698 | 821 | return false; |
jjg@802 | 822 | if (path.startsWith("/") || path.startsWith("./") || path.startsWith("../")) |
jjg@802 | 823 | return false; |
jjg@802 | 824 | return true; |
duke@1 | 825 | } |
duke@1 | 826 | |
jjg@400 | 827 | // Convenience method |
jjg@400 | 828 | protected static boolean isRelativeUri(String u) { |
jjg@400 | 829 | try { |
jjg@400 | 830 | return isRelativeUri(new URI(u)); |
jjg@400 | 831 | } catch (URISyntaxException e) { |
jjg@400 | 832 | return false; |
jjg@400 | 833 | } |
jjg@400 | 834 | } |
jjg@400 | 835 | |
duke@1 | 836 | /** |
duke@1 | 837 | * Converts a relative file name to a relative URI. This is |
duke@1 | 838 | * different from File.toURI as this method does not canonicalize |
duke@1 | 839 | * the file before creating the URI. Furthermore, no schema is |
duke@1 | 840 | * used. |
duke@1 | 841 | * @param file a relative file name |
duke@1 | 842 | * @return a relative URI |
duke@1 | 843 | * @throws IllegalArgumentException if the file name is not |
duke@1 | 844 | * relative according to the definition given in {@link |
duke@1 | 845 | * javax.tools.JavaFileManager#getFileForInput} |
duke@1 | 846 | */ |
duke@1 | 847 | public static String getRelativeName(File file) { |
duke@1 | 848 | if (!file.isAbsolute()) { |
duke@1 | 849 | String result = file.getPath().replace(File.separatorChar, '/'); |
jjg@400 | 850 | if (isRelativeUri(result)) |
duke@1 | 851 | return result; |
duke@1 | 852 | } |
duke@1 | 853 | throw new IllegalArgumentException("Invalid relative path: " + file); |
duke@1 | 854 | } |
jjg@510 | 855 | |
jjg@510 | 856 | /** |
jjg@510 | 857 | * Get a detail message from an IOException. |
jjg@510 | 858 | * Most, but not all, instances of IOException provide a non-null result |
jjg@510 | 859 | * for getLocalizedMessage(). But some instances return null: in these |
jjg@510 | 860 | * cases, fallover to getMessage(), and if even that is null, return the |
jjg@510 | 861 | * name of the exception itself. |
jjg@510 | 862 | * @param e an IOException |
jjg@510 | 863 | * @return a string to include in a compiler diagnostic |
jjg@510 | 864 | */ |
jjg@510 | 865 | public static String getMessage(IOException e) { |
jjg@510 | 866 | String s = e.getLocalizedMessage(); |
jjg@510 | 867 | if (s != null) |
jjg@510 | 868 | return s; |
jjg@510 | 869 | s = e.getMessage(); |
jjg@510 | 870 | if (s != null) |
jjg@510 | 871 | return s; |
jjg@510 | 872 | return e.toString(); |
jjg@510 | 873 | } |
duke@1 | 874 | } |