Class PathFileObject

java.lang.Object
io.github.ascopes.jct.filemanagers.PathFileObject
All Implemented Interfaces:
FileObject, JavaFileObject

@API(since="0.0.1", status=STABLE) public final class PathFileObject extends Object implements JavaFileObject
A very simple JavaFileObject that points to a path on some FileSystem somewhere.

This object will always use UTF-8 encoding when obtaining readers or writers.

No access level or nesting kind information is provided by this implementation.

Since:
0.0.1
Author:
Ashley Scopes
  • Constructor Details

    • PathFileObject

      public PathFileObject(JavaFileManager.Location location, Path rootPath, Path relativePath)
      Initialize this file object.
      Parameters:
      location - the location that the file object is located within.
      rootPath - the root directory that the path is a package within.
      relativePath - the path to point to, relative to the root.
  • Method Details

    • delete

      public boolean delete()
      Attempt to delete the file.
      Specified by:
      delete in interface FileObject
      Returns:
      true if the file was deleted, or false if it was not deleted.
    • equals

      public boolean equals(@Nullable Object other)
      Determine if this object equals another.
      Overrides:
      equals in class Object
      Parameters:
      other - the other object to compare with.
      Returns:
      true if the other object is also a FileObject and has the same URI as this object, or false otherwise.
    • getAccessLevel

      Get the class access level, where appropriate.

      In this implementation, this class will always return null, since this information is not readily available without preloading the file in question and parsing it first.

      At the time of writing, the OpenJDK implementations of the JavaFileObject class do not provide an implementation for this method either.

      Specified by:
      getAccessLevel in interface JavaFileObject
      Returns:
      null, always. This implementation does not provide this functionality.
    • getBinaryName

      @API(since="0.2.1", status=STABLE) public String getBinaryName()
      Get the inferred binary name of the file object.
      Returns:
      the inferred binary name.
      Since:
      0.2.1
    • getCharContent

      public String getCharContent(boolean ignoreEncodingErrors) throws IOException
      Read the character content of the file into memory and decode it using the default character set.
      Specified by:
      getCharContent in interface FileObject
      Parameters:
      ignoreEncodingErrors - ignore encoding errors if true, or throw them otherwise.
      Returns:
      the character content, encoded as UTF-8.
      Throws:
      IOException - if an IO error occurs.
    • getFullPath

      public Path getFullPath()
      Get the full path of this file object.
      Returns:
      the full path.
    • getKind

      Get the kind of the file.
      Specified by:
      getKind in interface JavaFileObject
      Returns:
      the inferred file kind.
    • getLastModified

      public long getLastModified()
      Determine when the file was last modified.
      Specified by:
      getLastModified in interface FileObject
      Returns:
      the timestamp in milliseconds since UNIX epoch that the file was last modified at, or 0L if unmodified.
    • getLocation

      Get the location of this path file object.
      Returns:
      the location.
    • getName

      public String getName()
      Get the file name as a string.
      Specified by:
      getName in interface FileObject
      Returns:
      the name of the file.
    • getNestingKind

      Determine the class nesting kind, where appropriate.

      In this implementation, this class will always return null, since this information is not readily available without preloading the file in question and parsing it first.

      At the time of writing, the OpenJDK implementations of the JavaFileObject class do not provide an implementation for this method either.

      Specified by:
      getNestingKind in interface JavaFileObject
      Returns:
      null in all cases, this operation is not implemented.
    • getRelativePath

      Get the relative path of this file object.
      Returns:
      the path of this file object.
    • getRootPath

      public Path getRootPath()
      Get the root path that the package containing this file is nested within.
      Returns:
      the root path.
    • hashCode

      public int hashCode()
      Determine the hash code for this object.
      Overrides:
      hashCode in class Object
      Returns:
      the hash code for the object.
    • isNameCompatible

      public boolean isNameCompatible(String simpleName, JavaFileObject.Kind kind)
      Determine if a given simple name and file kind are compatible with this file object.

      This will perform a case-sensitive check, regardless of the platform that it runs on.

      Specified by:
      isNameCompatible in interface JavaFileObject
      Returns:
      true if the simple name and kind are compatible with the current file object name, or false if not.
    • openInputStream

      Open an input stream into this file.

      This input stream must be closed once finished with, otherwise resources will be leaked.

      The returned implementation will always be buffered.

      Specified by:
      openInputStream in interface FileObject
      Returns:
      a buffered input stream.
      Throws:
      NoSuchFileException - if the file does not exist.
      IOException - if an IO error occurs.
    • openOutputStream

      Open an output stream to this file.

      This will create the file first if it does not already exist. If it does exist, then this will overwrite the file and truncate it. The parent directories will also be created if they do not exist.

      This output stream must be closed once finished with, otherwise resources will be leaked.

      The returned implementation will always be buffered.

      Specified by:
      openOutputStream in interface FileObject
      Returns:
      a buffered output stream.
      Throws:
      IOException - if an IO error occurs.
    • openReader

      public BufferedReader openReader(boolean ignoreEncodingErrors) throws IOException
      Open a reader to this file using the default charset (UTF-8).

      This reader must be closed once finished with, otherwise resources will be leaked.

      The returned implementation will always be buffered.

      Specified by:
      openReader in interface FileObject
      Parameters:
      ignoreEncodingErrors - true to suppress encoding errors, or false to throw them to the caller.
      Returns:
      a buffered reader.
      Throws:
      NoSuchFileException - if the file does not exist.
      IOException - if an IO error occurs.
    • openWriter

      Open a writer to this file using the default charset (UTF-8).

      This will create the file first if it does not already exist. If it does exist, this will first overwrite the file and truncate it. The parent directories will also be created if they do not exist.

      This input stream must be closed once finished with, otherwise resources will be leaked.

      The returned implementation will always be buffered.

      Specified by:
      openWriter in interface FileObject
      Returns:
      a buffered writer.
      Throws:
      IOException - if an IO error occurs.
    • toUri

      public URI toUri()
      Determine the URI for this file.
      Specified by:
      toUri in interface FileObject
      Returns:
      the URI for this file object.
    • toString

      public String toString()
      Get a string representation of this object.
      Overrides:
      toString in class Object
      Returns:
      a string representation of this object.