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