KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > annotation > processing > Filer


1 /*
2  * @(#)Filer.java 1.10 06/08/28
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.annotation.processing;
9
10 import javax.tools.JavaFileManager;
11 import javax.tools.*;
12 import javax.lang.model.element.Element;
13 import java.io.IOException JavaDoc;
14
15 /**
16  * This interface supports the creation of new files by an annotation
17  * processor. Files created in this way will be known to the
18  * annotation processing tool implementing this interface, better
19  * enabling the tool to manage them. Source and class files so
20  * created will be considered for processing by the tool after the
21  * {@code close} method has been called on the {@code Writer} or
22  * {@code OutputStream} used to write the contents of the file.
23  *
24  * Three kinds of files are distinguished: source files, class files,
25  * and auxiliary resource files.
26  *
27  * <p> There are two distinguished supported locations (subtrees
28  * within the logical file system) where newly created files are
29  * placed: one for {@linkplain
30  * javax.tools.StandardLocation#SOURCE_OUTPUT new source files}, and
31  * one for {@linkplain javax.tools.StandardLocation#CLASS_OUTPUT new
32  * class files}. (These might be specified on a tool's command line,
33  * for example, using flags such as {@code -s} and {@code -d}.) The
34  * actual locations for new source files and new class files may or
35  * may not be distinct on a particular run of the tool. Resource
36  * files may be created in either location. The methods for reading
37  * and writing resources take a relative name argument. A relative
38  * name is a non-null, non-empty sequence of path segments separated
39  * by {@code '/'}; {@code '.'} and {@code '..'} are invalid path
40  * segments. A valid relative name must match the
41  * &quot;path-rootless&quot; rule of <a
42  * HREF="http://www.ietf.org/rfc/rfc3986.txt">RFC 3986</a>, section
43  * 3.3.
44  *
45  * <p>The file creation methods take a variable number of arguments to
46  * allow the <em>originating elements</em> to be provided as hints to
47  * the tool infrastructure to better manage dependencies. The
48  * originating elements are the types or packages (representing {@code
49  * package-info} files) which caused an annotation processor to
50  * attempt to create a new file. For example, if an annotation
51  * processor tries to create a source file, {@code
52  * GeneratedFromUserSource}, in response to processing
53  *
54  * <blockquote><pre>
55  * &#64;Generate
56  * public class UserSource {}
57  * </pre></blockquote>
58  *
59  * the type element for {@code UserSource} should be passed as part of
60  * the creation method call as in:
61  *
62  * <blockquote><pre>
63  * filer.createSourceFile("GeneratedFromUserSource",
64  * eltUtils.getTypeElement("UserSource"));
65  * </pre></blockquote>
66  *
67  * If there are no originating elements, none need to be passed. This
68  * information may be used in an incremental environment to determine
69  * the need to rerun processors or remove generated files.
70  * Non-incremental environments may ignore the originating element
71  * information.
72  *
73  * <p> During each run of an annotation processing tool, a file with a
74  * given pathname may be created only once. If that file already
75  * exists before the first attempt to create it, the old contents will
76  * be deleted. Any subsequent attempt to create the same file during
77  * a run will throw a {@link FilerException}, as will attempting to
78  * create both a class file and source file for the same type name or
79  * same package name. The {@linkplain Processor initial inputs} to
80  * the tool are considered to be created by the zeroth round;
81  * therefore, attempting to create a source or class file
82  * corresponding to one of those inputs will result in a {@link
83  * FilerException}.
84  *
85  * <p> In general, processors must not knowingly attempt to overwrite
86  * existing files that were not generated by some processor. A {@code
87  * Filer} may reject attempts to open a file corresponding to an
88  * existing type, like {@code java.lang.Object}. Likewise, the
89  * invoker of the annotation processing tool must not knowingly
90  * configure the tool such that the discovered processors will attempt
91  * to overwrite existing files that were not generated.
92  *
93  * <p> Processors can indicate a source or class file is generated by
94  * including an {@link javax.annotation.Generated @Generated}
95  * annotation.
96  *
97  * <p> Note that some of the effect of overwriting a file can be
98  * achieved by using a <i>decorator</i>-style pattern. Instead of
99  * modifying a class directly, the class is designed so that either
100  * its superclass is generated by annotation processing or subclasses
101  * of the class are generated by annotation processing. If the
102  * subclasses are generated, the parent class may be designed to use
103  * factories instead of public constructors so that only subclass
104  * instances would be presented to clients of the parent class.
105  *
106  * @author Joseph D. Darcy
107  * @author Scott Seligman
108  * @author Peter von der Ah&eacute;
109  * @version 1.10 06/08/28
110  * @since 1.6
111  */

