Path: blob/master/src/java.compiler/share/classes/javax/annotation/processing/Filer.java
41159 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.annotation.processing;2627import javax.tools.JavaFileManager;28import javax.tools.*;29import javax.lang.model.element.Element;30import javax.lang.model.util.Elements;31import java.io.IOException;3233/**34* This interface supports the creation of new files by an annotation35* processor. Files created in this way will be known to the36* annotation processing tool implementing this interface, better37* enabling the tool to manage them. Source and class files so38* created will be {@linkplain RoundEnvironment#getRootElements39* considered for processing} by the tool in a subsequent {@linkplain40* RoundEnvironment round of processing} after the {@code close}41* method has been called on the {@code Writer} or {@code42* OutputStream} used to write the contents of the file.43*44* Three kinds of files are distinguished: source files, class files,45* and auxiliary resource files.46*47* <p> There are two distinguished supported locations (subtrees48* within the logical file system) where newly created files are49* placed: one for {@linkplain50* javax.tools.StandardLocation#SOURCE_OUTPUT new source files}, and51* one for {@linkplain javax.tools.StandardLocation#CLASS_OUTPUT new52* class files}. (These might be specified on a tool's command line,53* for example, using flags such as {@code -s} and {@code -d}.) The54* actual locations for new source files and new class files may or55* may not be distinct on a particular run of the tool. Resource56* files may be created in either location. The methods for reading57* and writing resources take a relative name argument. A relative58* name is a non-null, non-empty sequence of path segments separated59* by {@code '/'}; {@code '.'} and {@code '..'} are invalid path60* segments. A valid relative name must match the61* "path-rootless" rule of <a62* href="http://www.ietf.org/rfc/rfc3986.txt">RFC 3986</a>, section63* 3.3.64*65* <p>The file creation methods take a variable number of arguments to66* allow the <em>originating elements</em> to be provided as hints to67* the tool infrastructure to better manage dependencies. The68* originating elements are the classes or interfaces or packages69* (representing {@code package-info} files) or modules (representing70* {@code module-info} files) which caused an annotation processor to71* attempt to create a new file. For example, if an annotation72* processor tries to create a source file, {@code73* GeneratedFromUserSource}, in response to processing74*75* <blockquote><pre>76* @Generate77* public class UserSource {}78* </pre></blockquote>79*80* the type element for {@code UserSource} should be passed as part of81* the creation method call as in:82*83* <blockquote><pre>84* filer.createSourceFile("GeneratedFromUserSource",85* eltUtils.getTypeElement("UserSource"));86* </pre></blockquote>87*88* If there are no originating elements, none need to be passed. This89* information may be used in an incremental environment to determine90* the need to rerun processors or remove generated files.91* Non-incremental environments may ignore the originating element92* information.93*94* <p> During each run of an annotation processing tool, a file with a95* given pathname may be created only once. If that file already96* exists before the first attempt to create it, the old contents will97* be deleted. Any subsequent attempt to create the same file during98* a run will throw a {@link FilerException}, as will attempting to99* create both a class file and source file for the same type name or100* same package name. The {@linkplain Processor initial inputs} to101* the tool are considered to be created by the zeroth round;102* therefore, attempting to create a source or class file103* corresponding to one of those inputs will result in a {@link104* FilerException}.105*106* <p> In general, processors must not knowingly attempt to overwrite107* existing files that were not generated by some processor. A {@code108* Filer} may reject attempts to open a file corresponding to an109* existing class or interface, like {@code java.lang.Object}. Likewise, the110* invoker of the annotation processing tool must not knowingly111* configure the tool such that the discovered processors will attempt112* to overwrite existing files that were not generated.113*114* <p> Processors can indicate a source or class file is generated by115* including a {@link javax.annotation.processing.Generated}116* annotation if the environment is configured so that that class or117* interface is accessible.118*119* @apiNote Some of the effect of overwriting a file can be120* achieved by using a <i>decorator</i>-style pattern. Instead of121* modifying a class directly, the class is designed so that either122* its superclass is generated by annotation processing or subclasses123* of the class are generated by annotation processing. If the124* subclasses are generated, the parent class may be designed to use125* factories instead of public constructors so that only subclass126* instances would be presented to clients of the parent class.127*128* @author Joseph D. Darcy129* @author Scott Seligman130* @author Peter von der Ahé131* @since 1.6132*/133public interface Filer {134/**135* Creates a new source file and returns an object to allow136* writing to it. A source file for a class, interface, or a137* package can be created.138*139* The file's name and path (relative to the {@linkplain140* StandardLocation#SOURCE_OUTPUT root output location for source141* files}) are based on the name of the item to be declared in142* that file as well as the specified module for the item (if143* any).144*145* If more than one class or interface is being declared in a single file (that146* is, a single compilation unit), the name of the file should147* correspond to the name of the principal top-level class or interface (the148* public one, for example).149*150* <p>A source file can also be created to hold information about151* a package, including package annotations. To create a source152* file for a named package, have the {@code name} argument be the153* package's name followed by {@code ".package-info"}; to create a154* source file for an unnamed package, use {@code "package-info"}.155*156* <p>The optional module name is prefixed to the type name or157* package name and separated using a "{@code /}" character. For158* example, to create a source file for class {@code a.B} in module159* {@code foo}, use a {@code name} argument of {@code "foo/a.B"}.160*161* <p>If no explicit module prefix is given and modules are supported162* in the environment, a suitable module is inferred. If a suitable163* module cannot be inferred {@link FilerException} is thrown.164* An implementation may use information about the configuration of165* the annotation processing tool as part of the inference.166*167* <p>Creating a source file in or for an <em>unnamed</em> package in a <em>named</em>168* module is <em>not</em> supported.169*170* @apiNote To use a particular {@linkplain171* java.nio.charset.Charset charset} to encode the contents of the172* file, an {@code OutputStreamWriter} with the chosen charset can173* be created from the {@code OutputStream} from the returned174* object. If the {@code Writer} from the returned object is175* directly used for writing, its charset is determined by the176* implementation. An annotation processing tool may have an177* {@code -encoding} flag or analogous option for specifying this;178* otherwise, it will typically be the platform's default179* encoding.180*181* <p>To avoid subsequent errors, the contents of the source file182* should be compatible with the {@linkplain183* ProcessingEnvironment#getSourceVersion source version} being used184* for this run.185*186* @implNote In the reference implementation, if the annotation187* processing tool is processing a single module <i>M</i>,188* then <i>M</i> is used as the module for files created without189* an explicit module prefix. If the tool is processing multiple190* modules, and {@link191* Elements#getPackageElement(java.lang.CharSequence)192* Elements.getPackageElement(package-of(name))}193* returns a package, the module that owns the returned package is used194* as the target module. A separate option may be used to provide the target195* module if it cannot be determined using the above rules.196*197* @param name canonical (fully qualified) name of the principal class or interface198* being declared in this file or a package name followed by199* {@code ".package-info"} for a package information file200* @param originatingElements class, interface, package, or module201* elements causally associated with the creation of this file,202* may be elided or {@code null}203* @return a {@code JavaFileObject} to write the new source file204* @throws FilerException if the same pathname has already been205* created, the same class or interface has already been created, the name is206* otherwise not valid for the entity requested to being created,207* if the target module cannot be determined, if the target208* module is not writable, or a module is specified when the environment209* doesn't support modules.210* @throws IOException if the file cannot be created211* @jls 7.3 Compilation Units212*/213JavaFileObject createSourceFile(CharSequence name,214Element... originatingElements) throws IOException;215216/**217* Creates a new class file, and returns an object to allow218* writing to it. A class file for a class, interface, or a package can219* be created.220*221* The file's name and path (relative to the {@linkplain222* StandardLocation#CLASS_OUTPUT root output location for class223* files}) are based on the name of the item to be declared as224* well as the specified module for the item (if any).225*226* <p>A class file can also be created to hold information about a227* package, including package annotations. To create a class file228* for a named package, have the {@code name} argument be the229* package's name followed by {@code ".package-info"}; creating a230* class file for an unnamed package is not supported.231*232* <p>The optional module name is prefixed to the type name or233* package name and separated using a "{@code /}" character. For234* example, to create a class file for class {@code a.B} in module235* {@code foo}, use a {@code name} argument of {@code "foo/a.B"}.236*237* <p>If no explicit module prefix is given and modules are supported238* in the environment, a suitable module is inferred. If a suitable239* module cannot be inferred {@link FilerException} is thrown.240* An implementation may use information about the configuration of241* the annotation processing tool as part of the inference.242*243* <p>Creating a class file in or for an <em>unnamed</em> package in a <em>named</em>244* module is <em>not</em> supported.245*246* @apiNote To avoid subsequent errors, the contents of the class247* file should be compatible with the {@linkplain248* ProcessingEnvironment#getSourceVersion source version} being249* used for this run.250*251* @implNote In the reference implementation, if the annotation252* processing tool is processing a single module <i>M</i>,253* then <i>M</i> is used as the module for files created without254* an explicit module prefix. If the tool is processing multiple255* modules, and {@link256* Elements#getPackageElement(java.lang.CharSequence)257* Elements.getPackageElement(package-of(name))}258* returns a package, the module that owns the returned package is used259* as the target module. A separate option may be used to provide the target260* module if it cannot be determined using the above rules.261*262* @param name binary name of the class or interface being written263* or a package name followed by {@code ".package-info"} for a264* package information file265* @param originatingElements class or interface or package or266* module elements causally associated with the creation of this267* file, may be elided or {@code null}268* @return a {@code JavaFileObject} to write the new class file269* @throws FilerException if the same pathname has already been270* created, the same class or interface has already been created, the name is271* not valid for a class or interface, if the target module cannot be determined,272* if the target module is not writable, or a module is specified when273* the environment doesn't support modules.274* @throws IOException if the file cannot be created275*/276JavaFileObject createClassFile(CharSequence name,277Element... originatingElements) throws IOException;278279/**280* Creates a new auxiliary resource file for writing and returns a281* file object for it. The file may be located along with the282* newly created source files, newly created binary files, or283* other supported location. The locations {@link284* StandardLocation#CLASS_OUTPUT CLASS_OUTPUT} and {@link285* StandardLocation#SOURCE_OUTPUT SOURCE_OUTPUT} must be286* supported. The resource may be named relative to some module287* and/or package (as are source and class files), and from there288* by a relative pathname. In a loose sense, the full pathname of289* the new file will be the concatenation of {@code location},290* {@code moduleAndPkg}, and {@code relativeName}.291*292* If {@code moduleAndPkg} contains a "{@code /}" character, the293* prefix before the "{@code /}" character is the module name and294* the suffix after the "{@code /}" character is the package295* name. The package suffix may be empty. If {@code moduleAndPkg}296* does not contain a "{@code /}" character, the entire argument297* is interpreted as a package name.298*299* <p>If the given location is neither a {@linkplain300* JavaFileManager.Location#isModuleOrientedLocation()301* module oriented location}, nor an {@linkplain302* JavaFileManager.Location#isOutputLocation()303* output location containing multiple modules}, and the explicit304* module prefix is given, {@link FilerException} is thrown.305*306* <p>If the given location is either a module oriented location,307* or an output location containing multiple modules, and no explicit308* modules prefix is given, a suitable module is309* inferred. If a suitable module cannot be inferred {@link310* FilerException} is thrown. An implementation may use information311* about the configuration of the annotation processing tool312* as part of the inference.313*314* <p>Files created via this method are <em>not</em> registered for315* annotation processing, even if the full pathname of the file316* would correspond to the full pathname of a new source file317* or new class file.318*319* @implNote In the reference implementation, if the annotation320* processing tool is processing a single module <i>M</i>,321* then <i>M</i> is used as the module for files created without322* an explicit module prefix. If the tool is processing multiple323* modules, and {@link324* Elements#getPackageElement(java.lang.CharSequence)325* Elements.getPackageElement(package-of(name))}326* returns a package, the module that owns the returned package is used327* as the target module. A separate option may be used to provide the target328* module if it cannot be determined using the above rules.329*330* @param location location of the new file331* @param moduleAndPkg module and/or package relative to which the file332* should be named, or the empty string if none333* @param relativeName final pathname components of the file334* @param originatingElements class or interface or package or335* module elements causally associated with the creation of this336* file, may be elided or337* {@code null}338* @return a {@code FileObject} to write the new resource339* @throws IOException if the file cannot be created340* @throws FilerException if the same pathname has already been341* created, if the target module cannot be determined,342* or if the target module is not writable, or if an explicit343* target module is specified and the location does not support it.344* @throws IllegalArgumentException for an unsupported location345* @throws IllegalArgumentException if {@code moduleAndPkg} is ill-formed346* @throws IllegalArgumentException if {@code relativeName} is not relative347*/348FileObject createResource(JavaFileManager.Location location,349CharSequence moduleAndPkg,350CharSequence relativeName,351Element... originatingElements) throws IOException;352353/**354* Returns an object for reading an existing resource. The355* locations {@link StandardLocation#CLASS_OUTPUT CLASS_OUTPUT}356* and {@link StandardLocation#SOURCE_OUTPUT SOURCE_OUTPUT} must357* be supported.358*359* <p>If {@code moduleAndPkg} contains a "{@code /}" character, the360* prefix before the "{@code /}" character is the module name and361* the suffix after the "{@code /}" character is the package362* name. The package suffix may be empty; however, if a module363* name is present, it must be nonempty. If {@code moduleAndPkg}364* does not contain a "{@code /}" character, the entire argument365* is interpreted as a package name.366*367* <p>If the given location is neither a {@linkplain368* JavaFileManager.Location#isModuleOrientedLocation()369* module oriented location}, nor an {@linkplain370* JavaFileManager.Location#isOutputLocation()371* output location containing multiple modules}, and the explicit372* module prefix is given, {@link FilerException} is thrown.373*374* <p>If the given location is either a module oriented location,375* or an output location containing multiple modules, and no explicit376* modules prefix is given, a suitable module is377* inferred. If a suitable module cannot be inferred {@link378* FilerException} is thrown. An implementation may use information379* about the configuration of the annotation processing tool380* as part of the inference.381*382* @implNote In the reference implementation, if the annotation383* processing tool is processing a single module <i>M</i>,384* then <i>M</i> is used as the module for files read without385* an explicit module prefix. If the tool is processing multiple386* modules, and {@link387* Elements#getPackageElement(java.lang.CharSequence)388* Elements.getPackageElement(package-of(name))}389* returns a package, the module that owns the returned package is used390* as the source module. A separate option may be used to provide the target391* module if it cannot be determined using the above rules.392*393* @param location location of the file394* @param moduleAndPkg module and/or package relative to which the file395* should be searched for, or the empty string if none396* @param relativeName final pathname components of the file397* @return an object to read the file398* @throws FilerException if the same pathname has already been399* opened for writing, if the source module cannot be determined,400* or if the target module is not writable, or if an explicit target401* module is specified and the location does not support it.402* @throws IOException if the file cannot be opened403* @throws IllegalArgumentException for an unsupported location404* @throws IllegalArgumentException if {@code moduleAndPkg} is ill-formed405* @throws IllegalArgumentException if {@code relativeName} is not relative406*/407FileObject getResource(JavaFileManager.Location location,408CharSequence moduleAndPkg,409CharSequence relativeName) throws IOException;410}411412413