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

Mon, 07 Mar 2011 17:39:42 -0800

author
ksrini
date
Mon, 07 Mar 2011 17:39:42 -0800
changeset 923
6970d9fb8e02
parent 893
8f0dcb9499db
child 1111
d2cbb77469ed
permissions
-rw-r--r--

7021927: javac: regression in performance
Reviewed-by: jjg

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

mercurial