APIdia Launch Partner
Top Description Fields Constructors Methods
javax.tools

public Class SimpleJavaFileObject

extends Object
implements JavaFileObject
Class Inheritance
All Implemented Interfaces
javax.tools.JavaFileObject, javax.tools.FileObject
Imports
java.io.*, java.net.URI, java.nio.CharBuffer, java.util.Objects, javax.lang.model.element.Modifier, .NestingKind

Provides simple implementations for most methods in JavaFileObject. This class is designed to be subclassed and used as a basis for JavaFileObject implementations. Subclasses can override the implementation and specification of any method of this class as long as the general contract of JavaFileObject is obeyed.
Since
1.6

Field Summary

Modifier and TypeField and Description
protected final JavaFileObject.Kind
kind

The kind of this file object.

protected final URI
uri

A URI for this file object.

Constructor Summary

AccessConstructor and Description
protected
SimpleJavaFileObject(URI
the URI for this file object
uri
,
JavaFileObject.Kind
the kind of this file object
kind
)

Construct a SimpleJavaFileObject of the given kind and with the given URI.

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

false
delete
()

Implements javax.tools.FileObject.delete.

Deletes this file object.

public static JavaFileObject

Returns:

a JavaFileObject representing the given source content.
forSource
(URI
that should be used for the resulting JavaFileObject
uri
,
String
the content of the JavaFileObject
content
)

Creates a JavaFileObject which represents the given source content.

public Modifier
getAccessLevel()

Implements javax.tools.JavaFileObject.getAccessLevel.

Provides a hint about the access level of the class represented by this file object.

public CharSequence
getCharContent(boolean ignoreEncodingErrors)

Implements javax.tools.FileObject.getCharContent.

Returns the character content of this file object, if available.

public JavaFileObject.Kind

Returns:

this.kind
getKind
()

Implements javax.tools.JavaFileObject.getKind.

Returns the kind of this file object.

public long

Returns:

0L
getLastModified
()

Implements javax.tools.FileObject.getLastModified.

Returns the time this file object was last modified.

public String
getName()

Implements javax.tools.FileObject.getName.

Returns a user-friendly name for this file object.

public NestingKind
getNestingKind()

Implements javax.tools.JavaFileObject.getNestingKind.

Provides a hint about the nesting level of the class represented by this file object.

public boolean
isNameCompatible(String simpleName, JavaFileObject.Kind kind)

Implements javax.tools.JavaFileObject.isNameCompatible.

Checks if this file object is compatible with the specified simple name and kind.

public InputStream
openInputStream()

Implements javax.tools.FileObject.openInputStream.

Returns an InputStream for this file object.

public OutputStream
openOutputStream()

Implements javax.tools.FileObject.openOutputStream.

Returns an OutputStream for this file object.

public Reader

Returns:

a Reader wrapping the result of getCharContent
openReader
(boolean ignoreEncodingErrors)

Implements javax.tools.FileObject.openReader.

Returns a reader for this object.

public Writer

Returns:

a Writer wrapping the result of openOutputStream
openWriter
()

Implements javax.tools.FileObject.openWriter.

Returns a Writer for this file object.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

public URI
toUri()

Implements javax.tools.FileObject.toUri.

Returns a URI identifying this file object.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

kindback to summary
protected final JavaFileObject.Kind kind

The kind of this file object.

uriback to summary
protected final URI uri

A URI for this file object.

Constructor Detail

SimpleJavaFileObjectback to summary
protected SimpleJavaFileObject(URI uri, JavaFileObject.Kind kind)

Construct a SimpleJavaFileObject of the given kind and with the given URI.

Parameters
uri:URI

the URI for this file object

kind:JavaFileObject.Kind

the kind of this file object

Method Detail

deleteback to summary
public boolean delete()

Implements javax.tools.FileObject.delete.

Doc from javax.tools.FileObject.delete.

Deletes this file object. In case of errors, returns false.

Implementation Specification

This implementation does nothing.

Returns:boolean

false

Annotations
@Override
forSourceback to summary
public static JavaFileObject forSource(URI uri, String content)

Creates a 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 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!"); }
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:URI

that should be used for the resulting JavaFileObject

content:String

the content of the JavaFileObject

Returns:JavaFileObject

a JavaFileObject representing the given source content.

Since
23
getAccessLevelback to summary
public Modifier getAccessLevel()

