Class RemoveUnusedImports

java.lang.Object
com.google.googlejavaformat.java.RemoveUnusedImports

public class RemoveUnusedImports extends Object
Removes unused imports from a source file. Imports that are only used in javadoc are also removed, and the references in javadoc are replaced with fully qualified names.
  • Constructor Details

    • RemoveUnusedImports

      public RemoveUnusedImports()
  • Method Details

    • removeUnusedImports

      public static String removeUnusedImports(String contents) throws FormatterException
      Throws:
      FormatterException
    • parse

      private static com.sun.tools.javac.tree.JCTree.JCCompilationUnit parse(com.sun.tools.javac.util.Context context, String javaInput) throws FormatterException
      Throws:
      FormatterException
    • buildReplacements

      private static com.google.common.collect.RangeMap<Integer,String> buildReplacements(String contents, com.sun.tools.javac.tree.JCTree.JCCompilationUnit unit, Set<String> usedNames, com.google.common.collect.Multimap<String,com.google.common.collect.Range<Integer>> usedInJavadoc)
      Construct replacements to fix unused imports.
    • getSimpleName

      private static String getSimpleName(com.sun.tools.javac.tree.JCTree importTree)
    • isUnused

      private static boolean isUnused(com.sun.tools.javac.tree.JCTree.JCCompilationUnit unit, Set<String> usedNames, com.google.common.collect.Multimap<String,com.google.common.collect.Range<Integer>> usedInJavadoc, com.sun.tools.javac.tree.JCTree importTree, String simpleName)
    • getQualifiedIdentifier

      private static com.sun.tools.javac.tree.JCTree.JCFieldAccess getQualifiedIdentifier(com.sun.tools.javac.tree.JCTree importTree)
    • applyReplacements

      private static String applyReplacements(String source, com.google.common.collect.RangeMap<Integer,String> replacements)
      Applies the replacements to the given source, and re-format any edited javadoc.