Wed, 25 Mar 2009 10:29:28 +0000
6400189: raw types and inference
Summary: Fixed resolution problem with raw overriding (CCC)
Reviewed-by: jjg
duke@1 | 1 | /* |
xdono@54 | 2 | * Copyright 2005-2008 Sun Microsystems, Inc. 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 |
duke@1 | 7 | * published by the Free Software Foundation. Sun designates this |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
duke@1 | 9 | * by Sun 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 | * |
duke@1 | 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
duke@1 | 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
duke@1 | 23 | * have any 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.FileInputStream; |
duke@1 | 31 | import java.io.FileNotFoundException; |
duke@1 | 32 | import java.io.IOException; |
duke@1 | 33 | import java.io.InputStream; |
duke@1 | 34 | import java.io.OutputStreamWriter; |
duke@1 | 35 | import java.lang.ref.SoftReference; |
duke@1 | 36 | import java.net.MalformedURLException; |
duke@1 | 37 | import java.net.URI; |
duke@1 | 38 | import java.net.URL; |
duke@1 | 39 | import java.net.URLClassLoader; |
duke@1 | 40 | import java.nio.ByteBuffer; |
duke@1 | 41 | import java.nio.CharBuffer; |
duke@1 | 42 | import java.nio.channels.FileChannel; |
duke@1 | 43 | import java.nio.charset.Charset; |
duke@1 | 44 | import java.nio.charset.CharsetDecoder; |
duke@1 | 45 | import java.nio.charset.CoderResult; |
duke@1 | 46 | import java.nio.charset.CodingErrorAction; |
duke@1 | 47 | import java.nio.charset.IllegalCharsetNameException; |
duke@1 | 48 | import java.nio.charset.UnsupportedCharsetException; |
duke@1 | 49 | import java.util.ArrayList; |
duke@1 | 50 | import java.util.Arrays; |
duke@1 | 51 | import java.util.Collection; |
duke@1 | 52 | import java.util.Collections; |
duke@1 | 53 | import java.util.EnumSet; |
duke@1 | 54 | import java.util.HashMap; |
duke@1 | 55 | import java.util.Iterator; |
duke@1 | 56 | import java.util.Map; |
duke@1 | 57 | import java.util.Set; |
duke@1 | 58 | import java.util.zip.ZipFile; |
duke@1 | 59 | |
duke@1 | 60 | import javax.lang.model.SourceVersion; |
duke@1 | 61 | import javax.tools.FileObject; |
duke@1 | 62 | import javax.tools.JavaFileManager; |
duke@1 | 63 | import javax.tools.JavaFileObject; |
jjg@50 | 64 | import javax.tools.StandardJavaFileManager; |
duke@1 | 65 | |
duke@1 | 66 | import com.sun.tools.javac.code.Source; |
jjg@103 | 67 | import com.sun.tools.javac.file.RelativePath.RelativeFile; |
jjg@103 | 68 | import com.sun.tools.javac.file.RelativePath.RelativeDirectory; |
jjg@50 | 69 | import com.sun.tools.javac.main.JavacOption; |
jjg@50 | 70 | import com.sun.tools.javac.main.OptionName; |
jjg@50 | 71 | import com.sun.tools.javac.main.RecognizedOptions; |
jjg@50 | 72 | import com.sun.tools.javac.util.Context; |
duke@1 | 73 | import com.sun.tools.javac.util.JCDiagnostic.SimpleDiagnosticPosition; |
jjg@50 | 74 | import com.sun.tools.javac.util.List; |
jjg@50 | 75 | import com.sun.tools.javac.util.ListBuffer; |
jjg@50 | 76 | import com.sun.tools.javac.util.Log; |
jjg@50 | 77 | import com.sun.tools.javac.util.Options; |
duke@1 | 78 | |
jjg@103 | 79 | import static javax.tools.StandardLocation.*; |
duke@1 | 80 | import static com.sun.tools.javac.main.OptionName.*; |
duke@1 | 81 | |
duke@1 | 82 | /** |
duke@1 | 83 | * This class provides access to the source, class and other files |
duke@1 | 84 | * used by the compiler and related tools. |
duke@1 | 85 | */ |
duke@1 | 86 | public class JavacFileManager implements StandardJavaFileManager { |
duke@1 | 87 | |
duke@1 | 88 | boolean useZipFileIndex; |
duke@1 | 89 | |
duke@1 | 90 | public static char[] toArray(CharBuffer buffer) { |
duke@1 | 91 | if (buffer.hasArray()) |
duke@1 | 92 | return ((CharBuffer)buffer.compact().flip()).array(); |
duke@1 | 93 | else |
duke@1 | 94 | return buffer.toString().toCharArray(); |
duke@1 | 95 | } |
duke@1 | 96 | |
duke@1 | 97 | /** |
duke@1 | 98 | * The log to be used for error reporting. |
duke@1 | 99 | */ |
duke@1 | 100 | protected Log log; |
duke@1 | 101 | |
duke@1 | 102 | /** Encapsulates knowledge of paths |
duke@1 | 103 | */ |
duke@1 | 104 | private Paths paths; |
duke@1 | 105 | |
duke@1 | 106 | private Options options; |
duke@1 | 107 | |
jjg@106 | 108 | private FSInfo fsInfo; |
jjg@106 | 109 | |
duke@1 | 110 | private final File uninited = new File("U N I N I T E D"); |
duke@1 | 111 | |
duke@1 | 112 | private final Set<JavaFileObject.Kind> sourceOrClass = |
duke@1 | 113 | EnumSet.of(JavaFileObject.Kind.SOURCE, JavaFileObject.Kind.CLASS); |
duke@1 | 114 | |
duke@1 | 115 | /** The standard output directory, primarily used for classes. |
duke@1 | 116 | * Initialized by the "-d" option. |
duke@1 | 117 | * If classOutDir = null, files are written into same directory as the sources |
duke@1 | 118 | * they were generated from. |
duke@1 | 119 | */ |
duke@1 | 120 | private File classOutDir = uninited; |
duke@1 | 121 | |
duke@1 | 122 | /** The output directory, used when generating sources while processing annotations. |
duke@1 | 123 | * Initialized by the "-s" option. |
duke@1 | 124 | */ |
duke@1 | 125 | private File sourceOutDir = uninited; |
duke@1 | 126 | |
duke@1 | 127 | protected boolean mmappedIO; |
duke@1 | 128 | protected boolean ignoreSymbolFile; |
duke@1 | 129 | |
duke@1 | 130 | /** |
duke@1 | 131 | * User provided charset (through javax.tools). |
duke@1 | 132 | */ |
duke@1 | 133 | protected Charset charset; |
duke@1 | 134 | |
duke@1 | 135 | /** |
duke@1 | 136 | * Register a Context.Factory to create a JavacFileManager. |
duke@1 | 137 | */ |
duke@1 | 138 | public static void preRegister(final Context context) { |
duke@1 | 139 | context.put(JavaFileManager.class, new Context.Factory<JavaFileManager>() { |
duke@1 | 140 | public JavaFileManager make() { |
duke@1 | 141 | return new JavacFileManager(context, true, null); |
duke@1 | 142 | } |
duke@1 | 143 | }); |
duke@1 | 144 | } |
duke@1 | 145 | |
duke@1 | 146 | /** |
duke@1 | 147 | * Create a JavacFileManager using a given context, optionally registering |
duke@1 | 148 | * it as the JavaFileManager for that context. |
duke@1 | 149 | */ |
duke@1 | 150 | public JavacFileManager(Context context, boolean register, Charset charset) { |
duke@1 | 151 | if (register) |
duke@1 | 152 | context.put(JavaFileManager.class, this); |
duke@1 | 153 | byteBufferCache = new ByteBufferCache(); |
duke@1 | 154 | this.charset = charset; |
duke@1 | 155 | setContext(context); |
duke@1 | 156 | } |
duke@1 | 157 | |
duke@1 | 158 | /** |
duke@1 | 159 | * Set the context for JavacFileManager. |
duke@1 | 160 | */ |
duke@1 | 161 | public void setContext(Context context) { |
duke@1 | 162 | log = Log.instance(context); |
duke@1 | 163 | if (paths == null) { |
duke@1 | 164 | paths = Paths.instance(context); |
duke@1 | 165 | } else { |
duke@1 | 166 | // Reuse the Paths object as it stores the locations that |
duke@1 | 167 | // have been set with setLocation, etc. |
duke@1 | 168 | paths.setContext(context); |
duke@1 | 169 | } |
duke@1 | 170 | |
duke@1 | 171 | options = Options.instance(context); |
jjg@106 | 172 | fsInfo = FSInfo.instance(context); |
duke@1 | 173 | |
duke@1 | 174 | useZipFileIndex = System.getProperty("useJavaUtilZip") == null;// TODO: options.get("useJavaUtilZip") == null; |
duke@1 | 175 | |
duke@1 | 176 | mmappedIO = options.get("mmappedIO") != null; |
duke@1 | 177 | ignoreSymbolFile = options.get("ignore.symbol.file") != null; |
duke@1 | 178 | } |
duke@1 | 179 | |
duke@1 | 180 | public JavaFileObject getFileForInput(String name) { |
duke@1 | 181 | return getRegularFile(new File(name)); |
duke@1 | 182 | } |
duke@1 | 183 | |
duke@1 | 184 | public JavaFileObject getRegularFile(File file) { |
jjg@57 | 185 | return new RegularFileObject(this, file); |
duke@1 | 186 | } |
duke@1 | 187 | |
duke@1 | 188 | public JavaFileObject getFileForOutput(String classname, |
duke@1 | 189 | JavaFileObject.Kind kind, |
duke@1 | 190 | JavaFileObject sibling) |
duke@1 | 191 | throws IOException |
duke@1 | 192 | { |
duke@1 | 193 | return getJavaFileForOutput(CLASS_OUTPUT, classname, kind, sibling); |
duke@1 | 194 | } |
duke@1 | 195 | |
duke@1 | 196 | public Iterable<? extends JavaFileObject> getJavaFileObjectsFromStrings(Iterable<String> names) { |
duke@1 | 197 | ListBuffer<File> files = new ListBuffer<File>(); |
duke@1 | 198 | for (String name : names) |
duke@1 | 199 | files.append(new File(nullCheck(name))); |
duke@1 | 200 | return getJavaFileObjectsFromFiles(files.toList()); |
duke@1 | 201 | } |
duke@1 | 202 | |
duke@1 | 203 | public Iterable<? extends JavaFileObject> getJavaFileObjects(String... names) { |
duke@1 | 204 | return getJavaFileObjectsFromStrings(Arrays.asList(nullCheck(names))); |
duke@1 | 205 | } |
duke@1 | 206 | |
duke@1 | 207 | protected JavaFileObject.Kind getKind(String extension) { |
duke@1 | 208 | if (extension.equals(JavaFileObject.Kind.CLASS.extension)) |
duke@1 | 209 | return JavaFileObject.Kind.CLASS; |
duke@1 | 210 | else if (extension.equals(JavaFileObject.Kind.SOURCE.extension)) |
duke@1 | 211 | return JavaFileObject.Kind.SOURCE; |
duke@1 | 212 | else if (extension.equals(JavaFileObject.Kind.HTML.extension)) |
duke@1 | 213 | return JavaFileObject.Kind.HTML; |
duke@1 | 214 | else |
duke@1 | 215 | return JavaFileObject.Kind.OTHER; |
duke@1 | 216 | } |
duke@1 | 217 | |
duke@1 | 218 | private static boolean isValidName(String name) { |
duke@1 | 219 | // Arguably, isValidName should reject keywords (such as in SourceVersion.isName() ), |
duke@1 | 220 | // but the set of keywords depends on the source level, and we don't want |
duke@1 | 221 | // impls of JavaFileManager to have to be dependent on the source level. |
duke@1 | 222 | // Therefore we simply check that the argument is a sequence of identifiers |
duke@1 | 223 | // separated by ".". |
duke@1 | 224 | for (String s : name.split("\\.", -1)) { |
duke@1 | 225 | if (!SourceVersion.isIdentifier(s)) |
duke@1 | 226 | return false; |
duke@1 | 227 | } |
duke@1 | 228 | return true; |
duke@1 | 229 | } |
duke@1 | 230 | |
duke@1 | 231 | private static void validateClassName(String className) { |
duke@1 | 232 | if (!isValidName(className)) |
duke@1 | 233 | throw new IllegalArgumentException("Invalid class name: " + className); |
duke@1 | 234 | } |
duke@1 | 235 | |
duke@1 | 236 | private static void validatePackageName(String packageName) { |
duke@1 | 237 | if (packageName.length() > 0 && !isValidName(packageName)) |
duke@1 | 238 | throw new IllegalArgumentException("Invalid packageName name: " + packageName); |
duke@1 | 239 | } |
duke@1 | 240 | |
duke@1 | 241 | public static void testName(String name, |
duke@1 | 242 | boolean isValidPackageName, |
duke@1 | 243 | boolean isValidClassName) |
duke@1 | 244 | { |
duke@1 | 245 | try { |
duke@1 | 246 | validatePackageName(name); |
duke@1 | 247 | if (!isValidPackageName) |
duke@1 | 248 | throw new AssertionError("Invalid package name accepted: " + name); |
duke@1 | 249 | printAscii("Valid package name: \"%s\"", name); |
duke@1 | 250 | } catch (IllegalArgumentException e) { |
duke@1 | 251 | if (isValidPackageName) |
duke@1 | 252 | throw new AssertionError("Valid package name rejected: " + name); |
duke@1 | 253 | printAscii("Invalid package name: \"%s\"", name); |
duke@1 | 254 | } |
duke@1 | 255 | try { |
duke@1 | 256 | validateClassName(name); |
duke@1 | 257 | if (!isValidClassName) |
duke@1 | 258 | throw new AssertionError("Invalid class name accepted: " + name); |
duke@1 | 259 | printAscii("Valid class name: \"%s\"", name); |
duke@1 | 260 | } catch (IllegalArgumentException e) { |
duke@1 | 261 | if (isValidClassName) |
duke@1 | 262 | throw new AssertionError("Valid class name rejected: " + name); |
duke@1 | 263 | printAscii("Invalid class name: \"%s\"", name); |
duke@1 | 264 | } |
duke@1 | 265 | } |
jjg@103 | 266 | |
duke@1 | 267 | private static void printAscii(String format, Object... args) { |
duke@1 | 268 | String message; |
duke@1 | 269 | try { |
duke@1 | 270 | final String ascii = "US-ASCII"; |
duke@1 | 271 | message = new String(String.format(null, format, args).getBytes(ascii), ascii); |
duke@1 | 272 | } catch (java.io.UnsupportedEncodingException ex) { |
duke@1 | 273 | throw new AssertionError(ex); |
duke@1 | 274 | } |
duke@1 | 275 | System.out.println(message); |
duke@1 | 276 | } |
duke@1 | 277 | |
duke@1 | 278 | /** |
duke@1 | 279 | * Insert all files in subdirectory `subdirectory' of `directory' which end |
duke@1 | 280 | * in one of the extensions in `extensions' into packageSym. |
duke@1 | 281 | */ |
duke@1 | 282 | private void listDirectory(File directory, |
jjg@103 | 283 | RelativeDirectory subdirectory, |
duke@1 | 284 | Set<JavaFileObject.Kind> fileKinds, |
duke@1 | 285 | boolean recurse, |
duke@1 | 286 | ListBuffer<JavaFileObject> l) { |
duke@1 | 287 | Archive archive = archives.get(directory); |
duke@1 | 288 | |
jjg@106 | 289 | boolean isFile = fsInfo.isFile(directory); |
duke@1 | 290 | |
duke@1 | 291 | if (archive != null || isFile) { |
duke@1 | 292 | if (archive == null) { |
duke@1 | 293 | try { |
duke@1 | 294 | archive = openArchive(directory); |
duke@1 | 295 | } catch (IOException ex) { |
duke@1 | 296 | log.error("error.reading.file", |
duke@1 | 297 | directory, ex.getLocalizedMessage()); |
duke@1 | 298 | return; |
duke@1 | 299 | } |
duke@1 | 300 | } |
duke@1 | 301 | |
duke@1 | 302 | List<String> files = archive.getFiles(subdirectory); |
duke@1 | 303 | if (files != null) { |
duke@1 | 304 | for (String file; !files.isEmpty(); files = files.tail) { |
duke@1 | 305 | file = files.head; |
duke@1 | 306 | if (isValidFile(file, fileKinds)) { |
duke@1 | 307 | l.append(archive.getFileObject(subdirectory, file)); |
duke@1 | 308 | } |
duke@1 | 309 | } |
duke@1 | 310 | } |
duke@1 | 311 | if (recurse) { |
jjg@103 | 312 | for (RelativeDirectory s: archive.getSubdirectories()) { |
jjg@103 | 313 | if (subdirectory.contains(s)) { |
duke@1 | 314 | // Because the archive map is a flat list of directories, |
duke@1 | 315 | // the enclosing loop will pick up all child subdirectories. |
duke@1 | 316 | // Therefore, there is no need to recurse deeper. |
duke@1 | 317 | listDirectory(directory, s, fileKinds, false, l); |
duke@1 | 318 | } |
duke@1 | 319 | } |
duke@1 | 320 | } |
duke@1 | 321 | } else { |
jjg@103 | 322 | File d = subdirectory.getFile(directory); |
duke@1 | 323 | if (!caseMapCheck(d, subdirectory)) |
duke@1 | 324 | return; |
duke@1 | 325 | |
duke@1 | 326 | File[] files = d.listFiles(); |
duke@1 | 327 | if (files == null) |
duke@1 | 328 | return; |
duke@1 | 329 | |
duke@1 | 330 | for (File f: files) { |
duke@1 | 331 | String fname = f.getName(); |
duke@1 | 332 | if (f.isDirectory()) { |
duke@1 | 333 | if (recurse && SourceVersion.isIdentifier(fname)) { |
duke@1 | 334 | listDirectory(directory, |
jjg@103 | 335 | new RelativeDirectory(subdirectory, fname), |
duke@1 | 336 | fileKinds, |
duke@1 | 337 | recurse, |
duke@1 | 338 | l); |
duke@1 | 339 | } |
duke@1 | 340 | } else { |
duke@1 | 341 | if (isValidFile(fname, fileKinds)) { |
duke@1 | 342 | JavaFileObject fe = |
jjg@57 | 343 | new RegularFileObject(this, fname, new File(d, fname)); |
duke@1 | 344 | l.append(fe); |
duke@1 | 345 | } |
duke@1 | 346 | } |
duke@1 | 347 | } |
duke@1 | 348 | } |
duke@1 | 349 | } |
duke@1 | 350 | |
duke@1 | 351 | private boolean isValidFile(String s, Set<JavaFileObject.Kind> fileKinds) { |
duke@1 | 352 | int lastDot = s.lastIndexOf("."); |
duke@1 | 353 | String extn = (lastDot == -1 ? s : s.substring(lastDot)); |
duke@1 | 354 | JavaFileObject.Kind kind = getKind(extn); |
duke@1 | 355 | return fileKinds.contains(kind); |
duke@1 | 356 | } |
duke@1 | 357 | |
duke@1 | 358 | private static final boolean fileSystemIsCaseSensitive = |
duke@1 | 359 | File.separatorChar == '/'; |
duke@1 | 360 | |
duke@1 | 361 | /** Hack to make Windows case sensitive. Test whether given path |
duke@1 | 362 | * ends in a string of characters with the same case as given name. |
duke@1 | 363 | * Ignore file separators in both path and name. |
duke@1 | 364 | */ |
jjg@103 | 365 | private boolean caseMapCheck(File f, RelativePath name) { |
duke@1 | 366 | if (fileSystemIsCaseSensitive) return true; |
duke@1 | 367 | // Note that getCanonicalPath() returns the case-sensitive |
duke@1 | 368 | // spelled file name. |
duke@1 | 369 | String path; |
duke@1 | 370 | try { |
duke@1 | 371 | path = f.getCanonicalPath(); |
duke@1 | 372 | } catch (IOException ex) { |
duke@1 | 373 | return false; |
duke@1 | 374 | } |
duke@1 | 375 | char[] pcs = path.toCharArray(); |
jjg@103 | 376 | char[] ncs = name.path.toCharArray(); |
duke@1 | 377 | int i = pcs.length - 1; |
duke@1 | 378 | int j = ncs.length - 1; |
duke@1 | 379 | while (i >= 0 && j >= 0) { |
duke@1 | 380 | while (i >= 0 && pcs[i] == File.separatorChar) i--; |
jjg@103 | 381 | while (j >= 0 && ncs[j] == '/') j--; |
duke@1 | 382 | if (i >= 0 && j >= 0) { |
duke@1 | 383 | if (pcs[i] != ncs[j]) return false; |
duke@1 | 384 | i--; |
duke@1 | 385 | j--; |
duke@1 | 386 | } |
duke@1 | 387 | } |
duke@1 | 388 | return j < 0; |
duke@1 | 389 | } |
duke@1 | 390 | |
duke@1 | 391 | /** |
duke@1 | 392 | * An archive provides a flat directory structure of a ZipFile by |
duke@1 | 393 | * mapping directory names to lists of files (basenames). |
duke@1 | 394 | */ |
duke@1 | 395 | public interface Archive { |
duke@1 | 396 | void close() throws IOException; |
duke@1 | 397 | |
jjg@103 | 398 | boolean contains(RelativePath name); |
duke@1 | 399 | |
jjg@103 | 400 | JavaFileObject getFileObject(RelativeDirectory subdirectory, String file); |
duke@1 | 401 | |
jjg@103 | 402 | List<String> getFiles(RelativeDirectory subdirectory); |
duke@1 | 403 | |
jjg@103 | 404 | Set<RelativeDirectory> getSubdirectories(); |
duke@1 | 405 | } |
duke@1 | 406 | |
duke@1 | 407 | public class MissingArchive implements Archive { |
duke@1 | 408 | final File zipFileName; |
duke@1 | 409 | public MissingArchive(File name) { |
duke@1 | 410 | zipFileName = name; |
duke@1 | 411 | } |
jjg@103 | 412 | public boolean contains(RelativePath name) { |
jjg@57 | 413 | return false; |
duke@1 | 414 | } |
duke@1 | 415 | |
duke@1 | 416 | public void close() { |
duke@1 | 417 | } |
duke@1 | 418 | |
jjg@103 | 419 | public JavaFileObject getFileObject(RelativeDirectory subdirectory, String file) { |
duke@1 | 420 | return null; |
duke@1 | 421 | } |
duke@1 | 422 | |
jjg@103 | 423 | public List<String> getFiles(RelativeDirectory subdirectory) { |
duke@1 | 424 | return List.nil(); |
duke@1 | 425 | } |
duke@1 | 426 | |
jjg@103 | 427 | public Set<RelativeDirectory> getSubdirectories() { |
duke@1 | 428 | return Collections.emptySet(); |
duke@1 | 429 | } |
jjg@103 | 430 | |
jjg@103 | 431 | public String toString() { |
jjg@103 | 432 | return "MissingArchive[" + zipFileName + "]"; |
jjg@103 | 433 | } |
duke@1 | 434 | } |
duke@1 | 435 | |
duke@1 | 436 | /** A directory of zip files already opened. |
duke@1 | 437 | */ |
duke@1 | 438 | Map<File, Archive> archives = new HashMap<File,Archive>(); |
duke@1 | 439 | |
jjg@103 | 440 | private static final String[] symbolFileLocation = { "lib", "ct.sym" }; |
jjg@103 | 441 | private static final RelativeDirectory symbolFilePrefix |
jjg@103 | 442 | = new RelativeDirectory("META-INF/sym/rt.jar/"); |
jjg@103 | 443 | |
duke@1 | 444 | /** Open a new zip file directory. |
duke@1 | 445 | */ |
duke@1 | 446 | protected Archive openArchive(File zipFileName) throws IOException { |
duke@1 | 447 | Archive archive = archives.get(zipFileName); |
duke@1 | 448 | if (archive == null) { |
duke@1 | 449 | File origZipFileName = zipFileName; |
duke@1 | 450 | if (!ignoreSymbolFile && paths.isBootClassPathRtJar(zipFileName)) { |
duke@1 | 451 | File file = zipFileName.getParentFile().getParentFile(); // ${java.home} |
duke@1 | 452 | if (new File(file.getName()).equals(new File("jre"))) |
duke@1 | 453 | file = file.getParentFile(); |
duke@1 | 454 | // file == ${jdk.home} |
duke@1 | 455 | for (String name : symbolFileLocation) |
duke@1 | 456 | file = new File(file, name); |
duke@1 | 457 | // file == ${jdk.home}/lib/ct.sym |
duke@1 | 458 | if (file.exists()) |
duke@1 | 459 | zipFileName = file; |
duke@1 | 460 | } |
duke@1 | 461 | |
duke@1 | 462 | try { |
duke@1 | 463 | |
duke@1 | 464 | ZipFile zdir = null; |
duke@1 | 465 | |
duke@1 | 466 | boolean usePreindexedCache = false; |
duke@1 | 467 | String preindexCacheLocation = null; |
duke@1 | 468 | |
duke@1 | 469 | if (!useZipFileIndex) { |
duke@1 | 470 | zdir = new ZipFile(zipFileName); |
duke@1 | 471 | } |
duke@1 | 472 | else { |
duke@1 | 473 | usePreindexedCache = options.get("usezipindex") != null; |
duke@1 | 474 | preindexCacheLocation = options.get("java.io.tmpdir"); |
duke@1 | 475 | String optCacheLoc = options.get("cachezipindexdir"); |
duke@1 | 476 | |
duke@1 | 477 | if (optCacheLoc != null && optCacheLoc.length() != 0) { |
duke@1 | 478 | if (optCacheLoc.startsWith("\"")) { |
duke@1 | 479 | if (optCacheLoc.endsWith("\"")) { |
duke@1 | 480 | optCacheLoc = optCacheLoc.substring(1, optCacheLoc.length() - 1); |
duke@1 | 481 | } |
duke@1 | 482 | else { |
duke@1 | 483 | optCacheLoc = optCacheLoc.substring(1); |
duke@1 | 484 | } |
duke@1 | 485 | } |
duke@1 | 486 | |
duke@1 | 487 | File cacheDir = new File(optCacheLoc); |
duke@1 | 488 | if (cacheDir.exists() && cacheDir.canWrite()) { |
duke@1 | 489 | preindexCacheLocation = optCacheLoc; |
duke@1 | 490 | if (!preindexCacheLocation.endsWith("/") && |
duke@1 | 491 | !preindexCacheLocation.endsWith(File.separator)) { |
duke@1 | 492 | preindexCacheLocation += File.separator; |
duke@1 | 493 | } |
duke@1 | 494 | } |
duke@1 | 495 | } |
duke@1 | 496 | } |
duke@1 | 497 | |
duke@1 | 498 | if (origZipFileName == zipFileName) { |
duke@1 | 499 | if (!useZipFileIndex) { |
jjg@57 | 500 | archive = new ZipArchive(this, zdir); |
duke@1 | 501 | } else { |
jjg@103 | 502 | archive = new ZipFileIndexArchive(this, |
jjg@103 | 503 | ZipFileIndex.getZipFileIndex(zipFileName, |
jjg@103 | 504 | null, |
jjg@103 | 505 | usePreindexedCache, |
jjg@103 | 506 | preindexCacheLocation, |
jjg@103 | 507 | options.get("writezipindexfiles") != null)); |
duke@1 | 508 | } |
duke@1 | 509 | } |
duke@1 | 510 | else { |
duke@1 | 511 | if (!useZipFileIndex) { |
jjg@57 | 512 | archive = new SymbolArchive(this, origZipFileName, zdir, symbolFilePrefix); |
duke@1 | 513 | } |
duke@1 | 514 | else { |
jjg@57 | 515 | archive = new ZipFileIndexArchive(this, |
jjg@57 | 516 | ZipFileIndex.getZipFileIndex(zipFileName, |
jjg@103 | 517 | symbolFilePrefix, |
jjg@103 | 518 | usePreindexedCache, |
jjg@103 | 519 | preindexCacheLocation, |
jjg@103 | 520 | options.get("writezipindexfiles") != null)); |
duke@1 | 521 | } |
duke@1 | 522 | } |
duke@1 | 523 | } catch (FileNotFoundException ex) { |
duke@1 | 524 | archive = new MissingArchive(zipFileName); |
duke@1 | 525 | } catch (IOException ex) { |
jjg@56 | 526 | if (zipFileName.exists()) |
jjg@56 | 527 | log.error("error.reading.file", zipFileName, ex.getLocalizedMessage()); |
duke@1 | 528 | archive = new MissingArchive(zipFileName); |
duke@1 | 529 | } |
duke@1 | 530 | |
duke@1 | 531 | archives.put(origZipFileName, archive); |
duke@1 | 532 | } |
duke@1 | 533 | return archive; |
duke@1 | 534 | } |
duke@1 | 535 | |
duke@1 | 536 | /** Flush any output resources. |
duke@1 | 537 | */ |
duke@1 | 538 | public void flush() { |
duke@1 | 539 | contentCache.clear(); |
duke@1 | 540 | } |
duke@1 | 541 | |
duke@1 | 542 | /** |
duke@1 | 543 | * Close the JavaFileManager, releasing resources. |
duke@1 | 544 | */ |
duke@1 | 545 | public void close() { |
duke@1 | 546 | for (Iterator<Archive> i = archives.values().iterator(); i.hasNext(); ) { |
duke@1 | 547 | Archive a = i.next(); |
duke@1 | 548 | i.remove(); |
duke@1 | 549 | try { |
duke@1 | 550 | a.close(); |
duke@1 | 551 | } catch (IOException e) { |
duke@1 | 552 | } |
duke@1 | 553 | } |
duke@1 | 554 | } |
duke@1 | 555 | |
jjg@57 | 556 | CharBuffer getCachedContent(JavaFileObject file) { |
jjg@57 | 557 | SoftReference<CharBuffer> r = contentCache.get(file); |
jjg@57 | 558 | return (r == null ? null : r.get()); |
jjg@57 | 559 | } |
jjg@57 | 560 | |
jjg@57 | 561 | void cache(JavaFileObject file, CharBuffer cb) { |
jjg@57 | 562 | contentCache.put(file, new SoftReference<CharBuffer>(cb)); |
jjg@57 | 563 | } |
jjg@57 | 564 | |
jjg@57 | 565 | private final Map<JavaFileObject, SoftReference<CharBuffer>> contentCache |
jjg@57 | 566 | = new HashMap<JavaFileObject, SoftReference<CharBuffer>>(); |
duke@1 | 567 | |
duke@1 | 568 | private String defaultEncodingName; |
duke@1 | 569 | private String getDefaultEncodingName() { |
duke@1 | 570 | if (defaultEncodingName == null) { |
duke@1 | 571 | defaultEncodingName = |
duke@1 | 572 | new OutputStreamWriter(new ByteArrayOutputStream()).getEncoding(); |
duke@1 | 573 | } |
duke@1 | 574 | return defaultEncodingName; |
duke@1 | 575 | } |
duke@1 | 576 | |
duke@1 | 577 | protected String getEncodingName() { |
duke@1 | 578 | String encName = options.get(OptionName.ENCODING); |
duke@1 | 579 | if (encName == null) |
duke@1 | 580 | return getDefaultEncodingName(); |
duke@1 | 581 | else |
duke@1 | 582 | return encName; |
duke@1 | 583 | } |
duke@1 | 584 | |
duke@1 | 585 | protected Source getSource() { |
duke@1 | 586 | String sourceName = options.get(OptionName.SOURCE); |
duke@1 | 587 | Source source = null; |
duke@1 | 588 | if (sourceName != null) |
duke@1 | 589 | source = Source.lookup(sourceName); |
duke@1 | 590 | return (source != null ? source : Source.DEFAULT); |
duke@1 | 591 | } |
duke@1 | 592 | |
duke@1 | 593 | /** |
duke@1 | 594 | * Make a byte buffer from an input stream. |
duke@1 | 595 | */ |
jjg@57 | 596 | ByteBuffer makeByteBuffer(InputStream in) |
duke@1 | 597 | throws IOException { |
duke@1 | 598 | int limit = in.available(); |
duke@1 | 599 | if (mmappedIO && in instanceof FileInputStream) { |
duke@1 | 600 | // Experimental memory mapped I/O |
duke@1 | 601 | FileInputStream fin = (FileInputStream)in; |
duke@1 | 602 | return fin.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, limit); |
duke@1 | 603 | } |
duke@1 | 604 | if (limit < 1024) limit = 1024; |
duke@1 | 605 | ByteBuffer result = byteBufferCache.get(limit); |
duke@1 | 606 | int position = 0; |
duke@1 | 607 | while (in.available() != 0) { |
duke@1 | 608 | if (position >= limit) |
duke@1 | 609 | // expand buffer |
duke@1 | 610 | result = ByteBuffer. |
duke@1 | 611 | allocate(limit <<= 1). |
duke@1 | 612 | put((ByteBuffer)result.flip()); |
duke@1 | 613 | int count = in.read(result.array(), |
duke@1 | 614 | position, |
duke@1 | 615 | limit - position); |
duke@1 | 616 | if (count < 0) break; |
duke@1 | 617 | result.position(position += count); |
duke@1 | 618 | } |
duke@1 | 619 | return (ByteBuffer)result.flip(); |
duke@1 | 620 | } |
duke@1 | 621 | |
jjg@57 | 622 | void recycleByteBuffer(ByteBuffer bb) { |
jjg@57 | 623 | byteBufferCache.put(bb); |
jjg@57 | 624 | } |
jjg@57 | 625 | |
duke@1 | 626 | /** |
duke@1 | 627 | * A single-element cache of direct byte buffers. |
duke@1 | 628 | */ |
duke@1 | 629 | private static class ByteBufferCache { |
duke@1 | 630 | private ByteBuffer cached; |
duke@1 | 631 | ByteBuffer get(int capacity) { |
duke@1 | 632 | if (capacity < 20480) capacity = 20480; |
duke@1 | 633 | ByteBuffer result = |
duke@1 | 634 | (cached != null && cached.capacity() >= capacity) |
duke@1 | 635 | ? (ByteBuffer)cached.clear() |
duke@1 | 636 | : ByteBuffer.allocate(capacity + capacity>>1); |
duke@1 | 637 | cached = null; |
duke@1 | 638 | return result; |
duke@1 | 639 | } |
duke@1 | 640 | void put(ByteBuffer x) { |
duke@1 | 641 | cached = x; |
duke@1 | 642 | } |
duke@1 | 643 | } |
jjg@57 | 644 | |
duke@1 | 645 | private final ByteBufferCache byteBufferCache; |
duke@1 | 646 | |
jjg@57 | 647 | CharsetDecoder getDecoder(String encodingName, boolean ignoreEncodingErrors) { |
duke@1 | 648 | Charset charset = (this.charset == null) |
duke@1 | 649 | ? Charset.forName(encodingName) |
duke@1 | 650 | : this.charset; |
duke@1 | 651 | CharsetDecoder decoder = charset.newDecoder(); |
duke@1 | 652 | |
duke@1 | 653 | CodingErrorAction action; |
duke@1 | 654 | if (ignoreEncodingErrors) |
duke@1 | 655 | action = CodingErrorAction.REPLACE; |
duke@1 | 656 | else |
duke@1 | 657 | action = CodingErrorAction.REPORT; |
duke@1 | 658 | |
duke@1 | 659 | return decoder |
duke@1 | 660 | .onMalformedInput(action) |
duke@1 | 661 | .onUnmappableCharacter(action); |
duke@1 | 662 | } |
duke@1 | 663 | |
duke@1 | 664 | /** |
duke@1 | 665 | * Decode a ByteBuffer into a CharBuffer. |
duke@1 | 666 | */ |
jjg@57 | 667 | CharBuffer decode(ByteBuffer inbuf, boolean ignoreEncodingErrors) { |
duke@1 | 668 | String encodingName = getEncodingName(); |
duke@1 | 669 | CharsetDecoder decoder; |
duke@1 | 670 | try { |
duke@1 | 671 | decoder = getDecoder(encodingName, ignoreEncodingErrors); |
duke@1 | 672 | } catch (IllegalCharsetNameException e) { |
duke@1 | 673 | log.error("unsupported.encoding", encodingName); |
duke@1 | 674 | return (CharBuffer)CharBuffer.allocate(1).flip(); |
duke@1 | 675 | } catch (UnsupportedCharsetException e) { |
duke@1 | 676 | log.error("unsupported.encoding", encodingName); |
duke@1 | 677 | return (CharBuffer)CharBuffer.allocate(1).flip(); |
duke@1 | 678 | } |
duke@1 | 679 | |
duke@1 | 680 | // slightly overestimate the buffer size to avoid reallocation. |
duke@1 | 681 | float factor = |
duke@1 | 682 | decoder.averageCharsPerByte() * 0.8f + |
duke@1 | 683 | decoder.maxCharsPerByte() * 0.2f; |
duke@1 | 684 | CharBuffer dest = CharBuffer. |
duke@1 | 685 | allocate(10 + (int)(inbuf.remaining()*factor)); |
duke@1 | 686 | |
duke@1 | 687 | while (true) { |
duke@1 | 688 | CoderResult result = decoder.decode(inbuf, dest, true); |
duke@1 | 689 | dest.flip(); |
duke@1 | 690 | |
duke@1 | 691 | if (result.isUnderflow()) { // done reading |
duke@1 | 692 | // make sure there is at least one extra character |
duke@1 | 693 | if (dest.limit() == dest.capacity()) { |
duke@1 | 694 | dest = CharBuffer.allocate(dest.capacity()+1).put(dest); |
duke@1 | 695 | dest.flip(); |
duke@1 | 696 | } |
duke@1 | 697 | return dest; |
duke@1 | 698 | } else if (result.isOverflow()) { // buffer too small; expand |
duke@1 | 699 | int newCapacity = |
duke@1 | 700 | 10 + dest.capacity() + |
duke@1 | 701 | (int)(inbuf.remaining()*decoder.maxCharsPerByte()); |
duke@1 | 702 | dest = CharBuffer.allocate(newCapacity).put(dest); |
duke@1 | 703 | } else if (result.isMalformed() || result.isUnmappable()) { |
duke@1 | 704 | // bad character in input |
duke@1 | 705 | |
duke@1 | 706 | // report coding error (warn only pre 1.5) |
duke@1 | 707 | if (!getSource().allowEncodingErrors()) { |
duke@1 | 708 | log.error(new SimpleDiagnosticPosition(dest.limit()), |
duke@1 | 709 | "illegal.char.for.encoding", |
duke@1 | 710 | charset == null ? encodingName : charset.name()); |
duke@1 | 711 | } else { |
duke@1 | 712 | log.warning(new SimpleDiagnosticPosition(dest.limit()), |
duke@1 | 713 | "illegal.char.for.encoding", |
duke@1 | 714 | charset == null ? encodingName : charset.name()); |
duke@1 | 715 | } |
duke@1 | 716 | |
duke@1 | 717 | // skip past the coding error |
duke@1 | 718 | inbuf.position(inbuf.position() + result.length()); |
duke@1 | 719 | |
duke@1 | 720 | // undo the flip() to prepare the output buffer |
duke@1 | 721 | // for more translation |
duke@1 | 722 | dest.position(dest.limit()); |
duke@1 | 723 | dest.limit(dest.capacity()); |
duke@1 | 724 | dest.put((char)0xfffd); // backward compatible |
duke@1 | 725 | } else { |
duke@1 | 726 | throw new AssertionError(result); |
duke@1 | 727 | } |
duke@1 | 728 | } |
duke@1 | 729 | // unreached |
duke@1 | 730 | } |
duke@1 | 731 | |
duke@1 | 732 | public ClassLoader getClassLoader(Location location) { |
duke@1 | 733 | nullCheck(location); |
duke@1 | 734 | Iterable<? extends File> path = getLocation(location); |
duke@1 | 735 | if (path == null) |
duke@1 | 736 | return null; |
duke@1 | 737 | ListBuffer<URL> lb = new ListBuffer<URL>(); |
duke@1 | 738 | for (File f: path) { |
duke@1 | 739 | try { |
duke@1 | 740 | lb.append(f.toURI().toURL()); |
duke@1 | 741 | } catch (MalformedURLException e) { |
duke@1 | 742 | throw new AssertionError(e); |
duke@1 | 743 | } |
duke@1 | 744 | } |
duke@1 | 745 | return new URLClassLoader(lb.toArray(new URL[lb.size()]), |
duke@1 | 746 | getClass().getClassLoader()); |
duke@1 | 747 | } |
duke@1 | 748 | |
duke@1 | 749 | public Iterable<JavaFileObject> list(Location location, |
duke@1 | 750 | String packageName, |
duke@1 | 751 | Set<JavaFileObject.Kind> kinds, |
duke@1 | 752 | boolean recurse) |
duke@1 | 753 | throws IOException |
duke@1 | 754 | { |
duke@1 | 755 | // validatePackageName(packageName); |
duke@1 | 756 | nullCheck(packageName); |
duke@1 | 757 | nullCheck(kinds); |
duke@1 | 758 | |
duke@1 | 759 | Iterable<? extends File> path = getLocation(location); |
duke@1 | 760 | if (path == null) |
duke@1 | 761 | return List.nil(); |
jjg@103 | 762 | RelativeDirectory subdirectory = RelativeDirectory.forPackage(packageName); |
duke@1 | 763 | ListBuffer<JavaFileObject> results = new ListBuffer<JavaFileObject>(); |
duke@1 | 764 | |
duke@1 | 765 | for (File directory : path) |
duke@1 | 766 | listDirectory(directory, subdirectory, kinds, recurse, results); |
duke@1 | 767 | |
duke@1 | 768 | return results.toList(); |
duke@1 | 769 | } |
duke@1 | 770 | |
duke@1 | 771 | public String inferBinaryName(Location location, JavaFileObject file) { |
duke@1 | 772 | file.getClass(); // null check |
duke@1 | 773 | location.getClass(); // null check |
duke@1 | 774 | // Need to match the path semantics of list(location, ...) |
duke@1 | 775 | Iterable<? extends File> path = getLocation(location); |
duke@1 | 776 | if (path == null) { |
duke@1 | 777 | return null; |
duke@1 | 778 | } |
duke@1 | 779 | |
jjg@57 | 780 | if (file instanceof BaseFileObject) { |
jjg@57 | 781 | return ((BaseFileObject) file).inferBinaryName(path); |
duke@1 | 782 | } else |
duke@1 | 783 | throw new IllegalArgumentException(file.getClass().getName()); |
duke@1 | 784 | } |
duke@1 | 785 | |
duke@1 | 786 | public boolean isSameFile(FileObject a, FileObject b) { |
duke@1 | 787 | nullCheck(a); |
duke@1 | 788 | nullCheck(b); |
duke@1 | 789 | if (!(a instanceof BaseFileObject)) |
duke@1 | 790 | throw new IllegalArgumentException("Not supported: " + a); |
duke@1 | 791 | if (!(b instanceof BaseFileObject)) |
duke@1 | 792 | throw new IllegalArgumentException("Not supported: " + b); |
duke@1 | 793 | return a.equals(b); |
duke@1 | 794 | } |
duke@1 | 795 | |
duke@1 | 796 | public boolean handleOption(String current, Iterator<String> remaining) { |
duke@1 | 797 | for (JavacOption o: javacFileManagerOptions) { |
duke@1 | 798 | if (o.matches(current)) { |
duke@1 | 799 | if (o.hasArg()) { |
duke@1 | 800 | if (remaining.hasNext()) { |
duke@1 | 801 | if (!o.process(options, current, remaining.next())) |
duke@1 | 802 | return true; |
duke@1 | 803 | } |
duke@1 | 804 | } else { |
duke@1 | 805 | if (!o.process(options, current)) |
duke@1 | 806 | return true; |
duke@1 | 807 | } |
duke@1 | 808 | // operand missing, or process returned false |
duke@1 | 809 | throw new IllegalArgumentException(current); |
duke@1 | 810 | } |
duke@1 | 811 | } |
duke@1 | 812 | |
duke@1 | 813 | return false; |
duke@1 | 814 | } |
duke@1 | 815 | // where |
duke@1 | 816 | private static JavacOption[] javacFileManagerOptions = |
duke@1 | 817 | RecognizedOptions.getJavacFileManagerOptions( |
duke@1 | 818 | new RecognizedOptions.GrumpyHelper()); |
duke@1 | 819 | |
duke@1 | 820 | public int isSupportedOption(String option) { |
duke@1 | 821 | for (JavacOption o : javacFileManagerOptions) { |
duke@1 | 822 | if (o.matches(option)) |
duke@1 | 823 | return o.hasArg() ? 1 : 0; |
duke@1 | 824 | } |
duke@1 | 825 | return -1; |
duke@1 | 826 | } |
duke@1 | 827 | |
duke@1 | 828 | public boolean hasLocation(Location location) { |
duke@1 | 829 | return getLocation(location) != null; |
duke@1 | 830 | } |
duke@1 | 831 | |
duke@1 | 832 | public JavaFileObject getJavaFileForInput(Location location, |
duke@1 | 833 | String className, |
duke@1 | 834 | JavaFileObject.Kind kind) |
duke@1 | 835 | throws IOException |
duke@1 | 836 | { |
duke@1 | 837 | nullCheck(location); |
duke@1 | 838 | // validateClassName(className); |
duke@1 | 839 | nullCheck(className); |
duke@1 | 840 | nullCheck(kind); |
duke@1 | 841 | if (!sourceOrClass.contains(kind)) |
duke@1 | 842 | throw new IllegalArgumentException("Invalid kind " + kind); |
jjg@103 | 843 | return getFileForInput(location, RelativeFile.forClass(className, kind)); |
duke@1 | 844 | } |
duke@1 | 845 | |
duke@1 | 846 | public FileObject getFileForInput(Location location, |
duke@1 | 847 | String packageName, |
duke@1 | 848 | String relativeName) |
duke@1 | 849 | throws IOException |
duke@1 | 850 | { |
duke@1 | 851 | nullCheck(location); |
duke@1 | 852 | // validatePackageName(packageName); |
duke@1 | 853 | nullCheck(packageName); |
duke@1 | 854 | if (!isRelativeUri(URI.create(relativeName))) // FIXME 6419701 |
duke@1 | 855 | throw new IllegalArgumentException("Invalid relative name: " + relativeName); |
jjg@103 | 856 | RelativeFile name = packageName.length() == 0 |
jjg@103 | 857 | ? new RelativeFile(relativeName) |
jjg@103 | 858 | : new RelativeFile(RelativeDirectory.forPackage(packageName), relativeName); |
duke@1 | 859 | return getFileForInput(location, name); |
duke@1 | 860 | } |
duke@1 | 861 | |
jjg@103 | 862 | private JavaFileObject getFileForInput(Location location, RelativeFile name) throws IOException { |
duke@1 | 863 | Iterable<? extends File> path = getLocation(location); |
duke@1 | 864 | if (path == null) |
duke@1 | 865 | return null; |
duke@1 | 866 | |
duke@1 | 867 | for (File dir: path) { |
duke@1 | 868 | if (dir.isDirectory()) { |
jjg@103 | 869 | File f = name.getFile(dir); |
duke@1 | 870 | if (f.exists()) |
jjg@57 | 871 | return new RegularFileObject(this, f); |
duke@1 | 872 | } else { |
duke@1 | 873 | Archive a = openArchive(dir); |
duke@1 | 874 | if (a.contains(name)) { |
jjg@103 | 875 | return a.getFileObject(name.dirname(), name.basename()); |
duke@1 | 876 | } |
duke@1 | 877 | |
duke@1 | 878 | } |
duke@1 | 879 | } |
jjg@103 | 880 | |
duke@1 | 881 | return null; |
duke@1 | 882 | } |
duke@1 | 883 | |
duke@1 | 884 | public JavaFileObject getJavaFileForOutput(Location location, |
duke@1 | 885 | String className, |
duke@1 | 886 | JavaFileObject.Kind kind, |
duke@1 | 887 | FileObject sibling) |
duke@1 | 888 | throws IOException |
duke@1 | 889 | { |
duke@1 | 890 | nullCheck(location); |
duke@1 | 891 | // validateClassName(className); |
duke@1 | 892 | nullCheck(className); |
duke@1 | 893 | nullCheck(kind); |
duke@1 | 894 | if (!sourceOrClass.contains(kind)) |
duke@1 | 895 | throw new IllegalArgumentException("Invalid kind " + kind); |
jjg@103 | 896 | return getFileForOutput(location, RelativeFile.forClass(className, kind), sibling); |
duke@1 | 897 | } |
duke@1 | 898 | |
duke@1 | 899 | public FileObject getFileForOutput(Location location, |
duke@1 | 900 | String packageName, |
duke@1 | 901 | String relativeName, |
duke@1 | 902 | FileObject sibling) |
duke@1 | 903 | throws IOException |
duke@1 | 904 | { |
duke@1 | 905 | nullCheck(location); |
duke@1 | 906 | // validatePackageName(packageName); |
duke@1 | 907 | nullCheck(packageName); |
duke@1 | 908 | if (!isRelativeUri(URI.create(relativeName))) // FIXME 6419701 |
duke@1 | 909 | throw new IllegalArgumentException("relativeName is invalid"); |
jjg@103 | 910 | RelativeFile name = packageName.length() == 0 |
jjg@103 | 911 | ? new RelativeFile(relativeName) |
jjg@103 | 912 | : new RelativeFile(RelativeDirectory.forPackage(packageName), relativeName); |
duke@1 | 913 | return getFileForOutput(location, name, sibling); |
duke@1 | 914 | } |
duke@1 | 915 | |
duke@1 | 916 | private JavaFileObject getFileForOutput(Location location, |
jjg@103 | 917 | RelativeFile fileName, |
duke@1 | 918 | FileObject sibling) |
duke@1 | 919 | throws IOException |
duke@1 | 920 | { |
duke@1 | 921 | File dir; |
duke@1 | 922 | if (location == CLASS_OUTPUT) { |
duke@1 | 923 | if (getClassOutDir() != null) { |
duke@1 | 924 | dir = getClassOutDir(); |
duke@1 | 925 | } else { |
duke@1 | 926 | File siblingDir = null; |
duke@1 | 927 | if (sibling != null && sibling instanceof RegularFileObject) { |
duke@1 | 928 | siblingDir = ((RegularFileObject)sibling).f.getParentFile(); |
duke@1 | 929 | } |
jjg@103 | 930 | return new RegularFileObject(this, new File(siblingDir, fileName.basename())); |
duke@1 | 931 | } |
duke@1 | 932 | } else if (location == SOURCE_OUTPUT) { |
duke@1 | 933 | dir = (getSourceOutDir() != null ? getSourceOutDir() : getClassOutDir()); |
duke@1 | 934 | } else { |
duke@1 | 935 | Iterable<? extends File> path = paths.getPathForLocation(location); |
duke@1 | 936 | dir = null; |
duke@1 | 937 | for (File f: path) { |
duke@1 | 938 | dir = f; |
duke@1 | 939 | break; |
duke@1 | 940 | } |
duke@1 | 941 | } |
duke@1 | 942 | |
jjg@103 | 943 | File file = fileName.getFile(dir); // null-safe |
jjg@57 | 944 | return new RegularFileObject(this, file); |
duke@1 | 945 | |
duke@1 | 946 | } |
duke@1 | 947 | |
duke@1 | 948 | public Iterable<? extends JavaFileObject> getJavaFileObjectsFromFiles( |
duke@1 | 949 | Iterable<? extends File> files) |
duke@1 | 950 | { |
duke@1 | 951 | ArrayList<RegularFileObject> result; |
mcimadamore@184 | 952 | if (files instanceof Collection<?>) |
mcimadamore@184 | 953 | result = new ArrayList<RegularFileObject>(((Collection<?>)files).size()); |
duke@1 | 954 | else |
duke@1 | 955 | result = new ArrayList<RegularFileObject>(); |
duke@1 | 956 | for (File f: files) |
jjg@57 | 957 | result.add(new RegularFileObject(this, nullCheck(f))); |
duke@1 | 958 | return result; |
duke@1 | 959 | } |
duke@1 | 960 | |
duke@1 | 961 | public Iterable<? extends JavaFileObject> getJavaFileObjects(File... files) { |
duke@1 | 962 | return getJavaFileObjectsFromFiles(Arrays.asList(nullCheck(files))); |
duke@1 | 963 | } |
duke@1 | 964 | |
duke@1 | 965 | public void setLocation(Location location, |
duke@1 | 966 | Iterable<? extends File> path) |
duke@1 | 967 | throws IOException |
duke@1 | 968 | { |
duke@1 | 969 | nullCheck(location); |
duke@1 | 970 | paths.lazy(); |
duke@1 | 971 | |
duke@1 | 972 | final File dir = location.isOutputLocation() ? getOutputDirectory(path) : null; |
duke@1 | 973 | |
duke@1 | 974 | if (location == CLASS_OUTPUT) |
duke@1 | 975 | classOutDir = getOutputLocation(dir, D); |
duke@1 | 976 | else if (location == SOURCE_OUTPUT) |
duke@1 | 977 | sourceOutDir = getOutputLocation(dir, S); |
duke@1 | 978 | else |
duke@1 | 979 | paths.setPathForLocation(location, path); |
duke@1 | 980 | } |
duke@1 | 981 | // where |
duke@1 | 982 | private File getOutputDirectory(Iterable<? extends File> path) throws IOException { |
duke@1 | 983 | if (path == null) |
duke@1 | 984 | return null; |
duke@1 | 985 | Iterator<? extends File> pathIter = path.iterator(); |
duke@1 | 986 | if (!pathIter.hasNext()) |
duke@1 | 987 | throw new IllegalArgumentException("empty path for directory"); |
duke@1 | 988 | File dir = pathIter.next(); |
duke@1 | 989 | if (pathIter.hasNext()) |
duke@1 | 990 | throw new IllegalArgumentException("path too long for directory"); |
duke@1 | 991 | if (!dir.exists()) |
duke@1 | 992 | throw new FileNotFoundException(dir + ": does not exist"); |
duke@1 | 993 | else if (!dir.isDirectory()) |
duke@1 | 994 | throw new IOException(dir + ": not a directory"); |
duke@1 | 995 | return dir; |
duke@1 | 996 | } |
duke@1 | 997 | |
duke@1 | 998 | private File getOutputLocation(File dir, OptionName defaultOptionName) { |
duke@1 | 999 | if (dir != null) |
duke@1 | 1000 | return dir; |
duke@1 | 1001 | String arg = options.get(defaultOptionName); |
duke@1 | 1002 | if (arg == null) |
duke@1 | 1003 | return null; |
duke@1 | 1004 | return new File(arg); |
duke@1 | 1005 | } |
duke@1 | 1006 | |
duke@1 | 1007 | public Iterable<? extends File> getLocation(Location location) { |
duke@1 | 1008 | nullCheck(location); |
duke@1 | 1009 | paths.lazy(); |
duke@1 | 1010 | if (location == CLASS_OUTPUT) { |
duke@1 | 1011 | return (getClassOutDir() == null ? null : List.of(getClassOutDir())); |
duke@1 | 1012 | } else if (location == SOURCE_OUTPUT) { |
duke@1 | 1013 | return (getSourceOutDir() == null ? null : List.of(getSourceOutDir())); |
duke@1 | 1014 | } else |
duke@1 | 1015 | return paths.getPathForLocation(location); |
duke@1 | 1016 | } |
duke@1 | 1017 | |
duke@1 | 1018 | private File getClassOutDir() { |
duke@1 | 1019 | if (classOutDir == uninited) |
duke@1 | 1020 | classOutDir = getOutputLocation(null, D); |
duke@1 | 1021 | return classOutDir; |
duke@1 | 1022 | } |
duke@1 | 1023 | |
duke@1 | 1024 | private File getSourceOutDir() { |
duke@1 | 1025 | if (sourceOutDir == uninited) |
duke@1 | 1026 | sourceOutDir = getOutputLocation(null, S); |
duke@1 | 1027 | return sourceOutDir; |
duke@1 | 1028 | } |
duke@1 | 1029 | |
duke@1 | 1030 | /** |
duke@1 | 1031 | * Enforces the specification of a "relative" URI as used in |
duke@1 | 1032 | * {@linkplain #getFileForInput(Location,String,URI) |
duke@1 | 1033 | * getFileForInput}. This method must follow the rules defined in |
duke@1 | 1034 | * that method, do not make any changes without consulting the |
duke@1 | 1035 | * specification. |
duke@1 | 1036 | */ |
duke@1 | 1037 | protected static boolean isRelativeUri(URI uri) { |
duke@1 | 1038 | if (uri.isAbsolute()) |
duke@1 | 1039 | return false; |
duke@1 | 1040 | String path = uri.normalize().getPath(); |
duke@1 | 1041 | if (path.length() == 0 /* isEmpty() is mustang API */) |
duke@1 | 1042 | return false; |
duke@1 | 1043 | char first = path.charAt(0); |
duke@1 | 1044 | return first != '.' && first != '/'; |
duke@1 | 1045 | } |
duke@1 | 1046 | |
duke@1 | 1047 | /** |
duke@1 | 1048 | * Converts a relative file name to a relative URI. This is |
duke@1 | 1049 | * different from File.toURI as this method does not canonicalize |
duke@1 | 1050 | * the file before creating the URI. Furthermore, no schema is |
duke@1 | 1051 | * used. |
duke@1 | 1052 | * @param file a relative file name |
duke@1 | 1053 | * @return a relative URI |
duke@1 | 1054 | * @throws IllegalArgumentException if the file name is not |
duke@1 | 1055 | * relative according to the definition given in {@link |
duke@1 | 1056 | * javax.tools.JavaFileManager#getFileForInput} |
duke@1 | 1057 | */ |
duke@1 | 1058 | public static String getRelativeName(File file) { |
duke@1 | 1059 | if (!file.isAbsolute()) { |
duke@1 | 1060 | String result = file.getPath().replace(File.separatorChar, '/'); |
duke@1 | 1061 | if (JavacFileManager.isRelativeUri(URI.create(result))) // FIXME 6419701 |
duke@1 | 1062 | return result; |
duke@1 | 1063 | } |
duke@1 | 1064 | throw new IllegalArgumentException("Invalid relative path: " + file); |
duke@1 | 1065 | } |
duke@1 | 1066 | |
duke@1 | 1067 | @SuppressWarnings("deprecation") // bug 6410637 |
jjg@50 | 1068 | public static String getJavacFileName(FileObject file) { |
duke@1 | 1069 | if (file instanceof BaseFileObject) |
duke@1 | 1070 | return ((BaseFileObject)file).getPath(); |
duke@1 | 1071 | URI uri = file.toUri(); |
duke@1 | 1072 | String scheme = uri.getScheme(); |
duke@1 | 1073 | if (scheme == null || scheme.equals("file") || scheme.equals("jar")) |
duke@1 | 1074 | return uri.getPath(); |
duke@1 | 1075 | else |
duke@1 | 1076 | return uri.toString(); |
duke@1 | 1077 | } |
duke@1 | 1078 | |
duke@1 | 1079 | @SuppressWarnings("deprecation") // bug 6410637 |
jjg@50 | 1080 | public static String getJavacBaseFileName(FileObject file) { |
duke@1 | 1081 | if (file instanceof BaseFileObject) |
duke@1 | 1082 | return ((BaseFileObject)file).getName(); |
duke@1 | 1083 | URI uri = file.toUri(); |
duke@1 | 1084 | String scheme = uri.getScheme(); |
duke@1 | 1085 | if (scheme == null || scheme.equals("file") || scheme.equals("jar")) { |
duke@1 | 1086 | String path = uri.getPath(); |
duke@1 | 1087 | if (path == null) |
duke@1 | 1088 | return null; |
duke@1 | 1089 | if (scheme != null && scheme.equals("jar")) |
duke@1 | 1090 | path = path.substring(path.lastIndexOf('!') + 1); |
duke@1 | 1091 | return path.substring(path.lastIndexOf('/') + 1); |
duke@1 | 1092 | } else { |
duke@1 | 1093 | return uri.toString(); |
duke@1 | 1094 | } |
duke@1 | 1095 | } |
duke@1 | 1096 | |
duke@1 | 1097 | private static <T> T nullCheck(T o) { |
duke@1 | 1098 | o.getClass(); // null check |
duke@1 | 1099 | return o; |
duke@1 | 1100 | } |
duke@1 | 1101 | |
duke@1 | 1102 | private static <T> Iterable<T> nullCheck(Iterable<T> it) { |
duke@1 | 1103 | for (T t : it) |
duke@1 | 1104 | t.getClass(); // null check |
duke@1 | 1105 | return it; |
duke@1 | 1106 | } |
duke@1 | 1107 | } |