Class ImportOrderer

java.lang.Object
com.google.googlejavaformat.java.ImportOrderer

public class ImportOrderer extends Object
Orders imports in Java source code.
  • Field Details

    • DOT_SPLITTER

      private static final com.google.common.base.Splitter DOT_SPLITTER
    • CLASS_START

      private static final com.google.common.collect.ImmutableSet<com.sun.tools.javac.parser.Tokens.TokenKind> CLASS_START
      Tokens.TokenKinds that indicate the start of a type definition. We use this to avoid scanning the whole file, since we know that imports must precede any type definition.
    • IMPORT_OR_CLASS_START

      private static final com.google.common.collect.ImmutableSet<String> IMPORT_OR_CLASS_START
      We use this set to find the first import, and again to check that there are no imports after the place we stopped gathering them. An annotation definition (@interface) is two tokens, the second which is interface, so we don't need a separate entry for that.
    • GOOGLE_IMPORT_COMPARATOR

      private static final Comparator<ImportOrderer.Import> GOOGLE_IMPORT_COMPARATOR
      A Comparator that orders ImportOrderer.Imports by Google Style, defined at https://google.github.io/styleguide/javaguide.html#s3.3.3-import-ordering-and-spacing.
    • AOSP_IMPORT_COMPARATOR

      private static final Comparator<ImportOrderer.Import> AOSP_IMPORT_COMPARATOR
      A Comparator that orders ImportOrderer.Imports by AOSP Style, defined at https://source.android.com/setup/contribute/code-style#order-import-statements and implemented in IntelliJ at https://android.googlesource.com/platform/development/+/master/ide/intellij/codestyles/AndroidStyle.xml.
    • text

      private final String text
    • toks

      private final com.google.common.collect.ImmutableList<JavaInput.Tok> toks
    • lineSeparator

      private final String lineSeparator
    • importComparator

      private final Comparator<ImportOrderer.Import> importComparator
    • shouldInsertBlankLineFn

      private final BiFunction<ImportOrderer.Import,ImportOrderer.Import,Boolean> shouldInsertBlankLineFn
  • Constructor Details

  • Method Details

    • reorderImports

      public static String reorderImports(String text, JavaFormatterOptions.Style style) throws FormatterException
      Reorder the inputs in text, a complete Java program. On success, another complete Java program is returned, which is the same as the original except the imports are in order.
      Throws:
      FormatterException - if the input could not be parsed.
    • reorderImports

      @Deprecated public static String reorderImports(String text) throws FormatterException
      Deprecated.
      Reorder the inputs in text, a complete Java program, in Google style. On success, another complete Java program is returned, which is the same as the original except the imports are in order.
      Throws:
      FormatterException - if the input could not be parsed.
    • reorderImports

      private String reorderImports() throws FormatterException
      Throws:
      FormatterException
    • shouldInsertBlankLineGoogle

      private static boolean shouldInsertBlankLineGoogle(ImportOrderer.Import prev, ImportOrderer.Import curr)
      Determines whether to insert a blank line between the prev and curr ImportOrderer.Imports based on Google style.
    • shouldInsertBlankLineAosp

      private static boolean shouldInsertBlankLineAosp(ImportOrderer.Import prev, ImportOrderer.Import curr)
      Determines whether to insert a blank line between the prev and curr ImportOrderer.Imports based on AOSP style.
    • tokString

      private String tokString(int start, int end)
    • scanImports

      private ImportOrderer.ImportsAndIndex scanImports(int i) throws FormatterException
      Scans a sequence of import lines. The parsing uses this approximate grammar:
      
       <imports> -> (<end-of-line> | <import>)*
       <import> -> "import" <whitespace> ("static" <whitespace>)?
          <identifier> ("." <identifier>)* ("." "*")? <whitespace>? ";"
          <whitespace>? <end-of-line>? (<line-comment> <end-of-line>)*
       
      Parameters:
      i - the index to start parsing at.
      Returns:
      the result of parsing the imports.
      Throws:
      FormatterException - if imports could not parsed according to the grammar.
    • reorderedImportsString

      private String reorderedImportsString(com.google.common.collect.ImmutableSortedSet<ImportOrderer.Import> imports)
    • scanImported

      private ImportOrderer.StringAndIndex scanImported(int start) throws FormatterException
      Scans the imported thing, the dot-separated name that comes after import [static] and before the semicolon. We don't allow spaces inside the dot-separated name. Wildcard imports are supported: if the input is import java.util.*; then the returned string will be java.util.*.
      Parameters:
      start - the index of the start of the identifier. If the import is import java.util.List; then this index points to the token java.
      Returns:
      the parsed import (java.util.List in the example) and the index of the first token after the imported thing (; in the example).
      Throws:
      FormatterException - if the imported name could not be parsed.
    • findIdentifier

      private Optional<Integer> findIdentifier(int start, com.google.common.collect.ImmutableSet<String> identifiers)
      Returns the index of the first place where one of the given identifiers occurs, or Optional.empty() if there is none.
      Parameters:
      start - the index to start looking at
      identifiers - the identifiers to look for
    • unindent

      private int unindent(int i)
      Returns the given token, or the preceding token if it is a whitespace token.
    • tokenAt

      private String tokenAt(int i)
    • isIdentifierToken

      private boolean isIdentifierToken(int i)
    • isSpaceToken

      private boolean isSpaceToken(int i)
    • isSlashSlashCommentToken

      private boolean isSlashSlashCommentToken(int i)
    • isNewlineToken

      private boolean isNewlineToken(int i)