Path: blob/master/src/java.compiler/share/classes/javax/tools/JavaFileManager.java
41152 views
/*1* Copyright (c) 2005, 2020, 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.ServiceLoader;32import java.util.Set;3334import static javax.tools.JavaFileObject.Kind;3536/**37* File manager for tools operating on Java programming language38* source and class files. In this context, <em>file</em> means an39* abstraction of regular files and other sources of data.40*41* <p>When constructing new JavaFileObjects, the file manager must42* determine where to create them. For example, if a file manager43* manages regular files on a file system, it would most likely have a44* current/working directory to use as default location when creating45* or finding files. A number of hints can be provided to a file46* manager as to where to create files. Any file manager might choose47* to ignore these hints.48*49* <p>Some methods in this interface use class names. Such class50* names must be given in the Java Virtual Machine internal form of51* fully qualified class and interface names. For convenience '.'52* and '/' are interchangeable. The internal form is defined in53* chapter four of54* <cite>The Java Virtual Machine Specification</cite>.5556* <blockquote><p>57* <i>Discussion:</i> this means that the names58* "java/lang.package-info", "java/lang/package-info",59* "java.lang.package-info", are valid and equivalent. Compare to60* binary name as defined in61* <cite>The Java Language Specification</cite>,62* section 13.1 "The Form of a Binary".63* </p></blockquote>64*65* <p>The case of names is significant. All names should be treated66* as case-sensitive. For example, some file systems have67* case-insensitive, case-aware file names. File objects representing68* such files should take care to preserve case by using {@link69* java.io.File#getCanonicalFile} or similar means. If the system is70* not case-aware, file objects must use other means to preserve case.71*72* <p><em><a id="relative_name">Relative names</a>:</em> some73* methods in this interface use relative names. A relative name is a74* non-null, non-empty sequence of path segments separated by '/'.75* '.' or '..' are invalid path segments. A valid relative name must76* match the "path-rootless" rule of <a77* href="http://www.ietf.org/rfc/rfc3986.txt">RFC 3986</a>,78* section 3.3. Informally, this should be true:79*80* <!-- URI.create(relativeName).normalize().getPath().equals(relativeName) -->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>82*83* <p>All methods in this interface might throw a SecurityException.84*85* <p>An object of this interface is not required to support86* multi-threaded access, that is, be synchronized. However, it must87* support concurrent access to different file objects created by this88* object.89*90* <p><em>Implementation note:</em> a consequence of this requirement91* is that a trivial implementation of output to a {@linkplain92* java.util.jar.JarOutputStream} is not a sufficient implementation.93* That is, rather than creating a JavaFileObject that returns the94* JarOutputStream directly, the contents must be cached until closed95* and then written to the JarOutputStream.96*97* <p>Unless explicitly allowed, all methods in this interface might98* throw a NullPointerException if given a {@code null} argument.99*100* @author Peter von der Ahé101* @author Jonathan Gibbons102* @see JavaFileObject103* @see FileObject104* @since 1.6105*/106public interface JavaFileManager extends Closeable, Flushable, OptionChecker {107108/**109* Interface for locations of file objects. Used by file managers110* to determine where to place or search for file objects.111*112* <p>Informally, a {@code Location} corresponds to a "search path", such as a class113* path or module path, as used by command-line tools that use the default file system.114*115* <p>Some locations are typically used to identify a place in which116* a tool can find files to be read; others are typically used to identify117* a place where a tool can write files. If a location is used to identify118* a place for reading files, those files may be organized in a simple119* <em>package/class</em> hierarchy: such locations are described as120* <strong>package-oriented</strong>.121* Alternatively, the files may be organized in a <em>module/package/class</em>122* hierarchy: such locations are described as <strong>module-oriented</strong>.123* If a location is typically used to identify a place where a tool can write files,124* it is up to the tool that writes the files to specify how those files will be125* organized.126*127* <p>You can access the classes in a package-oriented location using methods like128* {@link JavaFileManager#getJavaFileForInput} or {@link JavaFileManager#list}.129* It is not possible to directly list the classes in a module-oriented130* location. Instead, you can get a package-oriented location for any specific module131* using methods like {@link JavaFileManager#getLocationForModule} or132* {@link JavaFileManager#listLocationsForModules}.133*/134interface Location {135/**136* Returns the name of this location.137*138* @return a name139*/140String getName();141142/**143* Determines if this is an output location.144* An output location is a location that is conventionally used for145* output.146*147* @apiNote An output location may be used to write files in either148* a package-oriented organization or in a module-oriented organization.149*150* @return true if this is an output location, false otherwise151*/152boolean isOutputLocation();153154/**155* Indicates if this location is module-oriented location, and therefore156* expected to contain classes in a <em>module/package/class</em>157* hierarchy, as compared to a package-oriented location, which158* is expected to contain classes in a <em>package/class</em> hierarchy.159* The result of this method is undefined if this is an output160* location.161*162* @implNote This implementation returns true if the name includes163* the word "MODULE".164*165* @return true if this location is expected to contain modules166* @since 9167*/168default boolean isModuleOrientedLocation() {169return getName().matches("\\bMODULE\\b");170}171}172173/**174* Returns a class loader for loading plug-ins from the given175* package-oriented location.176* For example, to load annotation processors,177* a compiler will request a class loader for the {@link178* StandardLocation#ANNOTATION_PROCESSOR_PATH179* ANNOTATION_PROCESSOR_PATH} location.180*181* @param location a location182* @return a class loader for the given location; or {@code null}183* if loading plug-ins from the given location is disabled or if184* the location is not known185* @throws SecurityException if a class loader can not be created186* in the current security context187* @throws IllegalStateException if {@link #close} has been called188* and this file manager cannot be reopened189* @throws IllegalArgumentException if the location is a module-oriented location190*/191ClassLoader getClassLoader(Location location);192193/**194* Lists all file objects matching the given criteria in the given195* package-oriented location.196* List file objects in "subpackages" if recurse is true.197*198* <p>Note: even if the given location is unknown to this file199* manager, it may not return {@code null}. Also, an unknown200* location may not cause an exception.201*202* @param location a location203* @param packageName a package name204* @param kinds return objects only of these kinds205* @param recurse if true include "subpackages"206* @return an Iterable of file objects matching the given criteria207* @throws IOException if an I/O error occurred, or if {@link208* #close} has been called and this file manager cannot be209* reopened210* @throws IllegalArgumentException if the location is a module-oriented location211* @throws IllegalStateException if {@link #close} has been called212* and this file manager cannot be reopened213*/214Iterable<JavaFileObject> list(Location location,215String packageName,216Set<Kind> kinds,217boolean recurse)218throws IOException;219220/**221* Infers a binary name of a file object based on a package-oriented location.222* The binary name returned might not be a valid binary name according to223* <cite>The Java Language Specification</cite>.224*225* @param location a location226* @param file a file object227* @return a binary name or {@code null} the file object is not228* found in the given location229* @throws IllegalArgumentException if the location is a module-oriented location230* @throws IllegalStateException if {@link #close} has been called231* and this file manager cannot be reopened232*/233String inferBinaryName(Location location, JavaFileObject file);234235/**236* Compares two file objects and return true if they represent the237* same underlying object.238*239* @param a a file object240* @param b a file object241* @return true if the given file objects represent the same242* underlying object243*244* @throws IllegalArgumentException if either of the arguments245* were created with another file manager and this file manager246* does not support foreign file objects247*/248boolean isSameFile(FileObject a, FileObject b);249250/**251* Handles one option. If {@code current} is an option to this252* file manager it will consume any arguments to that option from253* {@code remaining} and return true, otherwise return false.254*255* @param current current option256* @param remaining remaining options257* @return true if this option was handled by this file manager,258* false otherwise259* @throws IllegalArgumentException if this option to this file260* manager is used incorrectly261* @throws IllegalStateException if {@link #close} has been called262* and this file manager cannot be reopened263*/264boolean handleOption(String current, Iterator<String> remaining);265266/**267* Determines if a location is known to this file manager.268*269* @param location a location270* @return true if the location is known271*/272boolean hasLocation(Location location);273274/**275* Returns a {@linkplain JavaFileObject file object} for input276* representing the specified class of the specified kind in the277* given package-oriented location.278*279* @param location a location280* @param className the name of a class281* @param kind the kind of file, must be one of {@link282* JavaFileObject.Kind#SOURCE SOURCE} or {@link283* JavaFileObject.Kind#CLASS CLASS}284* @return a file object, might return {@code null} if the285* file does not exist286* @throws IllegalArgumentException if the location is not known287* to this file manager and the file manager does not support288* unknown locations, or if the kind is not valid, or if the289* location is a module-oriented location290* @throws IOException if an I/O error occurred, or if {@link291* #close} has been called and this file manager cannot be292* reopened293* @throws IllegalStateException if {@link #close} has been called294* and this file manager cannot be reopened295*/296JavaFileObject getJavaFileForInput(Location location,297String className,298Kind kind)299throws IOException;300301/**302* Returns a {@linkplain JavaFileObject file object} for output303* representing the specified class of the specified kind in the304* given package-oriented location.305*306* <p>Optionally, this file manager might consider the sibling as307* a hint for where to place the output. The exact semantics of308* this hint is unspecified. The JDK compiler, javac, for309* example, will place class files in the same directories as310* originating source files unless a class file output directory311* is provided. To facilitate this behavior, javac might provide312* the originating source file as sibling when calling this313* method.314*315* @param location a package-oriented location316* @param className the name of a class317* @param kind the kind of file, must be one of {@link318* JavaFileObject.Kind#SOURCE SOURCE} or {@link319* JavaFileObject.Kind#CLASS CLASS}320* @param sibling a file object to be used as hint for placement;321* might be {@code null}322* @return a file object for output323* @throws IllegalArgumentException if sibling is not known to324* this file manager, or if the location is not known to this file325* manager and the file manager does not support unknown326* locations, or if the kind is not valid, or if the location is327* not an output location328* @throws IOException if an I/O error occurred, or if {@link329* #close} has been called and this file manager cannot be330* reopened331* @throws IllegalStateException {@link #close} has been called332* and this file manager cannot be reopened333*/334JavaFileObject getJavaFileForOutput(Location location,335String className,336Kind kind,337FileObject sibling)338throws IOException;339340/**341* Returns a {@linkplain FileObject file object} for input342* representing the specified <a href="JavaFileManager.html#relative_name">relative343* name</a> in the specified package in the given package-oriented location.344*345* <p>If the returned object represents a {@linkplain346* JavaFileObject.Kind#SOURCE source} or {@linkplain347* JavaFileObject.Kind#CLASS class} file, it must be an instance348* of {@link JavaFileObject}.349*350* <p>Informally, the file object returned by this method is351* located in the concatenation of the location, package name, and352* relative name. For example, to locate the properties file353* "resources/compiler.properties" in the package354* "com.sun.tools.javac" in the {@linkplain355* StandardLocation#SOURCE_PATH SOURCE_PATH} location, this method356* might be called like so:357*358* <pre>getFileForInput(SOURCE_PATH, "com.sun.tools.javac", "resources/compiler.properties");</pre>359*360* <p>If the call was executed on Windows, with SOURCE_PATH set to361* <code>"C:\Documents and Settings\UncleBob\src\share\classes"</code>,362* a valid result would be a file object representing the file363* <code>"C:\Documents and Settings\UncleBob\src\share\classes\com\sun\tools\javac\resources\compiler.properties"</code>.364*365* @param location a package-oriented location366* @param packageName a package name367* @param relativeName a relative name368* @return a file object, might return {@code null} if the file369* does not exist370* @throws IllegalArgumentException if the location is not known371* to this file manager and the file manager does not support372* unknown locations, or if {@code relativeName} is not valid,373* or if the location is a module-oriented location374* @throws IOException if an I/O error occurred, or if {@link375* #close} has been called and this file manager cannot be376* reopened377* @throws IllegalStateException if {@link #close} has been called378* and this file manager cannot be reopened379*/380FileObject getFileForInput(Location location,381String packageName,382String relativeName)383throws IOException;384385/**386* Returns a {@linkplain FileObject file object} for output387* representing the specified <a href="JavaFileManager.html#relative_name">relative388* name</a> in the specified package in the given location.389*390* <p>Optionally, this file manager might consider the sibling as391* a hint for where to place the output. The exact semantics of392* this hint is unspecified. The JDK compiler, javac, for393* example, will place class files in the same directories as394* originating source files unless a class file output directory395* is provided. To facilitate this behavior, javac might provide396* the originating source file as sibling when calling this397* method.398*399* <p>If the returned object represents a {@linkplain400* JavaFileObject.Kind#SOURCE source} or {@linkplain401* JavaFileObject.Kind#CLASS class} file, it must be an instance402* of {@link JavaFileObject}.403*404* <p>Informally, the file object returned by this method is405* located in the concatenation of the location, package name, and406* relative name or next to the sibling argument. See {@link407* #getFileForInput getFileForInput} for an example.408*409* @param location an output location410* @param packageName a package name411* @param relativeName a relative name412* @param sibling a file object to be used as hint for placement;413* might be {@code null}414* @return a file object415* @throws IllegalArgumentException if sibling is not known to416* this file manager, or if the location is not known to this file417* manager and the file manager does not support unknown418* locations, or if {@code relativeName} is not valid,419* or if the location is not an output location420* @throws IOException if an I/O error occurred, or if {@link421* #close} has been called and this file manager cannot be422* reopened423* @throws IllegalStateException if {@link #close} has been called424* and this file manager cannot be reopened425*/426FileObject getFileForOutput(Location location,427String packageName,428String relativeName,429FileObject sibling)430throws IOException;431432/**433* Flushes any resources opened for output by this file manager434* directly or indirectly. Flushing a closed file manager has no435* effect.436*437* @throws IOException if an I/O error occurred438* @see #close439*/440@Override441void flush() throws IOException;442443/**444* Releases any resources opened by this file manager directly or445* indirectly. This might render this file manager useless and446* the effect of subsequent calls to methods on this object or any447* objects obtained through this object is undefined unless448* explicitly allowed. However, closing a file manager which has449* already been closed has no effect.450*451* @throws IOException if an I/O error occurred452* @see #flush453*/454@Override455void close() throws IOException;456457/**458* Gets a location for a named module within a location, which may be either459* a module-oriented location or an output location.460* The result will be an output location if the given location is461* an output location, or it will be a package-oriented location.462*463* @implSpec This implementation throws {@code UnsupportedOperationException}.464*465* @param location the module-oriented location466* @param moduleName the name of the module to be found467* @return the location for the named module468*469* @throws IOException if an I/O error occurred470* @throws UnsupportedOperationException if this operation if not supported by this file manager471* @throws IllegalArgumentException if the location is neither an output location nor a472* module-oriented location473* @since 9474*/ // TODO: describe failure modes475default Location getLocationForModule(Location location, String moduleName) throws IOException {476throw new UnsupportedOperationException();477}478479/**480* Gets a location for the module containing a specific file481* to be found within a location, which may be either482* a module-oriented location or an output location.483* The result will be an output location if the given location is484* an output location, or it will be a package-oriented location.485*486* @implSpec This implementation throws {@code UnsupportedOperationException}.487*488* @param location the module-oriented location489* @param fo the file490* @return the module containing the file491*492* @throws IOException if an I/O error occurred493* @throws UnsupportedOperationException if this operation if not supported by this file manager494* @throws IllegalArgumentException if the location is neither an output location nor a495* module-oriented location496* @since 9497*/498default Location getLocationForModule(Location location, JavaFileObject fo) throws IOException {499throw new UnsupportedOperationException();500}501502/**503* Get a service loader for a specific service class from a given location.504*505* If the location is a module-oriented location, the service loader will use the506* service declarations in the modules found in that location. Otherwise, a service loader507* is created using the package-oriented location, in which case, the services are508* determined using the provider-configuration files in {@code META-INF/services}.509*510* @implSpec This implementation throws {@code UnsupportedOperationException}.511*512* @param location the module-oriented location513* @param service the {@code Class} object of the service class514* @param <S> the service class515* @return a service loader for the given service class516*517* @throws IOException if an I/O error occurred518* @throws UnsupportedOperationException if this operation if not supported by this file manager519* @since 9520*/ // TODO: describe failure modes521default <S> ServiceLoader<S> getServiceLoader(Location location, Class<S> service) throws IOException {522throw new UnsupportedOperationException();523}524525/**526* Infer the name of the module from its location, as returned by527* {@code getLocationForModule} or {@code listModuleLocations}.528*529* @implSpec This implementation throws {@code UnsupportedOperationException}.530*531* @param location a package-oriented location representing a module532* @return the name of the module533*534* @throws IOException if an I/O error occurred535* @throws UnsupportedOperationException if this operation if not supported by this file manager536* @throws IllegalArgumentException if the location is not one known to this file manager537* @since 9538*/ // TODO: describe failure modes539default String inferModuleName(Location location) throws IOException {540throw new UnsupportedOperationException();541}542543/**544* Lists the locations for all the modules in a module-oriented location or an output location.545* The locations that are returned will be output locations if the given location is an output,546* or it will be a package-oriented locations.547*548* @implSpec This implementation throws {@code UnsupportedOperationException}.549*550* @param location the module-oriented location for which to list the modules551* @return a series of sets of locations containing modules552*553* @throws IOException if an I/O error occurred554* @throws UnsupportedOperationException if this operation if not supported by this file manager555* @throws IllegalArgumentException if the location is not a module-oriented location556* @since 9557*/ // TODO: describe failure modes558default Iterable<Set<Location>> listLocationsForModules(Location location) throws IOException {559throw new UnsupportedOperationException();560}561562/**563* Determines whether or not a given file object is "contained in" a specified location.564*565* <p>For a package-oriented location, a file object is contained in the location if there exist566* values for <i>packageName</i> and <i>relativeName</i> such that either of the following567* calls would return the {@link #isSameFile same} file object:568* <pre>569* getFileForInput(location, <i>packageName</i>, <i>relativeName</i>)570* getFileForOutput(location, <i>packageName</i>, <i>relativeName</i>, null)571* </pre>572*573* <p>For a module-oriented location, a file object is contained in the location if there exists574* a module that may be obtained by the call:575* <pre>576* getLocationForModule(location, <i>moduleName</i>)577* </pre>578* such that the file object is contained in the (package-oriented) location for that module.579*580* @implSpec This implementation throws {@code UnsupportedOperationException}.581*582* @param location the location583* @param fo the file object584* @return whether or not the file is contained in the location585*586* @throws IOException if there is a problem determining the result587* @throws UnsupportedOperationException if the method is not supported588*589* @since 9590*/591592default boolean contains(Location location, FileObject fo) throws IOException {593throw new UnsupportedOperationException();594}595596}597598599