Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.compiler/share/classes/javax/annotation/processing/Filer.java
41159 views
1
/*
2
* Copyright (c) 2005, 2020, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package javax.annotation.processing;
27
28
import javax.tools.JavaFileManager;
29
import javax.tools.*;
30
import javax.lang.model.element.Element;
31
import javax.lang.model.util.Elements;
32
import java.io.IOException;
33
34
/**
35
* This interface supports the creation of new files by an annotation
36
* processor. Files created in this way will be known to the
37
* annotation processing tool implementing this interface, better
38
* enabling the tool to manage them. Source and class files so
39
* created will be {@linkplain RoundEnvironment#getRootElements
40
* considered for processing} by the tool in a subsequent {@linkplain
41
* RoundEnvironment round of processing} after the {@code close}
42
* method has been called on the {@code Writer} or {@code
43
* OutputStream} used to write the contents of the file.
44
*
45
* Three kinds of files are distinguished: source files, class files,
46
* and auxiliary resource files.
47
*
48
* <p> There are two distinguished supported locations (subtrees
49
* within the logical file system) where newly created files are
50
* placed: one for {@linkplain
51
* javax.tools.StandardLocation#SOURCE_OUTPUT new source files}, and
52
* one for {@linkplain javax.tools.StandardLocation#CLASS_OUTPUT new
53
* class files}. (These might be specified on a tool's command line,
54
* for example, using flags such as {@code -s} and {@code -d}.) The
55
* actual locations for new source files and new class files may or
56
* may not be distinct on a particular run of the tool. Resource
57
* files may be created in either location. The methods for reading
58
* and writing resources take a relative name argument. A relative
59
* name is a non-null, non-empty sequence of path segments separated
60
* by {@code '/'}; {@code '.'} and {@code '..'} are invalid path
61
* segments. A valid relative name must match the
62
* &quot;path-rootless&quot; rule of <a
63
* href="http://www.ietf.org/rfc/rfc3986.txt">RFC&nbsp;3986</a>, section
64
* 3.3.
65
*
66
* <p>The file creation methods take a variable number of arguments to
67
* allow the <em>originating elements</em> to be provided as hints to
68
* the tool infrastructure to better manage dependencies. The
69
* originating elements are the classes or interfaces or packages
70
* (representing {@code package-info} files) or modules (representing
71
* {@code module-info} files) which caused an annotation processor to
72
* attempt to create a new file. For example, if an annotation
73
* processor tries to create a source file, {@code
74
* GeneratedFromUserSource}, in response to processing
75
*
76
* <blockquote><pre>
77
* &#64;Generate
78
* public class UserSource {}
79
* </pre></blockquote>
80
*
81
* the type element for {@code UserSource} should be passed as part of
82
* the creation method call as in:
83
*
84
* <blockquote><pre>
85
* filer.createSourceFile("GeneratedFromUserSource",
86
* eltUtils.getTypeElement("UserSource"));
87
* </pre></blockquote>
88
*
89
* If there are no originating elements, none need to be passed. This
90
* information may be used in an incremental environment to determine
91
* the need to rerun processors or remove generated files.
92
* Non-incremental environments may ignore the originating element
93
* information.
94
*
95
* <p> During each run of an annotation processing tool, a file with a
96
* given pathname may be created only once. If that file already
97
* exists before the first attempt to create it, the old contents will
98
* be deleted. Any subsequent attempt to create the same file during
99
* a run will throw a {@link FilerException}, as will attempting to
100
* create both a class file and source file for the same type name or
101
* same package name. The {@linkplain Processor initial inputs} to
102
* the tool are considered to be created by the zeroth round;
103
* therefore, attempting to create a source or class file
104
* corresponding to one of those inputs will result in a {@link
105
* FilerException}.
106
*
107
* <p> In general, processors must not knowingly attempt to overwrite
108
* existing files that were not generated by some processor. A {@code
109
* Filer} may reject attempts to open a file corresponding to an
110
* existing class or interface, like {@code java.lang.Object}. Likewise, the
111
* invoker of the annotation processing tool must not knowingly
112
* configure the tool such that the discovered processors will attempt
113
* to overwrite existing files that were not generated.
114
*
115
* <p> Processors can indicate a source or class file is generated by
116
* including a {@link javax.annotation.processing.Generated}
117
* annotation if the environment is configured so that that class or
118
* interface is accessible.
119
*
120
* @apiNote Some of the effect of overwriting a file can be
121
* achieved by using a <i>decorator</i>-style pattern. Instead of
122
* modifying a class directly, the class is designed so that either
123
* its superclass is generated by annotation processing or subclasses
124
* of the class are generated by annotation processing. If the
125
* subclasses are generated, the parent class may be designed to use
126
* factories instead of public constructors so that only subclass
127
* instances would be presented to clients of the parent class.
128
*
129
* @author Joseph D. Darcy
130
* @author Scott Seligman
131
* @author Peter von der Ah&eacute;
132
* @since 1.6
133
*/
134
public interface Filer {
135
/**
136
* Creates a new source file and returns an object to allow
137
* writing to it. A source file for a class, interface, or a
138
* package can be created.
139
*
140
* The file's name and path (relative to the {@linkplain
141
* StandardLocation#SOURCE_OUTPUT root output location for source
142
* files}) are based on the name of the item to be declared in
143
* that file as well as the specified module for the item (if
144
* any).
145
*
146
* If more than one class or interface is being declared in a single file (that
147
* is, a single compilation unit), the name of the file should
148
* correspond to the name of the principal top-level class or interface (the
149
* public one, for example).
150
*
151
* <p>A source file can also be created to hold information about
152
* a package, including package annotations. To create a source
153
* file for a named package, have the {@code name} argument be the
154
* package's name followed by {@code ".package-info"}; to create a
155
* source file for an unnamed package, use {@code "package-info"}.
156
*
157
* <p>The optional module name is prefixed to the type name or
158
* package name and separated using a "{@code /}" character. For
159
* example, to create a source file for class {@code a.B} in module
160
* {@code foo}, use a {@code name} argument of {@code "foo/a.B"}.
161
*
162
* <p>If no explicit module prefix is given and modules are supported
163
* in the environment, a suitable module is inferred. If a suitable
164
* module cannot be inferred {@link FilerException} is thrown.
165
* An implementation may use information about the configuration of
166
* the annotation processing tool as part of the inference.
167
*
168
* <p>Creating a source file in or for an <em>unnamed</em> package in a <em>named</em>
169
* module is <em>not</em> supported.
170
*
171
* @apiNote To use a particular {@linkplain
172
* java.nio.charset.Charset charset} to encode the contents of the
173
* file, an {@code OutputStreamWriter} with the chosen charset can
174
* be created from the {@code OutputStream} from the returned
175
* object. If the {@code Writer} from the returned object is
176
* directly used for writing, its charset is determined by the
177
* implementation. An annotation processing tool may have an
178
* {@code -encoding} flag or analogous option for specifying this;
179
* otherwise, it will typically be the platform's default
180
* encoding.
181
*
182
* <p>To avoid subsequent errors, the contents of the source file
183
* should be compatible with the {@linkplain
184
* ProcessingEnvironment#getSourceVersion source version} being used
185
* for this run.
186
*
187
* @implNote In the reference implementation, if the annotation
188
* processing tool is processing a single module <i>M</i>,
189
* then <i>M</i> is used as the module for files created without
190
* an explicit module prefix. If the tool is processing multiple
191
* modules, and {@link
192
* Elements#getPackageElement(java.lang.CharSequence)
193
* Elements.getPackageElement(package-of(name))}
194
* returns a package, the module that owns the returned package is used
195
* as the target module. A separate option may be used to provide the target
196
* module if it cannot be determined using the above rules.
197
*
198
* @param name canonical (fully qualified) name of the principal class or interface
199
* being declared in this file or a package name followed by
200
* {@code ".package-info"} for a package information file
201
* @param originatingElements class, interface, package, or module
202
* elements causally associated with the creation of this file,
203
* may be elided or {@code null}
204
* @return a {@code JavaFileObject} to write the new source file
205
* @throws FilerException if the same pathname has already been
206
* created, the same class or interface has already been created, the name is
207
* otherwise not valid for the entity requested to being created,
208
* if the target module cannot be determined, if the target
209
* module is not writable, or a module is specified when the environment
210
* doesn't support modules.
211
* @throws IOException if the file cannot be created
212
* @jls 7.3 Compilation Units
213
*/
214
JavaFileObject createSourceFile(CharSequence name,
215
Element... originatingElements) throws IOException;
216
217
/**
218
* Creates a new class file, and returns an object to allow
219
* writing to it. A class file for a class, interface, or a package can
220
* be created.
221
*
222
* The file's name and path (relative to the {@linkplain
223
* StandardLocation#CLASS_OUTPUT root output location for class
224
* files}) are based on the name of the item to be declared as
225
* well as the specified module for the item (if any).
226
*
227
* <p>A class file can also be created to hold information about a
228
* package, including package annotations. To create a class file
229
* for a named package, have the {@code name} argument be the
230
* package's name followed by {@code ".package-info"}; creating a
231
* class file for an unnamed package is not supported.
232
*
233
* <p>The optional module name is prefixed to the type name or
234
* package name and separated using a "{@code /}" character. For
235
* example, to create a class file for class {@code a.B} in module
236
* {@code foo}, use a {@code name} argument of {@code "foo/a.B"}.
237
*
238
* <p>If no explicit module prefix is given and modules are supported
239
* in the environment, a suitable module is inferred. If a suitable
240
* module cannot be inferred {@link FilerException} is thrown.
241
* An implementation may use information about the configuration of
242
* the annotation processing tool as part of the inference.
243
*
244
* <p>Creating a class file in or for an <em>unnamed</em> package in a <em>named</em>
245
* module is <em>not</em> supported.
246
*
247
* @apiNote To avoid subsequent errors, the contents of the class
248
* file should be compatible with the {@linkplain
249
* ProcessingEnvironment#getSourceVersion source version} being
250
* used for this run.
251
*
252
* @implNote In the reference implementation, if the annotation
253
* processing tool is processing a single module <i>M</i>,
254
* then <i>M</i> is used as the module for files created without
255
* an explicit module prefix. If the tool is processing multiple
256
* modules, and {@link
257
* Elements#getPackageElement(java.lang.CharSequence)
258
* Elements.getPackageElement(package-of(name))}
259
* returns a package, the module that owns the returned package is used
260
* as the target module. A separate option may be used to provide the target
261
* module if it cannot be determined using the above rules.
262
*
263
* @param name binary name of the class or interface being written
264
* or a package name followed by {@code ".package-info"} for a
265
* package information file
266
* @param originatingElements class or interface or package or
267
* module elements causally associated with the creation of this
268
* file, may be elided or {@code null}
269
* @return a {@code JavaFileObject} to write the new class file
270
* @throws FilerException if the same pathname has already been
271
* created, the same class or interface has already been created, the name is
272
* not valid for a class or interface, if the target module cannot be determined,
273
* if the target module is not writable, or a module is specified when
274
* the environment doesn't support modules.
275
* @throws IOException if the file cannot be created
276
*/
277
JavaFileObject createClassFile(CharSequence name,
278
Element... originatingElements) throws IOException;
279
280
/**
281
* Creates a new auxiliary resource file for writing and returns a
282
* file object for it. The file may be located along with the
283
* newly created source files, newly created binary files, or
284
* other supported location. The locations {@link
285
* StandardLocation#CLASS_OUTPUT CLASS_OUTPUT} and {@link
286
* StandardLocation#SOURCE_OUTPUT SOURCE_OUTPUT} must be
287
* supported. The resource may be named relative to some module
288
* and/or package (as are source and class files), and from there
289
* by a relative pathname. In a loose sense, the full pathname of
290
* the new file will be the concatenation of {@code location},
291
* {@code moduleAndPkg}, and {@code relativeName}.
292
*
293
* If {@code moduleAndPkg} contains a "{@code /}" character, the
294
* prefix before the "{@code /}" character is the module name and
295
* the suffix after the "{@code /}" character is the package
296
* name. The package suffix may be empty. If {@code moduleAndPkg}
297
* does not contain a "{@code /}" character, the entire argument
298
* is interpreted as a package name.
299
*
300
* <p>If the given location is neither a {@linkplain
301
* JavaFileManager.Location#isModuleOrientedLocation()
302
* module oriented location}, nor an {@linkplain
303
* JavaFileManager.Location#isOutputLocation()
304
* output location containing multiple modules}, and the explicit
305
* module prefix is given, {@link FilerException} is thrown.
306
*
307
* <p>If the given location is either a module oriented location,
308
* or an output location containing multiple modules, and no explicit
309
* modules prefix is given, a suitable module is
310
* inferred. If a suitable module cannot be inferred {@link
311
* FilerException} is thrown. An implementation may use information
312
* about the configuration of the annotation processing tool
313
* as part of the inference.
314
*
315
* <p>Files created via this method are <em>not</em> registered for
316
* annotation processing, even if the full pathname of the file
317
* would correspond to the full pathname of a new source file
318
* or new class file.
319
*
320
* @implNote In the reference implementation, if the annotation
321
* processing tool is processing a single module <i>M</i>,
322
* then <i>M</i> is used as the module for files created without
323
* an explicit module prefix. If the tool is processing multiple
324
* modules, and {@link
325
* Elements#getPackageElement(java.lang.CharSequence)
326
* Elements.getPackageElement(package-of(name))}
327
* returns a package, the module that owns the returned package is used
328
* as the target module. A separate option may be used to provide the target
329
* module if it cannot be determined using the above rules.
330
*
331
* @param location location of the new file
332
* @param moduleAndPkg module and/or package relative to which the file
333
* should be named, or the empty string if none
334
* @param relativeName final pathname components of the file
335
* @param originatingElements class or interface or package or
336
* module elements causally associated with the creation of this
337
* file, may be elided or
338
* {@code null}
339
* @return a {@code FileObject} to write the new resource
340
* @throws IOException if the file cannot be created
341
* @throws FilerException if the same pathname has already been
342
* created, if the target module cannot be determined,
343
* or if the target module is not writable, or if an explicit
344
* target module is specified and the location does not support it.
345
* @throws IllegalArgumentException for an unsupported location
346
* @throws IllegalArgumentException if {@code moduleAndPkg} is ill-formed
347
* @throws IllegalArgumentException if {@code relativeName} is not relative
348
*/
349
FileObject createResource(JavaFileManager.Location location,
350
CharSequence moduleAndPkg,
351
CharSequence relativeName,
352
Element... originatingElements) throws IOException;
353
354
/**
355
* Returns an object for reading an existing resource. The
356
* locations {@link StandardLocation#CLASS_OUTPUT CLASS_OUTPUT}
357
* and {@link StandardLocation#SOURCE_OUTPUT SOURCE_OUTPUT} must
358
* be supported.
359
*
360
* <p>If {@code moduleAndPkg} contains a "{@code /}" character, the
361
* prefix before the "{@code /}" character is the module name and
362
* the suffix after the "{@code /}" character is the package
363
* name. The package suffix may be empty; however, if a module
364
* name is present, it must be nonempty. If {@code moduleAndPkg}
365
* does not contain a "{@code /}" character, the entire argument
366
* is interpreted as a package name.
367
*
368
* <p>If the given location is neither a {@linkplain
369
* JavaFileManager.Location#isModuleOrientedLocation()
370
* module oriented location}, nor an {@linkplain
371
* JavaFileManager.Location#isOutputLocation()
372
* output location containing multiple modules}, and the explicit
373
* module prefix is given, {@link FilerException} is thrown.
374
*
375
* <p>If the given location is either a module oriented location,
376
* or an output location containing multiple modules, and no explicit
377
* modules prefix is given, a suitable module is
378
* inferred. If a suitable module cannot be inferred {@link
379
* FilerException} is thrown. An implementation may use information
380
* about the configuration of the annotation processing tool
381
* as part of the inference.
382
*
383
* @implNote In the reference implementation, if the annotation
384
* processing tool is processing a single module <i>M</i>,
385
* then <i>M</i> is used as the module for files read without
386
* an explicit module prefix. If the tool is processing multiple
387
* modules, and {@link
388
* Elements#getPackageElement(java.lang.CharSequence)
389
* Elements.getPackageElement(package-of(name))}
390
* returns a package, the module that owns the returned package is used
391
* as the source module. A separate option may be used to provide the target
392
* module if it cannot be determined using the above rules.
393
*
394
* @param location location of the file
395
* @param moduleAndPkg module and/or package relative to which the file
396
* should be searched for, or the empty string if none
397
* @param relativeName final pathname components of the file
398
* @return an object to read the file
399
* @throws FilerException if the same pathname has already been
400
* opened for writing, if the source module cannot be determined,
401
* or if the target module is not writable, or if an explicit target
402
* module is specified and the location does not support it.
403
* @throws IOException if the file cannot be opened
404
* @throws IllegalArgumentException for an unsupported location
405
* @throws IllegalArgumentException if {@code moduleAndPkg} is ill-formed
406
* @throws IllegalArgumentException if {@code relativeName} is not relative
407
*/
408
FileObject getResource(JavaFileManager.Location location,
409
CharSequence moduleAndPkg,
410
CharSequence relativeName) throws IOException;
411
}
412
413