Package com.google.googlejavaformat.java
Class ImportOrderer
java.lang.Object
com.google.googlejavaformat.java.ImportOrderer
Orders imports in Java source code.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) classAn import statement.private static classprivate static class -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final Comparator<ImportOrderer.Import> AComparatorthat ordersImportOrderer.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.private static final com.google.common.collect.ImmutableSet<com.sun.tools.javac.parser.Tokens.TokenKind> Tokens.TokenKinds that indicate the start of a type definition.private static final com.google.common.base.Splitterprivate static final Comparator<ImportOrderer.Import> AComparatorthat ordersImportOrderer.Imports by Google Style, defined at https://google.github.io/styleguide/javaguide.html#s3.3.3-import-ordering-and-spacing.private static final com.google.common.collect.ImmutableSet<String> 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.private final Comparator<ImportOrderer.Import> private final Stringprivate final BiFunction<ImportOrderer.Import, ImportOrderer.Import, Boolean> private final Stringprivate final com.google.common.collect.ImmutableList<JavaInput.Tok> -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateImportOrderer(String text, com.google.common.collect.ImmutableList<JavaInput.Tok> toks, JavaFormatterOptions.Style style) -
Method Summary
Modifier and TypeMethodDescriptionfindIdentifier(int start, com.google.common.collect.ImmutableSet<String> identifiers) Returns the index of the first place where one of the given identifiers occurs, orOptional.empty()if there is none.private booleanisIdentifierToken(int i) private booleanisNewlineToken(int i) private booleanisSlashSlashCommentToken(int i) private booleanisSpaceToken(int i) private StringreorderedImportsString(com.google.common.collect.ImmutableSortedSet<ImportOrderer.Import> imports) private Stringstatic StringreorderImports(String text) Deprecated.static StringreorderImports(String text, JavaFormatterOptions.Style style) Reorder the inputs intext, a complete Java program.private ImportOrderer.StringAndIndexscanImported(int start) Scans the imported thing, the dot-separated name that comes after import [static] and before the semicolon.private ImportOrderer.ImportsAndIndexscanImports(int i) Scans a sequence of import lines.private static booleanDetermines whether to insert a blank line between theprevandcurrImportOrderer.Imports based on AOSP style.private static booleanDetermines whether to insert a blank line between theprevandcurrImportOrderer.Imports based on Google style.private StringtokenAt(int i) private StringtokString(int start, int end) private intunindent(int i) Returns the given token, or the preceding token if it is a whitespace token.
-
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_STARTTokens.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
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 isinterface, so we don't need a separate entry for that. -
GOOGLE_IMPORT_COMPARATOR
AComparatorthat ordersImportOrderer.Imports by Google Style, defined at https://google.github.io/styleguide/javaguide.html#s3.3.3-import-ordering-and-spacing. -
AOSP_IMPORT_COMPARATOR
AComparatorthat ordersImportOrderer.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
-
toks
-
lineSeparator
-
importComparator
-
shouldInsertBlankLineFn
-
-
Constructor Details
-
ImportOrderer
private ImportOrderer(String text, com.google.common.collect.ImmutableList<JavaInput.Tok> toks, JavaFormatterOptions.Style style)
-
-
Method Details
-
reorderImports
public static String reorderImports(String text, JavaFormatterOptions.Style style) throws FormatterException Reorder the inputs intext, 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.UsereorderImports(String, Style)insteadReorder the inputs intext, 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
- Throws:
FormatterException
-
shouldInsertBlankLineGoogle
private static boolean shouldInsertBlankLineGoogle(ImportOrderer.Import prev, ImportOrderer.Import curr) Determines whether to insert a blank line between theprevandcurrImportOrderer.Imports based on Google style. -
shouldInsertBlankLineAosp
private static boolean shouldInsertBlankLineAosp(ImportOrderer.Import prev, ImportOrderer.Import curr) Determines whether to insert a blank line between theprevandcurrImportOrderer.Imports based on AOSP style. -
tokString
-
scanImports
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
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 isimport java.util.*;then the returned string will bejava.util.*.- Parameters:
start- the index of the start of the identifier. If the import isimport java.util.List;then this index points to the tokenjava.- Returns:
- the parsed import (
java.util.Listin 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, orOptional.empty()if there is none.- Parameters:
start- the index to start looking atidentifiers- 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
-
isIdentifierToken
private boolean isIdentifierToken(int i) -
isSpaceToken
private boolean isSpaceToken(int i) -
isSlashSlashCommentToken
private boolean isSlashSlashCommentToken(int i) -
isNewlineToken
private boolean isNewlineToken(int i)
-
reorderImports(String, Style)instead