src/share/classes/javax/tools/JavaFileManager.java

Mon, 10 Jan 2011 15:08:31 -0800

author
jjg
date
Mon, 10 Jan 2011 15:08:31 -0800
changeset 816
7c537f4298fb
parent 582
366a7b9b5627
child 972
694ff82ca68e
permissions
-rw-r--r--

6396503: javac should not require assertions enabled
Reviewed-by: mcimadamore

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
duke@1 52 * chapter four of the
duke@1 53 * <a href="http://java.sun.com/docs/books/vmspec/2nd-edition/jvms-maintenance.html">Java
duke@1 54 * Virtual Machine Specification</a>.
duke@1 55
duke@1 56 * <blockquote><p>
duke@1 57 * <i>Discussion:</i> this means that the names
duke@1 58 * "java/lang.package-info", "java/lang/package-info",
duke@1 59 * "java.lang.package-info", are valid and equivalent. Compare to
duke@1 60 * binary name as defined in the
duke@1 61 * <a href="http://java.sun.com/docs/books/jls/">Java Language
duke@1 62 * Specification (JLS)</a> section 13.1 "The Form of a Binary".
duke@1 63 * </p></blockquote>
duke@1 64 *
duke@1 65 * <p>The case of names is significant. All names should be treated
duke@1 66 * as case-sensitive. For example, some file systems have
duke@1 67 * case-insensitive, case-aware file names. File objects representing
duke@1 68 * such files should take care to preserve case by using {@link
duke@1 69 * java.io.File#getCanonicalFile} or similar means. If the system is
duke@1 70 * not case-aware, file objects must use other means to preserve case.
duke@1 71 *
duke@1 72 * <p><em><a name="relative_name">Relative names</a>:</em> some
duke@1 73 * methods in this interface use relative names. A relative name is a
duke@1 74 * non-null, non-empty sequence of path segments separated by '/'.
duke@1 75 * '.' or '..' are invalid path segments. A valid relative name must
duke@1 76 * match the "path-rootless" rule of <a
duke@1 77 * href="http://www.ietf.org/rfc/rfc3986.txt">RFC&nbsp;3986</a>,
duke@1 78 * section&nbsp;3.3. Informally, this should be true:
duke@1 79 *
duke@1 80 * <!-- URI.create(relativeName).normalize().getPath().equals(relativeName) -->
duke@1 81 * <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 82 *
duke@1 83 * <p>All methods in this interface might throw a SecurityException.
duke@1 84 *
duke@1 85 * <p>An object of this interface is not required to support
duke@1 86 * multi-threaded access, that is, be synchronized. However, it must
duke@1 87 * support concurrent access to different file objects created by this
duke@1 88 * object.
duke@1 89 *
duke@1 90 * <p><em>Implementation note:</em> a consequence of this requirement
duke@1 91 * is that a trivial implementation of output to a {@linkplain
duke@1 92 * java.util.jar.JarOutputStream} is not a sufficient implementation.
duke@1 93 * That is, rather than creating a JavaFileObject that returns the
duke@1 94 * JarOutputStream directly, the contents must be cached until closed
duke@1 95 * and then written to the JarOutputStream.
duke@1 96 *
duke@1 97 * <p>Unless explicitly allowed, all methods in this interface might
duke@1 98 * throw a NullPointerException if given a {@code null} argument.
duke@1 99 *
duke@1 100 * @author Peter von der Ah&eacute;
duke@1 101 * @author Jonathan Gibbons
duke@1 102 * @see JavaFileObject
duke@1 103 * @see FileObject
duke@1 104 * @since 1.6
duke@1 105 */
duke@1 106 public interface JavaFileManager extends Closeable, Flushable, OptionChecker {
duke@1 107
duke@1 108 /**
duke@1 109 * Interface for locations of file objects. Used by file managers
duke@1 110 * to determine where to place or search for file objects.
duke@1 111 */
duke@1 112 interface Location {
duke@1 113 /**
duke@1 114 * Gets the name of this location.
duke@1 115 *
duke@1 116 * @return a name
duke@1 117 */
duke@1 118 String getName();
duke@1 119
duke@1 120 /**
duke@1 121 * Determines if this is an output location. An output
duke@1 122 * location is a location that is conventionally used for
duke@1 123 * output.
duke@1 124 *
duke@1 125 * @return true if this is an output location, false otherwise
duke@1 126 */
duke@1 127 boolean isOutputLocation();
duke@1 128 }
duke@1 129
duke@1 130 /**
duke@1 131 * Gets a class loader for loading plug-ins from the given
duke@1 132 * location. For example, to load annotation processors, a
duke@1 133 * compiler will request a class loader for the {@link
duke@1 134 * StandardLocation#ANNOTATION_PROCESSOR_PATH
duke@1 135 * ANNOTATION_PROCESSOR_PATH} location.
duke@1 136 *
duke@1 137 * @param location a location
duke@1 138 * @return a class loader for the given location; or {@code null}
duke@1 139 * if loading plug-ins from the given location is disabled or if
duke@1 140 * the location is not known
duke@1 141 * @throws SecurityException if a class loader can not be created
duke@1 142 * in the current security context
duke@1 143 * @throws IllegalStateException if {@link #close} has been called
duke@1 144 * and this file manager cannot be reopened
duke@1 145 */
duke@1 146 ClassLoader getClassLoader(Location location);
duke@1 147
duke@1 148 /**
duke@1 149 * Lists all file objects matching the given criteria in the given
duke@1 150 * location. List file objects in "subpackages" if recurse is
duke@1 151 * true.
duke@1 152 *
duke@1 153 * <p>Note: even if the given location is unknown to this file
duke@1 154 * manager, it may not return {@code null}. Also, an unknown
duke@1 155 * location may not cause an exception.
duke@1 156 *
duke@1 157 * @param location a location
duke@1 158 * @param packageName a package name
duke@1 159 * @param kinds return objects only of these kinds
duke@1 160 * @param recurse if true include "subpackages"
duke@1 161 * @return an Iterable of file objects matching the given criteria
duke@1 162 * @throws IOException if an I/O error occurred, or if {@link
duke@1 163 * #close} has been called and this file manager cannot be
duke@1 164 * reopened
duke@1 165 * @throws IllegalStateException if {@link #close} has been called
duke@1 166 * and this file manager cannot be reopened
duke@1 167 */
duke@1 168 Iterable<JavaFileObject> list(Location location,
duke@1 169 String packageName,
duke@1 170 Set<Kind> kinds,
duke@1 171 boolean recurse)
duke@1 172 throws IOException;
duke@1 173
duke@1 174 /**
duke@1 175 * Infers a binary name of a file object based on a location. The
duke@1 176 * binary name returned might not be a valid JLS binary name.
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&nbsp;and&nbsp;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&nbsp;and&nbsp;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 }

mercurial