Wed, 08 Oct 2014 14:16:40 -0700
Merge
duke@1 | 1 | /* |
ohair@554 | 2 | * Copyright (c) 2005, 2006, 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 | |
duke@1 | 26 | package javax.tools; |
duke@1 | 27 | |
duke@1 | 28 | import java.io.Closeable; |
duke@1 | 29 | import java.io.Flushable; |
duke@1 | 30 | import java.io.IOException; |
duke@1 | 31 | import java.util.Iterator; |
duke@1 | 32 | import java.util.Set; |
duke@1 | 33 | import static javax.tools.JavaFileObject.Kind; |
duke@1 | 34 | |
duke@1 | 35 | /** |
duke@1 | 36 | * File manager for tools operating on Java™ programming language |
duke@1 | 37 | * source and class files. In this context, <em>file</em> means an |
duke@1 | 38 | * abstraction of regular files and other sources of data. |
duke@1 | 39 | * |
duke@1 | 40 | * <p>When constructing new JavaFileObjects, the file manager must |
duke@1 | 41 | * determine where to create them. For example, if a file manager |
duke@1 | 42 | * manages regular files on a file system, it would most likely have a |
duke@1 | 43 | * current/working directory to use as default location when creating |
duke@1 | 44 | * or finding files. A number of hints can be provided to a file |
duke@1 | 45 | * manager as to where to create files. Any file manager might choose |
duke@1 | 46 | * to ignore these hints. |
duke@1 | 47 | * |
duke@1 | 48 | * <p>Some methods in this interface use class names. Such class |
duke@1 | 49 | * names must be given in the Java Virtual Machine internal form of |
duke@1 | 50 | * fully qualified class and interface names. For convenience '.' |
duke@1 | 51 | * and '/' are interchangeable. The internal form is defined in |
jjh@972 | 52 | * chapter four of |
jjh@972 | 53 | * <cite>The Java™ Virtual Machine Specification</cite>. |
duke@1 | 54 | |
duke@1 | 55 | * <blockquote><p> |
duke@1 | 56 | * <i>Discussion:</i> this means that the names |
duke@1 | 57 | * "java/lang.package-info", "java/lang/package-info", |
duke@1 | 58 | * "java.lang.package-info", are valid and equivalent. Compare to |
jjh@972 | 59 | * binary name as defined in |
jjh@972 | 60 | * <cite>The Java™ Language Specification</cite>, |
jjh@972 | 61 | * section 13.1 "The Form of a Binary". |
duke@1 | 62 | * </p></blockquote> |
duke@1 | 63 | * |
duke@1 | 64 | * <p>The case of names is significant. All names should be treated |
duke@1 | 65 | * as case-sensitive. For example, some file systems have |
duke@1 | 66 | * case-insensitive, case-aware file names. File objects representing |
duke@1 | 67 | * such files should take care to preserve case by using {@link |
duke@1 | 68 | * java.io.File#getCanonicalFile} or similar means. If the system is |
duke@1 | 69 | * not case-aware, file objects must use other means to preserve case. |
duke@1 | 70 | * |
duke@1 | 71 | * <p><em><a name="relative_name">Relative names</a>:</em> some |
duke@1 | 72 | * methods in this interface use relative names. A relative name is a |
duke@1 | 73 | * non-null, non-empty sequence of path segments separated by '/'. |
duke@1 | 74 | * '.' or '..' are invalid path segments. A valid relative name must |
duke@1 | 75 | * match the "path-rootless" rule of <a |
duke@1 | 76 | * href="http://www.ietf.org/rfc/rfc3986.txt">RFC 3986</a>, |
duke@1 | 77 | * section 3.3. Informally, this should be true: |
duke@1 | 78 | * |
duke@1 | 79 | * <!-- URI.create(relativeName).normalize().getPath().equals(relativeName) --> |
duke@1 | 80 | * <pre> URI.{@linkplain java.net.URI#create create}(relativeName).{@linkplain java.net.URI#normalize normalize}().{@linkplain java.net.URI#getPath getPath}().equals(relativeName)</pre> |
duke@1 | 81 | * |
duke@1 | 82 | * <p>All methods in this interface might throw a SecurityException. |
duke@1 | 83 | * |
duke@1 | 84 | * <p>An object of this interface is not required to support |
duke@1 | 85 | * multi-threaded access, that is, be synchronized. However, it must |
duke@1 | 86 | * support concurrent access to different file objects created by this |
duke@1 | 87 | * object. |
duke@1 | 88 | * |
duke@1 | 89 | * <p><em>Implementation note:</em> a consequence of this requirement |
duke@1 | 90 | * is that a trivial implementation of output to a {@linkplain |
duke@1 | 91 | * java.util.jar.JarOutputStream} is not a sufficient implementation. |
duke@1 | 92 | * That is, rather than creating a JavaFileObject that returns the |
duke@1 | 93 | * JarOutputStream directly, the contents must be cached until closed |
duke@1 | 94 | * and then written to the JarOutputStream. |
duke@1 | 95 | * |
duke@1 | 96 | * <p>Unless explicitly allowed, all methods in this interface might |
duke@1 | 97 | * throw a NullPointerException if given a {@code null} argument. |
duke@1 | 98 | * |
duke@1 | 99 | * @author Peter von der Ahé |
duke@1 | 100 | * @author Jonathan Gibbons |
duke@1 | 101 | * @see JavaFileObject |
duke@1 | 102 | * @see FileObject |
duke@1 | 103 | * @since 1.6 |
duke@1 | 104 | */ |
duke@1 | 105 | public interface JavaFileManager extends Closeable, Flushable, OptionChecker { |
duke@1 | 106 | |
duke@1 | 107 | /** |
duke@1 | 108 | * Interface for locations of file objects. Used by file managers |
duke@1 | 109 | * to determine where to place or search for file objects. |
duke@1 | 110 | */ |
duke@1 | 111 | interface Location { |
duke@1 | 112 | /** |
duke@1 | 113 | * Gets the name of this location. |
duke@1 | 114 | * |
duke@1 | 115 | * @return a name |
duke@1 | 116 | */ |
duke@1 | 117 | String getName(); |
duke@1 | 118 | |
duke@1 | 119 | /** |
duke@1 | 120 | * Determines if this is an output location. An output |
duke@1 | 121 | * location is a location that is conventionally used for |
duke@1 | 122 | * output. |
duke@1 | 123 | * |
duke@1 | 124 | * @return true if this is an output location, false otherwise |
duke@1 | 125 | */ |
duke@1 | 126 | boolean isOutputLocation(); |
duke@1 | 127 | } |
duke@1 | 128 | |
duke@1 | 129 | /** |
duke@1 | 130 | * Gets a class loader for loading plug-ins from the given |
duke@1 | 131 | * location. For example, to load annotation processors, a |
duke@1 | 132 | * compiler will request a class loader for the {@link |
duke@1 | 133 | * StandardLocation#ANNOTATION_PROCESSOR_PATH |
duke@1 | 134 | * ANNOTATION_PROCESSOR_PATH} location. |
duke@1 | 135 | * |
duke@1 | 136 | * @param location a location |
duke@1 | 137 | * @return a class loader for the given location; or {@code null} |
duke@1 | 138 | * if loading plug-ins from the given location is disabled or if |
duke@1 | 139 | * the location is not known |
duke@1 | 140 | * @throws SecurityException if a class loader can not be created |
duke@1 | 141 | * in the current security context |
duke@1 | 142 | * @throws IllegalStateException if {@link #close} has been called |
duke@1 | 143 | * and this file manager cannot be reopened |
duke@1 | 144 | */ |
duke@1 | 145 | ClassLoader getClassLoader(Location location); |
duke@1 | 146 | |
duke@1 | 147 | /** |
duke@1 | 148 | * Lists all file objects matching the given criteria in the given |
duke@1 | 149 | * location. List file objects in "subpackages" if recurse is |
duke@1 | 150 | * true. |
duke@1 | 151 | * |
duke@1 | 152 | * <p>Note: even if the given location is unknown to this file |
duke@1 | 153 | * manager, it may not return {@code null}. Also, an unknown |
duke@1 | 154 | * location may not cause an exception. |
duke@1 | 155 | * |
duke@1 | 156 | * @param location a location |
duke@1 | 157 | * @param packageName a package name |
duke@1 | 158 | * @param kinds return objects only of these kinds |
duke@1 | 159 | * @param recurse if true include "subpackages" |
duke@1 | 160 | * @return an Iterable of file objects matching the given criteria |
duke@1 | 161 | * @throws IOException if an I/O error occurred, or if {@link |
duke@1 | 162 | * #close} has been called and this file manager cannot be |
duke@1 | 163 | * reopened |
duke@1 | 164 | * @throws IllegalStateException if {@link #close} has been called |
duke@1 | 165 | * and this file manager cannot be reopened |
duke@1 | 166 | */ |
duke@1 | 167 | Iterable<JavaFileObject> list(Location location, |
duke@1 | 168 | String packageName, |
duke@1 | 169 | Set<Kind> kinds, |
duke@1 | 170 | boolean recurse) |
duke@1 | 171 | throws IOException; |
duke@1 | 172 | |
duke@1 | 173 | /** |
duke@1 | 174 | * Infers a binary name of a file object based on a location. The |
jjh@972 | 175 | * binary name returned might not be a valid binary name according to |
jjh@972 | 176 | * <cite>The Java™ Language Specification</cite>. |
duke@1 | 177 | * |
duke@1 | 178 | * @param location a location |
duke@1 | 179 | * @param file a file object |
duke@1 | 180 | * @return a binary name or {@code null} the file object is not |
duke@1 | 181 | * found in the given location |
duke@1 | 182 | * @throws IllegalStateException if {@link #close} has been called |
duke@1 | 183 | * and this file manager cannot be reopened |
duke@1 | 184 | */ |
duke@1 | 185 | String inferBinaryName(Location location, JavaFileObject file); |
duke@1 | 186 | |
duke@1 | 187 | /** |
duke@1 | 188 | * Compares two file objects and return true if they represent the |
duke@1 | 189 | * same underlying object. |
duke@1 | 190 | * |
duke@1 | 191 | * @param a a file object |
duke@1 | 192 | * @param b a file object |
duke@1 | 193 | * @return true if the given file objects represent the same |
duke@1 | 194 | * underlying object |
duke@1 | 195 | * |
duke@1 | 196 | * @throws IllegalArgumentException if either of the arguments |
duke@1 | 197 | * were created with another file manager and this file manager |
duke@1 | 198 | * does not support foreign file objects |
duke@1 | 199 | */ |
duke@1 | 200 | boolean isSameFile(FileObject a, FileObject b); |
duke@1 | 201 | |
duke@1 | 202 | /** |
duke@1 | 203 | * Handles one option. If {@code current} is an option to this |
duke@1 | 204 | * file manager it will consume any arguments to that option from |
duke@1 | 205 | * {@code remaining} and return true, otherwise return false. |
duke@1 | 206 | * |
duke@1 | 207 | * @param current current option |
duke@1 | 208 | * @param remaining remaining options |
duke@1 | 209 | * @return true if this option was handled by this file manager, |
duke@1 | 210 | * false otherwise |
duke@1 | 211 | * @throws IllegalArgumentException if this option to this file |
duke@1 | 212 | * manager is used incorrectly |
duke@1 | 213 | * @throws IllegalStateException if {@link #close} has been called |
duke@1 | 214 | * and this file manager cannot be reopened |
duke@1 | 215 | */ |
duke@1 | 216 | boolean handleOption(String current, Iterator<String> remaining); |
duke@1 | 217 | |
duke@1 | 218 | /** |
duke@1 | 219 | * Determines if a location is known to this file manager. |
duke@1 | 220 | * |
duke@1 | 221 | * @param location a location |
duke@1 | 222 | * @return true if the location is known |
duke@1 | 223 | */ |
duke@1 | 224 | boolean hasLocation(Location location); |
duke@1 | 225 | |
duke@1 | 226 | /** |
duke@1 | 227 | * Gets a {@linkplain JavaFileObject file object} for input |
duke@1 | 228 | * representing the specified class of the specified kind in the |
duke@1 | 229 | * given location. |
duke@1 | 230 | * |
duke@1 | 231 | * @param location a location |
duke@1 | 232 | * @param className the name of a class |
duke@1 | 233 | * @param kind the kind of file, must be one of {@link |
duke@1 | 234 | * JavaFileObject.Kind#SOURCE SOURCE} or {@link |
duke@1 | 235 | * JavaFileObject.Kind#CLASS CLASS} |
duke@1 | 236 | * @return a file object, might return {@code null} if the |
duke@1 | 237 | * file does not exist |
duke@1 | 238 | * @throws IllegalArgumentException if the location is not known |
duke@1 | 239 | * to this file manager and the file manager does not support |
duke@1 | 240 | * unknown locations, or if the kind is not valid |
duke@1 | 241 | * @throws IOException if an I/O error occurred, or if {@link |
duke@1 | 242 | * #close} has been called and this file manager cannot be |
duke@1 | 243 | * reopened |
duke@1 | 244 | * @throws IllegalStateException if {@link #close} has been called |
duke@1 | 245 | * and this file manager cannot be reopened |
duke@1 | 246 | */ |
duke@1 | 247 | JavaFileObject getJavaFileForInput(Location location, |
duke@1 | 248 | String className, |
duke@1 | 249 | Kind kind) |
duke@1 | 250 | throws IOException; |
duke@1 | 251 | |
duke@1 | 252 | /** |
duke@1 | 253 | * Gets a {@linkplain JavaFileObject file object} for output |
duke@1 | 254 | * representing the specified class of the specified kind in the |
duke@1 | 255 | * given location. |
duke@1 | 256 | * |
duke@1 | 257 | * <p>Optionally, this file manager might consider the sibling as |
duke@1 | 258 | * a hint for where to place the output. The exact semantics of |
jjg@582 | 259 | * this hint is unspecified. The JDK compiler, javac, for |
duke@1 | 260 | * example, will place class files in the same directories as |
duke@1 | 261 | * originating source files unless a class file output directory |
duke@1 | 262 | * is provided. To facilitate this behavior, javac might provide |
duke@1 | 263 | * the originating source file as sibling when calling this |
duke@1 | 264 | * method. |
duke@1 | 265 | * |
duke@1 | 266 | * @param location a location |
duke@1 | 267 | * @param className the name of a class |
duke@1 | 268 | * @param kind the kind of file, must be one of {@link |
duke@1 | 269 | * JavaFileObject.Kind#SOURCE SOURCE} or {@link |
duke@1 | 270 | * JavaFileObject.Kind#CLASS CLASS} |
duke@1 | 271 | * @param sibling a file object to be used as hint for placement; |
duke@1 | 272 | * might be {@code null} |
duke@1 | 273 | * @return a file object for output |
duke@1 | 274 | * @throws IllegalArgumentException if sibling is not known to |
duke@1 | 275 | * this file manager, or if the location is not known to this file |
duke@1 | 276 | * manager and the file manager does not support unknown |
duke@1 | 277 | * locations, or if the kind is not valid |
duke@1 | 278 | * @throws IOException if an I/O error occurred, or if {@link |
duke@1 | 279 | * #close} has been called and this file manager cannot be |
duke@1 | 280 | * reopened |
duke@1 | 281 | * @throws IllegalStateException {@link #close} has been called |
duke@1 | 282 | * and this file manager cannot be reopened |
duke@1 | 283 | */ |
duke@1 | 284 | JavaFileObject getJavaFileForOutput(Location location, |
duke@1 | 285 | String className, |
duke@1 | 286 | Kind kind, |
duke@1 | 287 | FileObject sibling) |
duke@1 | 288 | throws IOException; |
duke@1 | 289 | |
duke@1 | 290 | /** |
duke@1 | 291 | * Gets a {@linkplain FileObject file object} for input |
duke@1 | 292 | * representing the specified <a href="JavaFileManager.html#relative_name">relative |
duke@1 | 293 | * name</a> in the specified package in the given location. |
duke@1 | 294 | * |
duke@1 | 295 | * <p>If the returned object represents a {@linkplain |
duke@1 | 296 | * JavaFileObject.Kind#SOURCE source} or {@linkplain |
duke@1 | 297 | * JavaFileObject.Kind#CLASS class} file, it must be an instance |
duke@1 | 298 | * of {@link JavaFileObject}. |
duke@1 | 299 | * |
duke@1 | 300 | * <p>Informally, the file object returned by this method is |
duke@1 | 301 | * located in the concatenation of the location, package name, and |
duke@1 | 302 | * relative name. For example, to locate the properties file |
duke@1 | 303 | * "resources/compiler.properties" in the package |
duke@1 | 304 | * "com.sun.tools.javac" in the {@linkplain |
duke@1 | 305 | * StandardLocation#SOURCE_PATH SOURCE_PATH} location, this method |
duke@1 | 306 | * might be called like so: |
duke@1 | 307 | * |
duke@1 | 308 | * <pre>getFileForInput(SOURCE_PATH, "com.sun.tools.javac", "resources/compiler.properties");</pre> |
duke@1 | 309 | * |
duke@1 | 310 | * <p>If the call was executed on Windows, with SOURCE_PATH set to |
duke@1 | 311 | * <code>"C:\Documents and Settings\UncleBob\src\share\classes"</code>, |
duke@1 | 312 | * a valid result would be a file object representing the file |
duke@1 | 313 | * <code>"C:\Documents and Settings\UncleBob\src\share\classes\com\sun\tools\javac\resources\compiler.properties"</code>. |
duke@1 | 314 | * |
duke@1 | 315 | * @param location a location |
duke@1 | 316 | * @param packageName a package name |
duke@1 | 317 | * @param relativeName a relative name |
duke@1 | 318 | * @return a file object, might return {@code null} if the file |
duke@1 | 319 | * does not exist |
duke@1 | 320 | * @throws IllegalArgumentException if the location is not known |
duke@1 | 321 | * to this file manager and the file manager does not support |
duke@1 | 322 | * unknown locations, or if {@code relativeName} is not valid |
duke@1 | 323 | * @throws IOException if an I/O error occurred, or if {@link |
duke@1 | 324 | * #close} has been called and this file manager cannot be |
duke@1 | 325 | * reopened |
duke@1 | 326 | * @throws IllegalStateException if {@link #close} has been called |
duke@1 | 327 | * and this file manager cannot be reopened |
duke@1 | 328 | */ |
duke@1 | 329 | FileObject getFileForInput(Location location, |
duke@1 | 330 | String packageName, |
duke@1 | 331 | String relativeName) |
duke@1 | 332 | throws IOException; |
duke@1 | 333 | |
duke@1 | 334 | /** |
duke@1 | 335 | * Gets a {@linkplain FileObject file object} for output |
duke@1 | 336 | * representing the specified <a href="JavaFileManager.html#relative_name">relative |
duke@1 | 337 | * name</a> in the specified package in the given location. |
duke@1 | 338 | * |
duke@1 | 339 | * <p>Optionally, this file manager might consider the sibling as |
duke@1 | 340 | * a hint for where to place the output. The exact semantics of |
jjg@582 | 341 | * this hint is unspecified. The JDK compiler, javac, for |
duke@1 | 342 | * example, will place class files in the same directories as |
duke@1 | 343 | * originating source files unless a class file output directory |
duke@1 | 344 | * is provided. To facilitate this behavior, javac might provide |
duke@1 | 345 | * the originating source file as sibling when calling this |
duke@1 | 346 | * method. |
duke@1 | 347 | * |
duke@1 | 348 | * <p>If the returned object represents a {@linkplain |
duke@1 | 349 | * JavaFileObject.Kind#SOURCE source} or {@linkplain |
duke@1 | 350 | * JavaFileObject.Kind#CLASS class} file, it must be an instance |
duke@1 | 351 | * of {@link JavaFileObject}. |
duke@1 | 352 | * |
duke@1 | 353 | * <p>Informally, the file object returned by this method is |
duke@1 | 354 | * located in the concatenation of the location, package name, and |
duke@1 | 355 | * relative name or next to the sibling argument. See {@link |
duke@1 | 356 | * #getFileForInput getFileForInput} for an example. |
duke@1 | 357 | * |
duke@1 | 358 | * @param location a location |
duke@1 | 359 | * @param packageName a package name |
duke@1 | 360 | * @param relativeName a relative name |
duke@1 | 361 | * @param sibling a file object to be used as hint for placement; |
duke@1 | 362 | * might be {@code null} |
duke@1 | 363 | * @return a file object |
duke@1 | 364 | * @throws IllegalArgumentException if sibling is not known to |
duke@1 | 365 | * this file manager, or if the location is not known to this file |
duke@1 | 366 | * manager and the file manager does not support unknown |
duke@1 | 367 | * locations, or if {@code relativeName} is not valid |
duke@1 | 368 | * @throws IOException if an I/O error occurred, or if {@link |
duke@1 | 369 | * #close} has been called and this file manager cannot be |
duke@1 | 370 | * reopened |
duke@1 | 371 | * @throws IllegalStateException if {@link #close} has been called |
duke@1 | 372 | * and this file manager cannot be reopened |
duke@1 | 373 | */ |
duke@1 | 374 | FileObject getFileForOutput(Location location, |
duke@1 | 375 | String packageName, |
duke@1 | 376 | String relativeName, |
duke@1 | 377 | FileObject sibling) |
duke@1 | 378 | throws IOException; |
duke@1 | 379 | |
duke@1 | 380 | /** |
duke@1 | 381 | * Flushes any resources opened for output by this file manager |
duke@1 | 382 | * directly or indirectly. Flushing a closed file manager has no |
duke@1 | 383 | * effect. |
duke@1 | 384 | * |
duke@1 | 385 | * @throws IOException if an I/O error occurred |
duke@1 | 386 | * @see #close |
duke@1 | 387 | */ |
duke@1 | 388 | void flush() throws IOException; |
duke@1 | 389 | |
duke@1 | 390 | /** |
duke@1 | 391 | * Releases any resources opened by this file manager directly or |
duke@1 | 392 | * indirectly. This might render this file manager useless and |
duke@1 | 393 | * the effect of subsequent calls to methods on this object or any |
duke@1 | 394 | * objects obtained through this object is undefined unless |
duke@1 | 395 | * explicitly allowed. However, closing a file manager which has |
duke@1 | 396 | * already been closed has no effect. |
duke@1 | 397 | * |
duke@1 | 398 | * @throws IOException if an I/O error occurred |
duke@1 | 399 | * @see #flush |
duke@1 | 400 | */ |
duke@1 | 401 | void close() throws IOException; |
duke@1 | 402 | } |