Implements javax.tools.JavaFileObject.getAccessLevel.

Doc from javax.tools.JavaFileObject.getAccessLevel.

Provides a hint about the access level of the class represented by this file object. If the access level is not known or this file object does not represent a class file this method returns null.

Implementation Specification

This implementation returns null.

Annotations
@Override
getCharContentback to summary
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException

Implements javax.tools.FileObject.getCharContent.

Doc from javax.tools.FileObject.getCharContent.

Returns the character content of this file object, if available. Any byte that cannot be decoded will be replaced by the default translation character. In addition, a diagnostic may be reported unless ignoreEncodingErrors is true.

Implementation Specification

This implementation always throws UnsupportedOperationException.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurred
getKindback to summary
public JavaFileObject.Kind getKind()

Implements javax.tools.JavaFileObject.getKind.

Doc from javax.tools.JavaFileObject.getKind.

Returns the kind of this file object.

Returns:JavaFileObject.Kind

this.kind

Annotations
@Override
getLastModifiedback to summary
public long getLastModified()

Implements javax.tools.FileObject.getLastModified.

Doc from javax.tools.FileObject.getLastModified.

Returns the time this file object was last modified. The time is measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970).

Implementation Specification

This implementation returns 0L.

Returns:long

0L

Annotations
@Override
getNameback to summary
public String getName()

Implements javax.tools.FileObject.getName.

Doc from javax.tools.FileObject.getName.

Returns a user-friendly name for this file object. The exact value returned is not specified but implementations should take care to preserve names as given by the user. For example, if the user writes the filename "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.

Annotations
@Override
getNestingKindback to summary
public NestingKind getNestingKind()

Implements javax.tools.JavaFileObject.getNestingKind.

Doc from javax.tools.JavaFileObject.getNestingKind.

Provides a hint about the nesting level of the class represented by this file object. This method may return 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.

Implementation Specification

This implementation returns null.

Annotations
@Override
isNameCompatibleback to summary
public boolean isNameCompatible(String simpleName, JavaFileObject.Kind kind)

Implements javax.tools.JavaFileObject.isNameCompatible.

Doc from javax.tools.JavaFileObject.isNameCompatible.

Checks if this file object is compatible with the specified simple name and kind. A simple name is a single identifier (not qualified) as defined in The Java Language Specification, section 6.2.

Implementation Specification

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.extension or if it ends with "/" + simpleName + kind.extension.

This method calls getKind and toUri and does not access the fields uri and kind directly.

Annotations
@Override
openInputStreamback to summary
public InputStream openInputStream() throws IOException

Implements javax.tools.FileObject.openInputStream.

Doc from javax.tools.FileObject.openInputStream.

Returns an InputStream for this file object.

Implementation Specification

This implementation always throws UnsupportedOperationException.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurred
openOutputStreamback to summary
public OutputStream openOutputStream() throws IOException

Implements javax.tools.FileObject.openOutputStream.

Doc from javax.tools.FileObject.openOutputStream.

Returns an OutputStream for this file object.

Implementation Specification

This implementation always throws UnsupportedOperationException.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurred
openReaderback to summary
public Reader openReader(boolean ignoreEncodingErrors) throws IOException

Implements javax.tools.FileObject.openReader.

Doc from javax.tools.FileObject.openReader.

Returns a reader for this object. The returned reader will replace bytes that cannot be decoded with the default translation character. In addition, the reader may report a diagnostic unless ignoreEncodingErrors is true.

Implementation Specification

This implementation wraps the result of getCharContent in a Reader.

Returns:Reader

a Reader wrapping the result of getCharContent

Annotations
@Override
Exceptions
IOException:
if an I/O error occurred
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
openWriterback to summary
public Writer openWriter() throws IOException

Implements javax.tools.FileObject.openWriter.

Doc from javax.tools.FileObject.openWriter.

Returns a Writer for this file object.

Implementation Specification

This implementation wraps the result of openOutputStream in a Writer.

Returns:Writer

a Writer wrapping the result of openOutputStream

Annotations
@Override
Exceptions
IOException:
if an I/O error occurred
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
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Annotations
@Override
toUriback to summary
public URI toUri()

Implements javax.tools.FileObject.toUri.

Doc from javax.tools.FileObject.toUri.

Returns a URI identifying this file object.

Annotations
@Override