112 public interface Filer {
113     /**
114      * Creates a new source file and returns an object to allow
115      * writing to it. The file's name and path (relative to the
116      * {@linkplain StandardLocation#SOURCE_OUTPUT root output location
117      * for source files}) are based on the type to be declared in that
118      * file. If more than one type is being declared, the name of the
119      * principal top-level type (the public one, for example) should
120      * be used. A source file can also be created to hold information
121      * about a package, including package annotations. To create a
122      * source file for a named package, have {@code name} be the
123      * package's name followed by {@code ".package-info"}; to create a
124      * source file for an unnamed package, use {@code "package-info"}.
125      *
126      * <p> Note that to use a particular {@linkplain
127      * java.nio.charset.Charset charset} to encode the contents of the
128      * file, an {@code OutputStreamWriter} with the chosen charset can
129      * be created from the {@code OutputStream} from the returned
130      * object. If the {@code Writer} from the returned object is
131      * directly used for writing, its charset is determined by the
132      * implementation. An annotation processing tool may have an
133      * {@code -encoding} flag or analogous option for specifying this;
134      * otherwise, it will typically be the platform's default
135      * encoding.
136      *
137      * <p>To avoid subsequent errors, the contents of the source file
138      * should be compatible with the {@linkplain
139      * ProcessingEnvironment#getSourceVersion source version} being used
140      * for this run.
141      *
142      * @param name canonical (fully qualified) name of the principal type
143      * being declared in this file or a package name followed by
144      * {@code ".package-info"} for a package information file
145      * @param originatingElements type or package elements causally
146      * associated with the creation of this file, may be elided or
147      * {@code null}
148      * @return a {@code JavaFileObject} to write the new source file
149      * @throws FilerException if the same pathname has already been
150      * created, the same type has already been created, or the name is
151      * not valid for a type
152      * @throws IOException if the file cannot be created
153      */

154     JavaFileObject createSourceFile(CharSequence JavaDoc name,
155                     Element... originatingElements) throws IOException JavaDoc;
156
157     /**
158      * Creates a new class file, and returns an object to allow
159      * writing to it. The file's name and path (relative to the
160      * {@linkplain StandardLocation#CLASS_OUTPUT root output location
161      * for class files}) are based on the name of the type being
162      * written. A class file can also be created to hold information
163      * about a package, including package annotations. To create a
164      * class file for a named package, have {@code name} be the
165      * package's name followed by {@code ".package-info"}; creating a
166      * class file for an unnamed package is not supported.
167      *
168      * <p>To avoid subsequent errors, the contents of the class file
169      * should be compatible with the {@linkplain
170      * ProcessingEnvironment#getSourceVersion source version} being used
171      * for this run.
172      *
173      * @param name binary name of the type being written or a package name followed by
174      * {@code ".package-info"} for a package information file
175      * @param originatingElements type or package elements causally
176      * associated with the creation of this file, may be elided or
177      * {@code null}
178      * @return a {@code JavaFileObject} to write the new class file
179      * @throws FilerException if the same pathname has already been
180      * created, the same type has already been created, or the name is
181      * not valid for a type
182      * @throws IOException if the file cannot be created
183      */

184     JavaFileObject createClassFile(CharSequence JavaDoc name,
185                    Element... originatingElements) throws IOException JavaDoc;
186
187     /**
188      * Creates a new auxiliary resource file for writing and returns a
189      * file object for it. The file may be located along with the
190      * newly created source files, newly created binary files, or
191      * other supported location. The locations {@link
192      * StandardLocation#CLASS_OUTPUT CLASS_OUTPUT} and {@link
193      * StandardLocation#SOURCE_OUTPUT SOURCE_OUTPUT} must be
194      * supported. The resource may be named relative to some package
195      * (as are source and class files), and from there by a relative
196      * pathname. In a loose sense, the full pathname of the new file
197      * will be the concatenation of {@code location}, {@code pkg}, and
198      * {@code relativeName}.
199      *
200      * <p>Files created via this method are not registered for
201      * annotation processing, even if the full pathname of the file
202      * would correspond to the full pathname of a new source file
203      * or new class file.
204      *
205      * @param location location of the new file
206      * @param pkg package relative to which the file should be named,
207      * or the empty string if none
208      * @param relativeName final pathname components of the file
209      * @param originatingElements type or package elements causally
210      * associated with the creation of this file, may be elided or
211      * {@code null}
212      * @return a {@code FileObject} to write the new resource
213      * @throws IOException if the file cannot be created
214      * @throws FilerException if the same pathname has already been
215      * created
216      * @throws IllegalArgumentException for an unsupported location
217      * @throws IllegalArgumentException if {@code relativeName} is not relative
218      */

219    FileObject createResource(JavaFileManager.Location location,
220                  CharSequence JavaDoc pkg,
221                  CharSequence JavaDoc relativeName,
222                  Element... originatingElements) throws IOException JavaDoc;
223
224     /**
225      * Returns an object for reading an existing resource. The
226      * locations {@link StandardLocation#CLASS_OUTPUT CLASS_OUTPUT}
227      * and {@link StandardLocation#SOURCE_OUTPUT SOURCE_OUTPUT} must
228      * be supported.
229      *
230      * @param location location of the file
231      * @param pkg package relative to which the file should be searched,
232      * or the empty string if none
233      * @param relativeName final pathname components of the file
234      * @return an object to read the file
235      * @throws FilerException if the same pathname has already been
236      * opened for writing
237      * @throws IOException if the file cannot be opened
238      * @throws IllegalArgumentException for an unsupported location
239      * @throws IllegalArgumentException if {@code relativeName} is not relative
240      */

241     FileObject getResource(JavaFileManager.Location location,
242                CharSequence JavaDoc pkg,
243                CharSequence JavaDoc relativeName) throws IOException JavaDoc;
244 }
245
Popular Tags