Class ImportStatements

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

public class ImportStatements
extends Object
Represents a list of import statements. Supports adding and removing import statements and pretty printing the result as source code. Sorts and organizes the imports using the given importOrganizer.
Author:
eaftan@google.com (Eddie Aftandilian)
  • Method Details

    • create

      public static ImportStatements create​(com.sun.tools.javac.tree.JCTree.JCCompilationUnit compilationUnit)
    • create

      public static ImportStatements create​(com.sun.tools.javac.tree.JCTree.JCCompilationUnit compilationUnit, ImportOrganizer importOrganizer)
    • getStartPos

      public int getStartPos()
      Return the start position of the import statements.
    • getEndPos

      public int getEndPos()
      Return the end position of the import statements.
    • add

      public boolean add​(String importToAdd)
      Add an import to the list of imports. If the import is already in the list, does nothing. The import should be of the form "import foo.bar".
      Parameters:
      importToAdd - a string representation of the import to add
      Returns:
      true if the import was added
    • addAll

      public boolean addAll​(Collection<String> importsToAdd)
      Add all imports in a collection to this list of imports. Does not add any imports that are already in the list.
      Parameters:
      importsToAdd - a collection of imports to add
      Returns:
      true if any imports were added to the list
    • remove

      public boolean remove​(String importToRemove)
      Remove an import from the list of imports. If the import is not in the list, does nothing. The import should be of the form "import foo.bar".
      Parameters:
      importToRemove - a string representation of the import to remove
      Returns:
      true if the import was removed
    • removeAll

      public boolean removeAll​(Collection<String> importsToRemove)
      Removes all imports in a collection to this list of imports. Does not remove any imports that are not in the list.
      Parameters:
      importsToRemove - a collection of imports to remove
      Returns:
      true if any imports were removed from the list
    • importsHaveChanged

      public boolean importsHaveChanged()
    • toString

      public String toString()
      Returns a string representation of the imports as Java code in correct order.
      Overrides:
      toString in class Object