close
method has been called on the Writer or OutputStream used to write the contents of the file.
Three kinds of files are distinguished: source files, class files,
and auxiliary resource files.
There are two distinguished supported locations (subtrees
within the logical file system) where newly created files are
placed: one for new source files, and
one for new
class files. (These might be specified on a tool's command line,
for example, using flags such as -s and -d.) The
actual locations for new source files and new class files may or
may not be distinct on a particular run of the tool. Resource
files may be created in either location. The methods for reading
and writing resources take a relative name argument. A relative
name is a non-null, non-empty sequence of path segments separated
by '/'; '.' and '..' are invalid path
segments. A valid relative name must match the
"path-rootless" rule of RFC 3986, section
3.3.
The file creation methods take a variable number of arguments to
allow the originating elements to be provided as hints to
the tool infrastructure to better manage dependencies. The
originating elements are the classes or interfaces or packages
(representing package-info files) or modules (representing
module-info files) which caused an annotation processor to
attempt to create a new file.
In other words, the originating elements are intended to have the
granularity of compilation units (JLS section 7.3),
essentially file-level granularity, rather than finer-scale
granularity of, say, a method or field declaration.
For example, if an annotation
processor tries to create a source file, GeneratedFromUserSource, in response to processing
@Generate
public class UserSource {}
the type element for UserSource should be passed as part of
the creation method call as in:
filer.createSourceFile("GeneratedFromUserSource",
eltUtils.getTypeElement("UserSource"));
If there are no originating elements, none need to be passed. This
information may be used in an incremental environment to determine
the need to rerun processors or remove generated files.
Non-incremental environments may ignore the originating element
information.
During each run of an annotation processing tool, a file with a
given pathname may be created only once. If that file already
exists before the first attempt to create it, the old contents will
be deleted. Any subsequent attempt to create the same file during
a run will throw a FilerException, as will attempting to
create both a class file and source file for the same type name or
same package name. The initial inputs to
the tool are considered to be created by the zeroth round;
therefore, attempting to create a source or class file
corresponding to one of those inputs will result in a FilerException.
In general, processors must not knowingly attempt to overwrite
existing files that were not generated by some processor. A Filer may reject attempts to open a file corresponding to an
existing class or interface, like java.lang.Object. Likewise, the
invoker of the annotation processing tool must not knowingly
configure the tool such that the discovered processors will attempt
to overwrite existing files that were not generated.
Processors can indicate a source or class file is generated by
including a javax.
annotation if the environment is configured so that that class or
interface is accessible.
API Note
Some of the effect of overwriting a file can be achieved by using a decorator-style pattern. Instead of modifying a class directly, the class is designed so that either its superclass is generated by annotation processing or subclasses of the class are generated by annotation processing. If the subclasses are generated, the parent class may be designed to use factories instead of public constructors so that only subclass instances would be presented to clients of the parent class.
| Modifier and Type | Method and Description |
|---|---|
| public JavaFileObject | Returns: aJavaFileObject to write the new class filebinary name of the class or interface being written
or a package name followed by name, Element... ".package-info" for a
package information fileclass or interface or package or
module elements causally associated with the creation of this
file, may be elided or originatingElements)nullCreates a new class file, and returns an object to allow writing to it. |
| public FileObject | Returns: aFileObject to write the new resourcelocation of the new file location,module and/or package relative to which the file
should be named, or the empty string if none moduleAndPkg, CharSequence final pathname components of the file relativeName, Element... class or interface or package or
module elements causally associated with the creation of this
file, may be elided or
originatingElements)nullCreates a new auxiliary resource file for writing and returns a file object for it. |
| public JavaFileObject | Returns: aJavaFileObject to write the new source filecanonical (fully qualified) name of the principal class or interface
being declared in this file or a package name followed by
name, Element... ".package-info" for a package information fileclass, interface, package, or module
elements causally associated with the creation of this file,
may be elided or originatingElements)nullCreates a new source file and returns an object to allow writing to it. |
| public FileObject | Returns: an object to read the filelocation of the file location,module and/or package relative to which the file
should be searched for, or the empty string if none moduleAndPkg, CharSequence final pathname components of the file relativeName)Returns an object for reading an existing resource. |
| createClassFile | back to summary |
|---|---|
| public JavaFileObject createClassFile(CharSequence name, Element... originatingElements) throws IOException Creates a new class file, and returns an object to allow writing to it. A class file for a class, interface, or a package can be created. The file's name and path (relative to the root output location for class files) are based on the name of the item to be declared as well as the specified module for the item (if any). A class file can also be created to hold information about a
package, including package annotations. To create a class file
for a named package, have the The optional module name is prefixed to the type name or
package name and separated using a " If no explicit module prefix is given and modules are supported
in the environment, a suitable module is inferred. If a suitable
module cannot be inferred Creating a class file in or for an unnamed package in a named module is not supported. If the environment is configured to support implicitly declared
classes, the name argument is used to provide the leading component of the
name used for the output file. For example API Note To avoid subsequent errors, the contents of the class file should be compatible with the source version being used for this run. Implementation Note In the reference implementation, if the annotation
processing tool is processing a single module M,
then M is used as the module for files created without
an explicit module prefix. If the tool is processing multiple
modules, and
| |
| createResource | back to summary |
|---|---|
| public FileObject createResource(JavaFileManager. Creates a new auxiliary resource file for writing and returns a
file object for it. The file may be located along with the
newly created source files, newly created binary files, or
other supported location. The locations If the given location is neither a module oriented location, nor an output location containing multiple modules, and the explicit
module prefix is given, If the given location is either a module oriented location,
or an output location containing multiple modules, and no explicit
modules prefix is given, a suitable module is
inferred. If a suitable module cannot be inferred Files created via this method are not registered for annotation processing, even if the full pathname of the file would correspond to the full pathname of a new source file or new class file. Implementation Note In the reference implementation, if the annotation
processing tool is processing a single module M,
then M is used as the module for files created without
an explicit module prefix. If the tool is processing multiple
modules, and
| |
| createSourceFile | back to summary |
|---|---|
| public JavaFileObject createSourceFile(CharSequence name, Element... originatingElements) throws IOException Creates a new source file and returns an object to allow writing to it. A source file for a class, interface, or a package can be created. The file's name and path (relative to the root output location for source files) are based on the name of the item to be declared in that file as well as the specified module for the item (if any). If more than one class or interface is being declared in a single file (that is, a single compilation unit), the name of the file should correspond to the name of the principal top-level class or interface (the public one, for example). A source file can also be created to hold information about
a package, including package annotations. To create a source
file for a named package, have the The optional module name is prefixed to the type name or
package name and separated using a " If no explicit module prefix is given and modules are supported
in the environment, a suitable module is inferred. If a suitable
module cannot be inferred Creating a source file in or for an unnamed package in a named module is not supported. If the environment is configured to support implicitly declared
classes, the name argument is used to provide the leading component of the
name used for the output file. For example API Note To use a particular charset to encode the contents of the
file, an To avoid subsequent errors, the contents of the source file should be compatible with the source version being used for this run. Implementation Note In the reference implementation, if the annotation
processing tool is processing a single module M,
then M is used as the module for files created without
an explicit module prefix. If the tool is processing multiple
modules, and
| |
| getResource | back to summary |
|---|---|
| public FileObject getResource(JavaFileManager. Returns an object for reading an existing resource. The
locations If If the given location is neither a module oriented location, nor an output location containing multiple modules, and the explicit
module prefix is given, If the given location is either a module oriented location,
or an output location containing multiple modules, and no explicit
modules prefix is given, a suitable module is
inferred. If a suitable module cannot be inferred Implementation Note In the reference implementation, if the annotation
processing tool is processing a single module M,
then M is used as the module for files read without
an explicit module prefix. If the tool is processing multiple
modules, and
| |