Package Summary  Overview Summary

class:SimpleJavaFileObject [NONE]

All Implemented Interfaces:
FileObject, JavaFileObject

public class SimpleJavaFileObjectextends Object implements 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

field:uri [NONE]

  • uri

    protected final  URI uri
    A URI for this file object.
  • field:kind [NONE]

    kind

    protected final  JavaFileObject.Kind kind
    The kind of this file object.

    constructor:SimpleJavaFileObject(java.net.URI,javax.tools.JavaFileObject.Kind) [NONE]

    • SimpleJavaFileObject

      protected SimpleJavaFileObject (URI uri, JavaFileObject.Kind kind)
      Construct 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:toUri() [NONE]

  • toUri

    public URI toUri()
    Description copied from interface: FileObject
    Returns a URI identifying this file object.
    Specified by:
    toUri in interface FileObject
    Returns:
    a URI
  • method:getName() [NONE]

    getName

    public String getName()
    Description copied from interface: FileObject
    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 .
    Specified by:
    getName in interface FileObject
    Returns:
    a user-friendly name

    method:openInputStream() [CHANGED]

    openInputStream

    public InputStream openInputStream() throws IOException
    This implementation always throws UnsupportedOperationException. Subclasses can changeReturns an InputStream for this behavior as long as the contract of FileObject is obeyedfile object.
    Specified by:
    openInputStream in interface FileObject
    Implementation Requirements:
    This implementation always throws UnsupportedOperationException.
    Returns:
    an InputStream
    Throws:
    IOException - if an I/O error occurred

    openInputStream

    public InputStream openInputStream() throws IOException
    This implementation always throws UnsupportedOperationException. Subclasses can change this behavior as long as the contract of FileObject is obeyed.
    Specified by:
    openInputStream in interface FileObject
    Returns:
    an InputStream
    Throws:
    IOException - if an I/O error occurred

    openInputStream

    public InputStream openInputStream() throws IOException
    Returns an InputStream for this file object.
    Specified by:
    openInputStream in interface FileObject
    Implementation Requirements:
    This implementation always throws UnsupportedOperationException.
    Returns:
    an InputStream
    Throws:
    IOException - if an I/O error occurred

    method:openOutputStream() [CHANGED]

    openOutputStream

    public OutputStream openOutputStream() throws IOException
    This implementation always throws UnsupportedOperationException. Subclasses can changeReturns an OutputStream for this behavior as long as the contract of FileObject is obeyedfile object.
    Specified by:
    openOutputStream in interface FileObject
    Implementation Requirements:
    This implementation always throws UnsupportedOperationException.
    Returns:
    an OutputStream
    Throws:
    IOException - if an I/O error occurred

    openOutputStream

    public OutputStream openOutputStream() throws IOException
    This implementation always throws UnsupportedOperationException. Subclasses can change this behavior as long as the contract of FileObject is obeyed.
    Specified by:
    openOutputStream in interface FileObject
    Returns:
    an OutputStream
    Throws:
    IOException - if an I/O error occurred

    openOutputStream

    public OutputStream openOutputStream() throws IOException
    Returns an OutputStream for this file object.
    Specified by:
    openOutputStream in interface FileObject
    Implementation Requirements:
    This implementation always throws UnsupportedOperationException.
    Returns:
    an OutputStream
    Throws:
    IOException - if an I/O error occurred

    method:openReader(boolean) [CHANGED]

    openReader

    public Reader openReader (boolean ignoreEncodingErrors) throws IOException
    Wraps the result of getCharContent(boolean) inReturns a Reader. Subclasses can changereader for this behavior as long asobject. The returned reader will replace bytes that cannot be decoded with the default translation character. In addition, the contract of FileObjectreader may report a diagnostic unless ignoreEncodingErrors is obeyedtrue.
    Specified by:
    openReader in interface FileObject
    Implementation Requirements:
    This implementation wraps the result of getCharContent(boolean) in a Reader.
    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

    openReader

    public Reader openReader (boolean ignoreEncodingErrors) throws IOException
    Wraps the result of getCharContent(boolean) in a Reader. Subclasses can change this behavior as long as the contract of FileObject is obeyed.
    Specified by:
    openReader in interface FileObject
    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

    openReader

    public Reader openReader (boolean ignoreEncodingErrors) throws IOException
    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.
    Specified by:
    openReader in interface FileObject
    Implementation Requirements:
    This implementation wraps the result of getCharContent(boolean) in a Reader.
    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

    method:getCharContent(boolean) [CHANGED]

    getCharContent

    public CharSequence getCharContent (boolean ignoreEncodingErrors) throws IOException
    This implementation always throws UnsupportedOperationException. Subclasses can changeReturns the character content of this behavior as long asfile object, if available. Any byte that cannot be decoded will be replaced by the contract of FileObjectdefault translation character. In addition, a diagnostic may be reported unless ignoreEncodingErrors is obeyedtrue.
    Specified by:
    getCharContent in interface FileObject
    Implementation Requirements:
    This implementation always throws UnsupportedOperationException.
    Parameters:
    ignoreEncodingErrors - ignore encoding errors if true
    Returns:
    a CharSequence if available; null otherwise
    Throws:
    IOException - if an I/O error occurred

    getCharContent

    public CharSequence getCharContent (boolean ignoreEncodingErrors) throws IOException
    This implementation always throws UnsupportedOperationException. Subclasses can change this behavior as long as the contract of FileObject is obeyed.
    Specified by:
    getCharContent in interface FileObject
    Parameters:
    ignoreEncodingErrors - ignore encoding errors if true
    Returns:
    a CharSequence if available; null otherwise
    Throws:
    IOException - if an I/O error occurred

    getCharContent

    public CharSequence getCharContent (boolean ignoreEncodingErrors) throws IOException
    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.
    Specified by:
    getCharContent in interface FileObject
    Implementation Requirements:
    This implementation always throws UnsupportedOperationException.
    Parameters:
    ignoreEncodingErrors - ignore encoding errors if true
    Returns:
    a CharSequence if available; null otherwise
    Throws:
    IOException - if an I/O error occurred

    method:openWriter() [CHANGED]

    openWriter

    public Writer openWriter() throws IOException
    Wraps the result of openOutputStream inReturns a Writer . Subclasses can changefor this behavior as long as the contract of FileObject is obeyedfile object.
    Specified by:
    openWriter in interface FileObject
    Implementation Requirements:
    This implementation wraps the result of openOutputStream() in a Writer.
    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

    openWriter

    public Writer openWriter() throws IOException
    Wraps the result of openOutputStream in a Writer. Subclasses can change this behavior as long as the contract of FileObject is obeyed.
    Specified by:
    openWriter in interface FileObject
    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

    openWriter

    public Writer openWriter() throws IOException
    Returns a Writer for this file object.
    Specified by:
    openWriter in interface FileObject
    Implementation Requirements:
    This implementation wraps the result of openOutputStream() in a Writer.
    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

    method:getLastModified() [CHANGED]

    getLastModified

    public long getLastModified()
    This implementation returns 0L. Subclasses can changeReturns the time this behavior as long asfile object was last modified. The time is measured in milliseconds since the contract of FileObject is obeyedepoch (00:00:00 GMT, January 1, 1970).
    Specified by:
    getLastModified in interface FileObject
    Implementation Requirements:
    This implementation returns 0L.
    Returns:
    0L

    getLastModified

    public long getLastModified()
    This implementation returns 0L. Subclasses can change this behavior as long as the contract of FileObject is obeyed.
    Specified by:
    getLastModified in interface FileObject
    Returns:
    0L

    getLastModified

    public 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:
    getLastModified in interface FileObject
    Implementation Requirements:
    This implementation returns 0L.
    Returns:
    0L

    method:delete() [CHANGED]

    delete

    public boolean delete()
    This implementation does nothing. Subclasses can changeDeletes this behavior as long as the contractfile object. In case of FileObject is obeyederrors, returns false.
    Specified by:
    delete in interface FileObject
    Implementation Requirements:
    This implementation does nothing.
    Returns:
    false

    delete

    public boolean delete()
    This implementation does nothing. Subclasses can change this behavior as long as the contract of FileObject is obeyed.
    Specified by:
    delete in interface FileObject
    Returns:
    false

    delete

    public boolean delete()
    Deletes this file object. In case of errors, returns false.
    Specified by:
    delete in interface FileObject
    Implementation Requirements:
    This implementation does nothing.
    Returns:
    false

    method:getKind() [NONE]

    getKind

    public JavaFileObject.Kind getKind()
    Description copied from interface: JavaFileObject
    Returns the kind of this file object.
    Specified by:
    getKind in interface JavaFileObject
    Returns:
    this.kind

    method:isNameCompatible(java.lang.String,javax.tools.JavaFileObject.Kind) [CHANGED]

    isNameCompatible

    public boolean isNameCompatible (String simpleName, JavaFileObject.Kind kind)
    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.
    Specified by:
    isNameCompatible in 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.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.

    Subclasses can change this behavior as long as the contract of JavaFileObject is obeyed.

    Specified by:
    isNameCompatible in interface JavaFileObject
    Parameters:
    simpleName - a simple name of a class
    kind - a kind
    Returns:
    true if this file object is compatible; false otherwise

    isNameCompatible

    public boolean isNameCompatible (String simpleName, JavaFileObject.Kind kind)
    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.

    Subclasses can change this behavior as long as the contract of JavaFileObject is obeyed.

    Specified by:
    isNameCompatible in interface JavaFileObject
    Parameters:
    simpleName - a simple name of a class
    kind - a kind
    Returns:
    true if this file object is compatible; false otherwise

    isNameCompatible

    public boolean isNameCompatible (String simpleName, JavaFileObject.Kind kind)
    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.
    Specified by:
    isNameCompatible in 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.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.

    Parameters:
    simpleName - a simple name of a class
    kind - a kind
    Returns:
    true if this file object is compatible; false otherwise

    method:getNestingKind() [CHANGED]

    getNestingKind

    public NestingKind getNestingKind()
    Provides a hint about the nesting level of the class represented by this file object. This implementationmethod 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. Subclasses can change this behavior as long as the contract of JavaFileObject is obeyed.
    Specified by:
    getNestingKind in interface JavaFileObject
    Implementation Requirements:
    This implementation returns null.
    Returns:
    the nesting kind, or null if the nesting kind is not known

    getNestingKind

    public NestingKind getNestingKind()
    This implementation returns null. Subclasses can change this behavior as long as the contract of JavaFileObject is obeyed.
    Specified by:
    getNestingKind in interface JavaFileObject
    Returns:
    the nesting kind, or null if the nesting kind is not known

    getNestingKind

    public NestingKind 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.
    Specified by:
    getNestingKind in interface JavaFileObject
    Implementation Requirements:
    This implementation returns null.
    Returns:
    the nesting kind, or null if the nesting kind is not known

    method:getAccessLevel() [CHANGED]

    getAccessLevel

    public Modifier getAccessLevel()
    This implementation returns null. Subclasses can changeProvides a hint about the access level of the class represented by this behavior as long asfile object. If the contract of JavaFileObjectaccess level is obeyednot known or this file object does not represent a class file this method returns null.
    Specified by:
    getAccessLevel in interface JavaFileObject
    Implementation Requirements:
    This implementation returns null.
    Returns:
    the access level

    getAccessLevel

    public Modifier getAccessLevel()
    This implementation returns null. Subclasses can change this behavior as long as the contract of JavaFileObject is obeyed.
    Specified by:
    getAccessLevel in interface JavaFileObject
    Returns:
    the access level

    getAccessLevel

    public Modifier 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.
    Specified by:
    getAccessLevel in interface JavaFileObject
    Implementation Requirements:
    This implementation returns null.
    Returns:
    the access level

    method:forSource(java.net.URI,java.lang.String) [ADDED]

    forSource

    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 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 resulting JavaFileObject
    content - the content of the JavaFileObject
    Returns:
    a JavaFileObject representing the given source content.
    Since:
    23