diff --git a/core/src/main/java/com/google/googlejavaformat/java/ImportOrderer.java b/core/src/main/java/com/google/googlejavaformat/java/ImportOrderer.java index ba2cd2e7e..83a2b74bb 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/ImportOrderer.java +++ b/core/src/main/java/com/google/googlejavaformat/java/ImportOrderer.java @@ -22,6 +22,8 @@ import com.google.common.collect.ImmutableSortedSet; import com.google.googlejavaformat.Newlines; import com.google.googlejavaformat.java.JavaInput.Tok; +import java.util.ArrayList; +import java.util.List; import org.openjdk.tools.javac.parser.Tokens.TokenKind; /** Orders imports in Java source code. */ @@ -115,16 +117,6 @@ private String reorderImports() throws FormatterException { throw new FormatterException("Imports not contiguous (perhaps a comment separates them?)"); } - // Add back the text from after the point where we stopped tokenizing. - String tail; - if (toks.isEmpty()) { - tail = ""; - } else { - Tok lastTok = getLast(toks); - int tailStart = lastTok.getPosition() + lastTok.length(); - tail = text.substring(tailStart); - } - StringBuilder result = new StringBuilder(); result.append( CharMatcher.whitespace().trimTrailingFrom(tokString(0, unindentedFirstImportStart))); @@ -132,10 +124,19 @@ private String reorderImports() throws FormatterException { result.append(lineSeparator).append(lineSeparator); } result.append(reorderedImportsString(imports.imports)); - result.append(lineSeparator); - result.append( - CharMatcher.whitespace().trimLeadingFrom(tokString(afterLastImport, toks.size()))); - result.append(tail); + + List tail = new ArrayList<>(); + tail.add(CharMatcher.whitespace().trimLeadingFrom(tokString(afterLastImport, toks.size()))); + if (!toks.isEmpty()) { + Tok lastTok = getLast(toks); + int tailStart = lastTok.getPosition() + lastTok.length(); + tail.add(text.substring(tailStart)); + } + if (tail.stream().anyMatch(s -> !s.isEmpty())) { + result.append(lineSeparator); + tail.forEach(result::append); + } + return result.toString(); } diff --git a/core/src/test/java/com/google/googlejavaformat/java/MainTest.java b/core/src/test/java/com/google/googlejavaformat/java/MainTest.java index b9058b74a..bdb74027e 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/MainTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/MainTest.java @@ -254,4 +254,26 @@ public void importRemoveErrorParseError() throws Exception { assertThat(main.format("-")).isEqualTo(1); assertThat(err.toString()).contains(":4:3: error: class, interface, or enum expected"); } + + @Test + public void packageInfo() throws Exception { + String[] input = { + "@CheckReturnValue", + "@ParametersAreNonnullByDefault", + "package com.google.common.labs.base;", + "", + "import javax.annotation.CheckReturnValue;", + "import javax.annotation.ParametersAreNonnullByDefault;", + "", + }; + StringWriter out = new StringWriter(); + StringWriter err = new StringWriter(); + Main main = + new Main( + new PrintWriter(out, true), + new PrintWriter(err, true), + new ByteArrayInputStream(joiner.join(input).getBytes(UTF_8))); + assertThat(main.format("-")).isEqualTo(0); + assertThat(out.toString()).isEqualTo(joiner.join(input)); + } }