public class JavacFiler extends Object implements Filer, Closeable
createSourceFile("foo.Bar") ... createSourceFile("foo.Bar")is disallowed as is opening a text file that happens to have the same name as a source file:
createSourceFile("foo.Bar") ... createTextFile(SOURCE_TREE, "foo", new File("Bar"), null)
Additionally, creating a source file that corresponds to an already created class file (or vice versa) also results in an IOException since each type can only be created once. However, if the Filer is used to create a text file named *.java that happens to correspond to an existing class file, a warning is *not* generated. Similarly, a warning is not generated for a binary file named *.class and an existing source file.
The reason for this difference is that source files and class files are registered with the tool and can get passed on as declarations to the next round of processing. Files that are just named *.java and *.class are not processed in that manner; although having extra source files and class files on the source path and class path can alter the behavior of the tool and any final compile.
This is NOT part of any supported API. If you write code that depends on this, you do so at your own risk. This code and its internal interfaces are subject to change or deletion without notice.
Modifier and Type | Method and Description |
---|---|
void |
close() |
JavaFileObject |
createClassFile(CharSequence name,
Element... originatingElements)
Creates a new class file, and returns an object to allow
writing to it.
|
FileObject |
createResource(JavaFileManager.Location location,
CharSequence pkg,
CharSequence relativeName,
Element... originatingElements)
Creates a new auxiliary resource file for writing and returns a
file object for it.
|
JavaFileObject |
createSourceFile(CharSequence name,
Element... originatingElements)
Creates a new source file and returns an object to allow
writing to it.
|
void |
displayState()
Debugging function to display internal state.
|
Map<String,JavaFileObject> |
getGeneratedClasses() |
Set<JavaFileObject> |
getGeneratedSourceFileObjects() |
Set<String> |
getGeneratedSourceNames() |
FileObject |
getResource(JavaFileManager.Location location,
CharSequence pkg,
CharSequence relativeName)
Returns an object for reading an existing resource.
|
boolean |
newFiles() |
void |
newRound(Context context)
Update internal state for a new round.
|
String |
toString() |
void |
warnIfUnclosedFiles() |
public JavaFileObject createSourceFile(CharSequence name, Element... originatingElements) throws IOException
Filer
name
be the
package's name followed by ".package-info"
; to create a
source file for an unnamed package, use "package-info"
.
Note that to use a particular charset to encode the contents of the
file, an OutputStreamWriter
with the chosen charset can
be created from the OutputStream
from the returned
object. If the Writer
from the returned object is
directly used for writing, its charset is determined by the
implementation. An annotation processing tool may have an
-encoding
flag or analogous option for specifying this;
otherwise, it will typically be the platform's default
encoding.
To avoid subsequent errors, the contents of the source file should be compatible with the source version being used for this run.
createSourceFile
in interface Filer
name
- canonical (fully qualified) name of the principal type
being declared in this file or a package name followed by
".package-info"
for a package information fileoriginatingElements
- type or package elements causally
associated with the creation of this file, may be elided or
null
JavaFileObject
to write the new source fileFilerException
- if the same pathname has already been
created, the same type has already been created, or the name is
not valid for a typeIOException
- if the file cannot be createdpublic JavaFileObject createClassFile(CharSequence name, Element... originatingElements) throws IOException
Filer
name
be the
package's name followed by ".package-info"
; creating a
class file for an unnamed package is not supported.
To avoid subsequent errors, the contents of the class file should be compatible with the source version being used for this run.
createClassFile
in interface Filer
name
- binary name of the type being written or a package name followed by
".package-info"
for a package information fileoriginatingElements
- type or package elements causally
associated with the creation of this file, may be elided or
null
JavaFileObject
to write the new class fileFilerException
- if the same pathname has already been
created, the same type has already been created, or the name is
not valid for a typeIOException
- if the file cannot be createdpublic FileObject createResource(JavaFileManager.Location location, CharSequence pkg, CharSequence relativeName, Element... originatingElements) throws IOException
Filer
CLASS_OUTPUT
and SOURCE_OUTPUT
must be
supported. The resource may be named relative to some package
(as are source and class files), and from there by a relative
pathname. In a loose sense, the full pathname of the new file
will be the concatenation of location
, pkg
, and
relativeName
.
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.
createResource
in interface Filer
location
- location of the new filepkg
- package relative to which the file should be named,
or the empty string if nonerelativeName
- final pathname components of the fileoriginatingElements
- type or package elements causally
associated with the creation of this file, may be elided or
null
FileObject
to write the new resourceIOException
- if the file cannot be createdFilerException
- if the same pathname has already been
createdpublic FileObject getResource(JavaFileManager.Location location, CharSequence pkg, CharSequence relativeName) throws IOException
Filer
CLASS_OUTPUT
and SOURCE_OUTPUT
must
be supported.getResource
in interface Filer
location
- location of the filepkg
- package relative to which the file should be searched,
or the empty string if nonerelativeName
- final pathname components of the fileFilerException
- if the same pathname has already been
opened for writingIOException
- if the file cannot be openedpublic boolean newFiles()
public Set<JavaFileObject> getGeneratedSourceFileObjects()
public Map<String,JavaFileObject> getGeneratedClasses()
public void warnIfUnclosedFiles()
public void newRound(Context context)
public void close()
close
in interface Closeable
close
in interface AutoCloseable
public void displayState()
Copyright © 2016. All rights reserved.