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