Class SimpleJavaFileObject
- All Implemented Interfaces:
FileObject, JavaFileObject
public class SimpleJavaFileObject extends Object implements JavaFileObject
- Since:
- 1.6
Nested Class Summary
Nested classes/interfaces declared in interface JavaFileObject
JavaFileObject.Kind
Field Summary
| Modifier and Type | Field | Description |
|---|---|---|
protected final JavaFileObject.Kind |
kind |
The kind of this file object. |
protected final URI |
uri |
A URI for this file object. |
Constructor Summary
| Modifier | Constructor | Description |
|---|---|---|
protected |
Construct a SimpleJavaFileObject of the given kind and with the given URI. |
Method Summary
| Modifier and Type | Method | Description |
|---|---|---|
boolean |
delete() |
Deletes this file object. |
static JavaFileObject |
forSource |
Creates a JavaFileObject which represents the given source content. |
Modifier |
getAccessLevel() |
Provides a hint about the access level of the class represented by this file object. |
CharSequence |
getCharContent |
Returns the character content of this file object, if available. |
JavaFileObject.Kind |
getKind() |
Returns the kind of this file object. |
long |
getLastModified() |
Returns the time this file object was last modified. |
String |
getName() |
Returns a user-friendly name for this file object. |
NestingKind |
getNestingKind() |
Provides a hint about the nesting level of the class represented by this file object. |
boolean |
isNameCompatible |
Checks if this file object is compatible with the specified simple name and kind. |
InputStream |
openInputStream() |
Returns an InputStream for this file object. |
OutputStream |
openOutputStream() |
Returns an OutputStream for this file object. |
Reader |
openReader |
Returns a reader for this object. |
Writer |
openWriter() |
Returns a Writer for this file object. |
URI |
toUri() |
Returns a URI identifying this file object. |
Field Details
uri
protected final URI uri
kind
protected final JavaFileObject.Kind kind
Constructor Details
SimpleJavaFileObject
protected SimpleJavaFileObject(URI uri, JavaFileObject.Kind kind)
- Parameters:
-
uri- the URI for this file object -
kind- the kind of this file object
Method Details
toUri
public URI toUri()
FileObject- Specified by:
-
toUriin interfaceFileObject - Returns:
- a URI
getName
public String getName()
FileObject"BobsApp\Test.java" on the command line, this method should return
"BobsApp\Test.java" whereas the toUri method might return
file:///C:/Documents%20and%20Settings/UncleBob/BobsApp/Test.java.- Specified by:
-
getNamein interfaceFileObject - Returns:
- a user-friendly name
openInputStream
public InputStream openInputStream() throws IOException
- Specified by:
-
openInputStreamin interfaceFileObject - Implementation Requirements:
- This implementation always throws UnsupportedOperationException.
- Returns:
- an InputStream
- Throws:
-
IOException- if an I/O error occurred
openOutputStream
public OutputStream openOutputStream() throws IOException
- Specified by:
-
openOutputStreamin interfaceFileObject - Implementation Requirements:
- This implementation always throws UnsupportedOperationException.
- Returns:
- an OutputStream
- Throws:
-
IOException- if an I/O error occurred
openReader
public Reader openReader(boolean ignoreEncodingErrors) throws IOException
ignoreEncodingErrors is true.- Specified by:
-
openReaderin interfaceFileObject - Implementation Requirements:
- This implementation wraps the result of
getCharContent(boolean)in aReader. - Parameters:
-
ignoreEncodingErrors- ignore encoding errors if true - Returns:
- a Reader wrapping the result of getCharContent
- Throws:
-
IllegalStateException- if this file object was opened for writing and does not support reading -
UnsupportedOperationException- if this kind of file object does not support character access -
IOException- if an I/O error occurred
getCharContent
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException
ignoreEncodingErrors is true.- Specified by:
-
getCharContentin interfaceFileObject - Implementation Requirements:
- This implementation always throws UnsupportedOperationException.
- Parameters:
-
ignoreEncodingErrors- ignore encoding errors if true - Returns:
- a CharSequence if available;
nullotherwise - Throws:
-
IOException- if an I/O error occurred
openWriter
public Writer openWriter() throws IOException
- Specified by:
-
openWriterin interfaceFileObject - Implementation Requirements:
- This implementation wraps the result of
openOutputStream()in aWriter. - Returns:
- a Writer wrapping the result of openOutputStream
- Throws:
-
IllegalStateException- if this file object was opened for reading and does not support writing -
UnsupportedOperationException- if this kind of file object does not support character access -
IOException- if an I/O error occurred
getLastModified
public long getLastModified()
- Specified by:
-
getLastModifiedin interfaceFileObject - Implementation Requirements:
- This implementation returns
0L. - Returns:
0L
delete
public boolean delete()
- Specified by:
-
deletein interfaceFileObject - Implementation Requirements:
- This implementation does nothing.
- Returns:
false
getKind
public JavaFileObject.Kind getKind()
JavaFileObject- Specified by:
-
getKindin interfaceJavaFileObject - Returns:
this.kind
isNameCompatible
public boolean isNameCompatible(String simpleName, JavaFileObject.Kind kind)
- Specified by:
-
isNameCompatiblein interfaceJavaFileObject - Implementation Requirements:
- This implementation compares the path of its URI to the given simple name. This method returns true if the given kind is equal to the kind of this object, and if the path is equal to
simpleName + kind.extensionor if it ends with"/" + simpleName + kind.extension.This method calls
getKind()andtoUri()and does not access the fieldsuriandkinddirectly. - Parameters:
-
simpleName- a simple name of a class -
kind- a kind - Returns:
-
trueif this file object is compatible;falseotherwise
getNestingKind
public NestingKind getNestingKind()
NestingKind.MEMBER to mean NestingKind.LOCAL or NestingKind.ANONYMOUS. If the nesting level is not known or this file object does not represent a class file this method returns null.- Specified by:
-
getNestingKindin interfaceJavaFileObject - Implementation Requirements:
- This implementation returns
null. - Returns:
- the nesting kind, or
nullif the nesting kind is not known
getAccessLevel
public Modifier getAccessLevel()
null.- Specified by:
-
getAccessLevelin interfaceJavaFileObject - Implementation Requirements:
- This implementation returns
null. - Returns:
- the access level
forSource
public static JavaFileObject forSource(URI uri, String content)
JavaFileObject which represents the given source content. The provided uri will be returned from toUri(). The provided content will be returned from getCharContent(boolean). The getKind() method will return JavaFileObject.Kind.SOURCE.
All other methods will behave as described in the documentation in this class, as if the constructor is called with uri and Kind.SOURCE.
This method can be, for example, used to compile an in-memory String to a set of classfile in a target directory:
var code = """
public class CompiledCode {}
""";
var compiler = ToolProvider.getSystemJavaCompiler();
var targetDirectory = "...";
var task = compiler.getTask(null,
null,
null,
List.of("-d", targetDirectory),
null,
List.of(SimpleJavaFileObject.forSource(URI.create("CompiledCode.java"), code)));
if (!task.call()) {
throw new IllegalStateException("Compilation failed!");
}
- Parameters:
-
uri- that should be used for the resultingJavaFileObject -
content- the content of theJavaFileObject - Returns:
- a
JavaFileObjectrepresenting the given source content. - Since:
- 23
© 1993, 2025, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/25/docs/api/java.compiler/javax/tools/SimpleJavaFileObject.html