Package com.google.googlejavaformat.java
Class ImportOrderer
- java.lang.Object
-
- com.google.googlejavaformat.java.ImportOrderer
-
public class ImportOrderer extends java.lang.ObjectOrders imports in Java source code.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) classImportOrderer.ImportAn import statement.private static classImportOrderer.ImportsAndIndexprivate static classImportOrderer.StringAndIndex
-
Field Summary
Fields Modifier and Type Field Description private static java.util.Comparator<ImportOrderer.Import>AOSP_IMPORT_COMPARATORAComparatorthat 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 com.google.common.collect.ImmutableSet<com.sun.tools.javac.parser.Tokens.TokenKind>CLASS_STARTTokens.TokenKinds that indicate the start of a type definition.private static com.google.common.base.SplitterDOT_SPLITTERprivate static java.util.Comparator<ImportOrderer.Import>GOOGLE_IMPORT_COMPARATORAComparatorthat ordersImportOrderer.Imports by Google Style, defined at https://google.github.io/styleguide/javaguide.html#s3.3.3-import-ordering-and-spacing.private static com.google.common.collect.ImmutableSet<java.lang.String>IMPORT_OR_CLASS_STARTWe 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 java.util.Comparator<ImportOrderer.Import>importComparatorprivate java.lang.StringlineSeparatorprivate java.util.function.BiFunction<ImportOrderer.Import,ImportOrderer.Import,java.lang.Boolean>shouldInsertBlankLineFnprivate java.lang.Stringtextprivate com.google.common.collect.ImmutableList<JavaInput.Tok>toks
-
Constructor Summary
Constructors Modifier Constructor Description privateImportOrderer(java.lang.String text, com.google.common.collect.ImmutableList<JavaInput.Tok> toks, JavaFormatterOptions.Style style)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description private java.util.Optional<java.lang.Integer>findIdentifier(int start, com.google.common.collect.ImmutableSet<java.lang.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 java.lang.StringreorderedImportsString(com.google.common.collect.ImmutableSortedSet<ImportOrderer.Import> imports)private java.lang.StringreorderImports()static java.lang.StringreorderImports(java.lang.String text)Deprecated.UsereorderImports(String, Style)insteadstatic java.lang.StringreorderImports(java.lang.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 booleanshouldInsertBlankLineAosp(ImportOrderer.Import prev, ImportOrderer.Import curr)Determines whether to insert a blank line between theprevandcurrImportOrderer.Imports based on AOSP style.private static booleanshouldInsertBlankLineGoogle(ImportOrderer.Import prev, ImportOrderer.Import curr)Determines whether to insert a blank line between theprevandcurrImportOrderer.Imports based on Google style.private java.lang.StringtokenAt(int i)private java.lang.StringtokString(int start, int end)private intunindent(int i)Returns the given token, or the preceding token if it is a whitespace token.
-
-
-
Field Detail
-
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<java.lang.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 isinterface, so we don't need a separate entry for that.
-
GOOGLE_IMPORT_COMPARATOR
private static final java.util.Comparator<ImportOrderer.Import> 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
private static final java.util.Comparator<ImportOrderer.Import> 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
private final java.lang.String text
-
toks
private final com.google.common.collect.ImmutableList<JavaInput.Tok> toks
-
lineSeparator
private final java.lang.String lineSeparator
-
importComparator
private final java.util.Comparator<ImportOrderer.Import> importComparator
-
shouldInsertBlankLineFn
private final java.util.function.BiFunction<ImportOrderer.Import,ImportOrderer.Import,java.lang.Boolean> shouldInsertBlankLineFn
-
-
Constructor Detail
-
ImportOrderer
private ImportOrderer(java.lang.String text, com.google.common.collect.ImmutableList<JavaInput.Tok> toks, JavaFormatterOptions.Style style)
-
-
Method Detail
-
reorderImports
public static java.lang.String reorderImports(java.lang.String text, JavaFormatterOptions.Style style) throws FormatterExceptionReorder 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 public static java.lang.String reorderImports(java.lang.String text) throws FormatterExceptionDeprecated.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
private java.lang.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 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
private java.lang.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 java.lang.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 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 java.util.Optional<java.lang.Integer> findIdentifier(int start, com.google.common.collect.ImmutableSet<java.lang.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
private java.lang.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)
-
-