Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/src/share/classes/javax/annotation/processing/Filer.java
38913 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.annotation.processing;2627import javax.tools.JavaFileManager;28import javax.tools.*;29import javax.lang.model.element.Element;30import java.io.IOException;3132/**33* This interface supports the creation of new files by an annotation34* processor. Files created in this way will be known to the35* annotation processing tool implementing this interface, better36* enabling the tool to manage them. Source and class files so37* created will be {@linkplain RoundEnvironment#getRootElements38* considered for processing} by the tool in a subsequent {@linkplain39* RoundEnvironment round of processing} after the {@code close}40* method has been called on the {@code Writer} or {@code41* OutputStream} used to write the contents of the file.42*43* Three kinds of files are distinguished: source files, class files,44* and auxiliary resource files.45*46* <p> There are two distinguished supported locations (subtrees47* within the logical file system) where newly created files are48* placed: one for {@linkplain49* javax.tools.StandardLocation#SOURCE_OUTPUT new source files}, and50* one for {@linkplain javax.tools.StandardLocation#CLASS_OUTPUT new51* class files}. (These might be specified on a tool's command line,52* for example, using flags such as {@code -s} and {@code -d}.) The53* actual locations for new source files and new class files may or54* may not be distinct on a particular run of the tool. Resource55* files may be created in either location. The methods for reading56* and writing resources take a relative name argument. A relative57* name is a non-null, non-empty sequence of path segments separated58* by {@code '/'}; {@code '.'} and {@code '..'} are invalid path59* segments. A valid relative name must match the60* "path-rootless" rule of <a61* href="http://www.ietf.org/rfc/rfc3986.txt">RFC 3986</a>, section62* 3.3.63*64* <p>The file creation methods take a variable number of arguments to65* allow the <em>originating elements</em> to be provided as hints to66* the tool infrastructure to better manage dependencies. The67* originating elements are the types or packages (representing {@code68* package-info} files) which caused an annotation processor to69* attempt to create a new file. For example, if an annotation70* processor tries to create a source file, {@code71* GeneratedFromUserSource}, in response to processing72*73* <blockquote><pre>74* @Generate75* public class UserSource {}76* </pre></blockquote>77*78* the type element for {@code UserSource} should be passed as part of79* the creation method call as in:80*81* <blockquote><pre>82* filer.createSourceFile("GeneratedFromUserSource",83* eltUtils.getTypeElement("UserSource"));84* </pre></blockquote>85*86* If there are no originating elements, none need to be passed. This87* information may be used in an incremental environment to determine88* the need to rerun processors or remove generated files.89* Non-incremental environments may ignore the originating element90* information.91*92* <p> During each run of an annotation processing tool, a file with a93* given pathname may be created only once. If that file already94* exists before the first attempt to create it, the old contents will95* be deleted. Any subsequent attempt to create the same file during96* a run will throw a {@link FilerException}, as will attempting to97* create both a class file and source file for the same type name or98* same package name. The {@linkplain Processor initial inputs} to99* the tool are considered to be created by the zeroth round;100* therefore, attempting to create a source or class file101* corresponding to one of those inputs will result in a {@link102* FilerException}.103*104* <p> In general, processors must not knowingly attempt to overwrite105* existing files that were not generated by some processor. A {@code106* Filer} may reject attempts to open a file corresponding to an107* existing type, like {@code java.lang.Object}. Likewise, the108* invoker of the annotation processing tool must not knowingly109* configure the tool such that the discovered processors will attempt110* to overwrite existing files that were not generated.111*112* <p> Processors can indicate a source or class file is generated by113* including an {@link javax.annotation.Generated @Generated}114* annotation.115*116* <p> Note that some of the effect of overwriting a file can be117* achieved by using a <i>decorator</i>-style pattern. Instead of118* modifying a class directly, the class is designed so that either119* its superclass is generated by annotation processing or subclasses120* of the class are generated by annotation processing. If the121* subclasses are generated, the parent class may be designed to use122* factories instead of public constructors so that only subclass123* instances would be presented to clients of the parent class.124*125* @author Joseph D. Darcy126* @author Scott Seligman127* @author Peter von der Ahé128* @since 1.6129*/130public interface Filer {131/**132* Creates a new source file and returns an object to allow133* writing to it. The file's name and path (relative to the134* {@linkplain StandardLocation#SOURCE_OUTPUT root output location135* for source files}) are based on the type to be declared in that136* file. If more than one type is being declared, the name of the137* principal top-level type (the public one, for example) should138* be used. A source file can also be created to hold information139* about a package, including package annotations. To create a140* source file for a named package, have {@code name} be the141* package's name followed by {@code ".package-info"}; to create a142* source file for an unnamed package, use {@code "package-info"}.143*144* <p> Note that to use a particular {@linkplain145* java.nio.charset.Charset charset} to encode the contents of the146* file, an {@code OutputStreamWriter} with the chosen charset can147* be created from the {@code OutputStream} from the returned148* object. If the {@code Writer} from the returned object is149* directly used for writing, its charset is determined by the150* implementation. An annotation processing tool may have an151* {@code -encoding} flag or analogous option for specifying this;152* otherwise, it will typically be the platform's default153* encoding.154*155* <p>To avoid subsequent errors, the contents of the source file156* should be compatible with the {@linkplain157* ProcessingEnvironment#getSourceVersion source version} being used158* for this run.159*160* @param name canonical (fully qualified) name of the principal type161* being declared in this file or a package name followed by162* {@code ".package-info"} for a package information file163* @param originatingElements type or package elements causally164* associated with the creation of this file, may be elided or165* {@code null}166* @return a {@code JavaFileObject} to write the new source file167* @throws FilerException if the same pathname has already been168* created, the same type has already been created, or the name is169* not valid for a type170* @throws IOException if the file cannot be created171*/172JavaFileObject createSourceFile(CharSequence name,173Element... originatingElements) throws IOException;174175/**176* Creates a new class file, and returns an object to allow177* writing to it. The file's name and path (relative to the178* {@linkplain StandardLocation#CLASS_OUTPUT root output location179* for class files}) are based on the name of the type being180* written. A class file can also be created to hold information181* about a package, including package annotations. To create a182* class file for a named package, have {@code name} be the183* package's name followed by {@code ".package-info"}; creating a184* class file for an unnamed package is not supported.185*186* <p>To avoid subsequent errors, the contents of the class file187* should be compatible with the {@linkplain188* ProcessingEnvironment#getSourceVersion source version} being used189* for this run.190*191* @param name binary name of the type being written or a package name followed by192* {@code ".package-info"} for a package information file193* @param originatingElements type or package elements causally194* associated with the creation of this file, may be elided or195* {@code null}196* @return a {@code JavaFileObject} to write the new class file197* @throws FilerException if the same pathname has already been198* created, the same type has already been created, or the name is199* not valid for a type200* @throws IOException if the file cannot be created201*/202JavaFileObject createClassFile(CharSequence name,203Element... originatingElements) throws IOException;204205/**206* Creates a new auxiliary resource file for writing and returns a207* file object for it. The file may be located along with the208* newly created source files, newly created binary files, or209* other supported location. The locations {@link210* StandardLocation#CLASS_OUTPUT CLASS_OUTPUT} and {@link211* StandardLocation#SOURCE_OUTPUT SOURCE_OUTPUT} must be212* supported. The resource may be named relative to some package213* (as are source and class files), and from there by a relative214* pathname. In a loose sense, the full pathname of the new file215* will be the concatenation of {@code location}, {@code pkg}, and216* {@code relativeName}.217*218* <p>Files created via this method are not registered for219* annotation processing, even if the full pathname of the file220* would correspond to the full pathname of a new source file221* or new class file.222*223* @param location location of the new file224* @param pkg package relative to which the file should be named,225* or the empty string if none226* @param relativeName final pathname components of the file227* @param originatingElements type or package elements causally228* associated with the creation of this file, may be elided or229* {@code null}230* @return a {@code FileObject} to write the new resource231* @throws IOException if the file cannot be created232* @throws FilerException if the same pathname has already been233* created234* @throws IllegalArgumentException for an unsupported location235* @throws IllegalArgumentException if {@code relativeName} is not relative236*/237FileObject createResource(JavaFileManager.Location location,238CharSequence pkg,239CharSequence relativeName,240Element... originatingElements) throws IOException;241242/**243* Returns an object for reading an existing resource. The244* locations {@link StandardLocation#CLASS_OUTPUT CLASS_OUTPUT}245* and {@link StandardLocation#SOURCE_OUTPUT SOURCE_OUTPUT} must246* be supported.247*248* @param location location of the file249* @param pkg package relative to which the file should be searched,250* or the empty string if none251* @param relativeName final pathname components of the file252* @return an object to read the file253* @throws FilerException if the same pathname has already been254* opened for writing255* @throws IOException if the file cannot be opened256* @throws IllegalArgumentException for an unsupported location257* @throws IllegalArgumentException if {@code relativeName} is not relative258*/259FileObject getResource(JavaFileManager.Location location,260CharSequence pkg,261CharSequence relativeName) throws IOException;262}263264265