Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/src/share/classes/javax/tools/JavaFileManager.java
38900 views
/*1* Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.tools;2627import java.io.Closeable;28import java.io.Flushable;29import java.io.IOException;30import java.util.Iterator;31import java.util.Set;32import static javax.tools.JavaFileObject.Kind;3334/**35* File manager for tools operating on Java™ programming language36* source and class files. In this context, <em>file</em> means an37* abstraction of regular files and other sources of data.38*39* <p>When constructing new JavaFileObjects, the file manager must40* determine where to create them. For example, if a file manager41* manages regular files on a file system, it would most likely have a42* current/working directory to use as default location when creating43* or finding files. A number of hints can be provided to a file44* manager as to where to create files. Any file manager might choose45* to ignore these hints.46*47* <p>Some methods in this interface use class names. Such class48* names must be given in the Java Virtual Machine internal form of49* fully qualified class and interface names. For convenience '.'50* and '/' are interchangeable. The internal form is defined in51* chapter four of52* <cite>The Java™ Virtual Machine Specification</cite>.5354* <blockquote><p>55* <i>Discussion:</i> this means that the names56* "java/lang.package-info", "java/lang/package-info",57* "java.lang.package-info", are valid and equivalent. Compare to58* binary name as defined in59* <cite>The Java™ Language Specification</cite>,60* section 13.1 "The Form of a Binary".61* </p></blockquote>62*63* <p>The case of names is significant. All names should be treated64* as case-sensitive. For example, some file systems have65* case-insensitive, case-aware file names. File objects representing66* such files should take care to preserve case by using {@link67* java.io.File#getCanonicalFile} or similar means. If the system is68* not case-aware, file objects must use other means to preserve case.69*70* <p><em><a name="relative_name">Relative names</a>:</em> some71* methods in this interface use relative names. A relative name is a72* non-null, non-empty sequence of path segments separated by '/'.73* '.' or '..' are invalid path segments. A valid relative name must74* match the "path-rootless" rule of <a75* href="http://www.ietf.org/rfc/rfc3986.txt">RFC 3986</a>,76* section 3.3. Informally, this should be true:77*78* <!-- URI.create(relativeName).normalize().getPath().equals(relativeName) -->79* <pre> URI.{@linkplain java.net.URI#create create}(relativeName).{@linkplain java.net.URI#normalize normalize}().{@linkplain java.net.URI#getPath getPath}().equals(relativeName)</pre>80*81* <p>All methods in this interface might throw a SecurityException.82*83* <p>An object of this interface is not required to support84* multi-threaded access, that is, be synchronized. However, it must85* support concurrent access to different file objects created by this86* object.87*88* <p><em>Implementation note:</em> a consequence of this requirement89* is that a trivial implementation of output to a {@linkplain90* java.util.jar.JarOutputStream} is not a sufficient implementation.91* That is, rather than creating a JavaFileObject that returns the92* JarOutputStream directly, the contents must be cached until closed93* and then written to the JarOutputStream.94*95* <p>Unless explicitly allowed, all methods in this interface might96* throw a NullPointerException if given a {@code null} argument.97*98* @author Peter von der Ahé99* @author Jonathan Gibbons100* @see JavaFileObject101* @see FileObject102* @since 1.6103*/104public interface JavaFileManager extends Closeable, Flushable, OptionChecker {105106/**107* Interface for locations of file objects. Used by file managers108* to determine where to place or search for file objects.109*/110interface Location {111/**112* Gets the name of this location.113*114* @return a name115*/116String getName();117118/**119* Determines if this is an output location. An output120* location is a location that is conventionally used for121* output.122*123* @return true if this is an output location, false otherwise124*/125boolean isOutputLocation();126}127128/**129* Gets a class loader for loading plug-ins from the given130* location. For example, to load annotation processors, a131* compiler will request a class loader for the {@link132* StandardLocation#ANNOTATION_PROCESSOR_PATH133* ANNOTATION_PROCESSOR_PATH} location.134*135* @param location a location136* @return a class loader for the given location; or {@code null}137* if loading plug-ins from the given location is disabled or if138* the location is not known139* @throws SecurityException if a class loader can not be created140* in the current security context141* @throws IllegalStateException if {@link #close} has been called142* and this file manager cannot be reopened143*/144ClassLoader getClassLoader(Location location);145146/**147* Lists all file objects matching the given criteria in the given148* location. List file objects in "subpackages" if recurse is149* true.150*151* <p>Note: even if the given location is unknown to this file152* manager, it may not return {@code null}. Also, an unknown153* location may not cause an exception.154*155* @param location a location156* @param packageName a package name157* @param kinds return objects only of these kinds158* @param recurse if true include "subpackages"159* @return an Iterable of file objects matching the given criteria160* @throws IOException if an I/O error occurred, or if {@link161* #close} has been called and this file manager cannot be162* reopened163* @throws IllegalStateException if {@link #close} has been called164* and this file manager cannot be reopened165*/166Iterable<JavaFileObject> list(Location location,167String packageName,168Set<Kind> kinds,169boolean recurse)170throws IOException;171172/**173* Infers a binary name of a file object based on a location. The174* binary name returned might not be a valid binary name according to175* <cite>The Java™ Language Specification</cite>.176*177* @param location a location178* @param file a file object179* @return a binary name or {@code null} the file object is not180* found in the given location181* @throws IllegalStateException if {@link #close} has been called182* and this file manager cannot be reopened183*/184String inferBinaryName(Location location, JavaFileObject file);185186/**187* Compares two file objects and return true if they represent the188* same underlying object.189*190* @param a a file object191* @param b a file object192* @return true if the given file objects represent the same193* underlying object194*195* @throws IllegalArgumentException if either of the arguments196* were created with another file manager and this file manager197* does not support foreign file objects198*/199boolean isSameFile(FileObject a, FileObject b);200201/**202* Handles one option. If {@code current} is an option to this203* file manager it will consume any arguments to that option from204* {@code remaining} and return true, otherwise return false.205*206* @param current current option207* @param remaining remaining options208* @return true if this option was handled by this file manager,209* false otherwise210* @throws IllegalArgumentException if this option to this file211* manager is used incorrectly212* @throws IllegalStateException if {@link #close} has been called213* and this file manager cannot be reopened214*/215boolean handleOption(String current, Iterator<String> remaining);216217/**218* Determines if a location is known to this file manager.219*220* @param location a location221* @return true if the location is known222*/223boolean hasLocation(Location location);224225/**226* Gets a {@linkplain JavaFileObject file object} for input227* representing the specified class of the specified kind in the228* given location.229*230* @param location a location231* @param className the name of a class232* @param kind the kind of file, must be one of {@link233* JavaFileObject.Kind#SOURCE SOURCE} or {@link234* JavaFileObject.Kind#CLASS CLASS}235* @return a file object, might return {@code null} if the236* file does not exist237* @throws IllegalArgumentException if the location is not known238* to this file manager and the file manager does not support239* unknown locations, or if the kind is not valid240* @throws IOException if an I/O error occurred, or if {@link241* #close} has been called and this file manager cannot be242* reopened243* @throws IllegalStateException if {@link #close} has been called244* and this file manager cannot be reopened245*/246JavaFileObject getJavaFileForInput(Location location,247String className,248Kind kind)249throws IOException;250251/**252* Gets a {@linkplain JavaFileObject file object} for output253* representing the specified class of the specified kind in the254* given location.255*256* <p>Optionally, this file manager might consider the sibling as257* a hint for where to place the output. The exact semantics of258* this hint is unspecified. The JDK compiler, javac, for259* example, will place class files in the same directories as260* originating source files unless a class file output directory261* is provided. To facilitate this behavior, javac might provide262* the originating source file as sibling when calling this263* method.264*265* @param location a location266* @param className the name of a class267* @param kind the kind of file, must be one of {@link268* JavaFileObject.Kind#SOURCE SOURCE} or {@link269* JavaFileObject.Kind#CLASS CLASS}270* @param sibling a file object to be used as hint for placement;271* might be {@code null}272* @return a file object for output273* @throws IllegalArgumentException if sibling is not known to274* this file manager, or if the location is not known to this file275* manager and the file manager does not support unknown276* locations, or if the kind is not valid277* @throws IOException if an I/O error occurred, or if {@link278* #close} has been called and this file manager cannot be279* reopened280* @throws IllegalStateException {@link #close} has been called281* and this file manager cannot be reopened282*/283JavaFileObject getJavaFileForOutput(Location location,284String className,285Kind kind,286FileObject sibling)287throws IOException;288289/**290* Gets a {@linkplain FileObject file object} for input291* representing the specified <a href="JavaFileManager.html#relative_name">relative292* name</a> in the specified package in the given location.293*294* <p>If the returned object represents a {@linkplain295* JavaFileObject.Kind#SOURCE source} or {@linkplain296* JavaFileObject.Kind#CLASS class} file, it must be an instance297* of {@link JavaFileObject}.298*299* <p>Informally, the file object returned by this method is300* located in the concatenation of the location, package name, and301* relative name. For example, to locate the properties file302* "resources/compiler.properties" in the package303* "com.sun.tools.javac" in the {@linkplain304* StandardLocation#SOURCE_PATH SOURCE_PATH} location, this method305* might be called like so:306*307* <pre>getFileForInput(SOURCE_PATH, "com.sun.tools.javac", "resources/compiler.properties");</pre>308*309* <p>If the call was executed on Windows, with SOURCE_PATH set to310* <code>"C:\Documents and Settings\UncleBob\src\share\classes"</code>,311* a valid result would be a file object representing the file312* <code>"C:\Documents and Settings\UncleBob\src\share\classes\com\sun\tools\javac\resources\compiler.properties"</code>.313*314* @param location a location315* @param packageName a package name316* @param relativeName a relative name317* @return a file object, might return {@code null} if the file318* does not exist319* @throws IllegalArgumentException if the location is not known320* to this file manager and the file manager does not support321* unknown locations, or if {@code relativeName} is not valid322* @throws IOException if an I/O error occurred, or if {@link323* #close} has been called and this file manager cannot be324* reopened325* @throws IllegalStateException if {@link #close} has been called326* and this file manager cannot be reopened327*/328FileObject getFileForInput(Location location,329String packageName,330String relativeName)331throws IOException;332333/**334* Gets a {@linkplain FileObject file object} for output335* representing the specified <a href="JavaFileManager.html#relative_name">relative336* name</a> in the specified package in the given location.337*338* <p>Optionally, this file manager might consider the sibling as339* a hint for where to place the output. The exact semantics of340* this hint is unspecified. The JDK compiler, javac, for341* example, will place class files in the same directories as342* originating source files unless a class file output directory343* is provided. To facilitate this behavior, javac might provide344* the originating source file as sibling when calling this345* method.346*347* <p>If the returned object represents a {@linkplain348* JavaFileObject.Kind#SOURCE source} or {@linkplain349* JavaFileObject.Kind#CLASS class} file, it must be an instance350* of {@link JavaFileObject}.351*352* <p>Informally, the file object returned by this method is353* located in the concatenation of the location, package name, and354* relative name or next to the sibling argument. See {@link355* #getFileForInput getFileForInput} for an example.356*357* @param location a location358* @param packageName a package name359* @param relativeName a relative name360* @param sibling a file object to be used as hint for placement;361* might be {@code null}362* @return a file object363* @throws IllegalArgumentException if sibling is not known to364* this file manager, or if the location is not known to this file365* manager and the file manager does not support unknown366* locations, or if {@code relativeName} is not valid367* @throws IOException if an I/O error occurred, or if {@link368* #close} has been called and this file manager cannot be369* reopened370* @throws IllegalStateException if {@link #close} has been called371* and this file manager cannot be reopened372*/373FileObject getFileForOutput(Location location,374String packageName,375String relativeName,376FileObject sibling)377throws IOException;378379/**380* Flushes any resources opened for output by this file manager381* directly or indirectly. Flushing a closed file manager has no382* effect.383*384* @throws IOException if an I/O error occurred385* @see #close386*/387void flush() throws IOException;388389/**390* Releases any resources opened by this file manager directly or391* indirectly. This might render this file manager useless and392* the effect of subsequent calls to methods on this object or any393* objects obtained through this object is undefined unless394* explicitly allowed. However, closing a file manager which has395* already been closed has no effect.396*397* @throws IOException if an I/O error occurred398* @see #flush399*/400void close() throws IOException;401}402403404