Class SimpleJavaFileObject
java.lang.Object
javax.tools.SimpleJavaFileObject
- All Implemented Interfaces:
- FileObject,- JavaFileObject
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
- 
Nested Class SummaryNested classes/interfaces declared in interface javax.tools.JavaFileObjectJavaFileObject.Kind
- 
Field SummaryFieldsModifier and TypeFieldDescriptionprotected final JavaFileObject.KindThe kind of this file object.protected final URIA URI for this file object.
- 
Constructor SummaryConstructorsModifierConstructorDescriptionprotectedSimpleJavaFileObject(URI uri, JavaFileObject.Kind kind) Construct a SimpleJavaFileObject of the given kind and with the given URI.
- 
Method SummaryModifier and TypeMethodDescriptionbooleandelete()Deletes this file object.static JavaFileObjectCreates aJavaFileObjectwhich represents the given source content.Provides a hint about the access level of the class represented by this file object.getCharContent(boolean ignoreEncodingErrors) Returns the character content of this file object, if available.getKind()Returns the kind of this file object.longReturns the time this file object was last modified.getName()Returns a user-friendly name for this file object.Provides a hint about the nesting level of the class represented by this file object.booleanisNameCompatible(String simpleName, JavaFileObject.Kind kind) Checks if this file object is compatible with the specified simple name and kind.Returns an InputStream for this file object.Returns an OutputStream for this file object.openReader(boolean ignoreEncodingErrors) Returns a reader for this object.Returns a Writer for this file object.toUri()Returns a URI identifying this file object.
- 
Field Details- 
uriA URI for this file object.
- 
kindThe kind of this file object.
 
- 
- 
Constructor Details- 
SimpleJavaFileObjectConstruct a SimpleJavaFileObject of the given kind and with the given URI.- Parameters:
- uri- the URI for this file object
- kind- the kind of this file object
 
 
- 
- 
Method Details- 
toUriDescription copied from interface:FileObjectReturns a URI identifying this file object.- Specified by:
- toUriin interface- FileObject
- Returns:
- a URI
 
- 
getNameDescription copied from interface:FileObjectReturns 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 returnfile:///C:/Documents%20and%20Settings/UncleBob/BobsApp/Test.java.- Specified by:
- getNamein interface- FileObject
- Returns:
- a user-friendly name
 
- 
openInputStreamReturns an InputStream for this file object.- Specified by:
- openInputStreamin interface- FileObject
- Implementation Requirements:
- This implementation always throws UnsupportedOperationException.
- Returns:
- an InputStream
- Throws:
- IOException- if an I/O error occurred
 
- 
openOutputStreamReturns an OutputStream for this file object.- Specified by:
- openOutputStreamin interface- FileObject
- Implementation Requirements:
- This implementation always throws UnsupportedOperationException.
- Returns:
- an OutputStream
- Throws:
- IOException- if an I/O error occurred
 
- 
openReaderReturns 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 unlessignoreEncodingErrorsis true.- Specified by:
- openReaderin interface- FileObject
- 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
 
- 
getCharContentReturns 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 unlessignoreEncodingErrorsis true.- Specified by:
- getCharContentin interface- FileObject
- 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
 
- 
openWriterReturns a Writer for this file object.- Specified by:
- openWriterin interface- FileObject
- 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
 
- 
getLastModifiedpublic long 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).- Specified by:
- getLastModifiedin interface- FileObject
- Implementation Requirements:
- This implementation returns 0L.
- Returns:
- 0L
 
- 
deletepublic boolean delete()Deletes this file object. In case of errors, returns false.- Specified by:
- deletein interface- FileObject
- Implementation Requirements:
- This implementation does nothing.
- Returns:
- false
 
- 
getKindDescription copied from interface:JavaFileObjectReturns the kind of this file object.- Specified by:
- getKindin interface- JavaFileObject
- Returns:
- this.kind
 
- 
isNameCompatibleChecks 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.- Specified by:
- isNameCompatiblein interface- JavaFileObject
- 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
 
- 
getNestingKindProvides a hint about the nesting level of the class represented by this file object. This method may returnNestingKind.MEMBERto meanNestingKind.LOCALorNestingKind.ANONYMOUS. If the nesting level is not known or this file object does not represent a class file this method returnsnull.- Specified by:
- getNestingKindin interface- JavaFileObject
- Implementation Requirements:
- This implementation returns null.
- Returns:
- the nesting kind, or nullif the nesting kind is not known
 
- 
getAccessLevelProvides 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 returnsnull.- Specified by:
- getAccessLevelin interface- JavaFileObject
- Implementation Requirements:
- This implementation returns null.
- Returns:
- the access level
 
- 
forSourceCreates aJavaFileObjectwhich represents the given source content.The provided uriwill be returned fromtoUri(). The providedcontentwill be returned fromgetCharContent(boolean). ThegetKind()method will returnJavaFileObject.Kind.SOURCE.All other methods will behave as described in the documentation in this class, as if the constructor is called with uriandKind.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 resulting- JavaFileObject
- content- the content of the- JavaFileObject
- Returns:
- a JavaFileObjectrepresenting the given source content.
- Since:
- 23
 
 
-