Class SourceFile

java.lang.Object
com.google.errorprone.apply.SourceFile

public class SourceFile extends Object
Representation of a mutable Java source file.

This class is not thread-safe.

Author:
sjnickerson@google.com (Simon Nickerson), alexeagle@google.com (Alex Eagle)
  • Constructor Details

  • Method Details

    • create

      public static SourceFile create(JavaFileObject fileObject) throws IOException
      Throws:
      IOException
    • getPath

      public String getPath()
      Returns the path for this source file
    • getLines

      public List<String> getLines()
      Returns a copy of code as a list of lines.
    • getSourceText

      public String getSourceText()
      Returns a copy of the code as a string.
    • getAsSequence

      public CharSequence getAsSequence()
    • setSourceText

      public void setSourceText(CharSequence source)
      Clears the current source test for this SourceFile and resets it to the passed-in value.
    • getFragmentByChars

      public String getFragmentByChars(int startPosition, int endPosition)
      Returns a fragment of the source code as a string.

      This method uses the same conventions as String.substring(int, int) for its start and end parameters.

    • getFragmentByLines

      public String getFragmentByLines(int startLine, int endLine)
      Returns a fragment of the source code between the two stated line numbers. The parameters represent inclusive line numbers.

      The returned fragment will end in a newline.

    • replaceLines

      public void replaceLines(List<String> lines)
      Replace the source code with the new lines of code.
    • replaceLines

      public void replaceLines(int startLine, int endLine, List<String> replacementLines)
      Replace the source code between the start and end lines with some new lines of code.
    • replaceChars

      public void replaceChars(int startPosition, int endPosition, String replacement)
      Replace the source code between the start and end character positions with a new string.

      This method uses the same conventions as String.substring(int, int) for its start and end parameters.