From e01b91efe4421256f0b1f3d93909bb2493fcaeba Mon Sep 17 00:00:00 2001 From: Christian Stein Date: Tue, 22 Aug 2017 05:25:53 +0200 Subject: [PATCH] Set supported Java source level to "9" Fixes #181 --- .../googlejavaformat/java/RemoveUnusedImports.java | 3 +++ .../java/RemoveUnusedImportsTest.java | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/core/src/main/java/com/google/googlejavaformat/java/RemoveUnusedImports.java b/core/src/main/java/com/google/googlejavaformat/java/RemoveUnusedImports.java index b8746d757..bd5ae3167 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/RemoveUnusedImports.java +++ b/core/src/main/java/com/google/googlejavaformat/java/RemoveUnusedImports.java @@ -53,6 +53,7 @@ import org.openjdk.source.util.TreeScanner; import org.openjdk.tools.javac.api.JavacTrees; import org.openjdk.tools.javac.file.JavacFileManager; +import org.openjdk.tools.javac.main.Option; import org.openjdk.tools.javac.parser.JavacParser; import org.openjdk.tools.javac.parser.ParserFactory; import org.openjdk.tools.javac.tree.DCTree; @@ -195,6 +196,8 @@ public Void visitIdentifier(IdentifierTree node, Void aVoid) { public static String removeUnusedImports( final String contents, JavadocOnlyImports javadocOnlyImports) { Context context = new Context(); + // TODO(cushon): this should default to the latest supported source level, same as in Formatter + Options.instance(context).put(Option.SOURCE, "9"); JCCompilationUnit unit = parse(context, contents); if (unit == null) { // error handling is done during formatting diff --git a/core/src/test/java/com/google/googlejavaformat/java/RemoveUnusedImportsTest.java b/core/src/test/java/com/google/googlejavaformat/java/RemoveUnusedImportsTest.java index 0da12b701..b9e2c267a 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/RemoveUnusedImportsTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/RemoveUnusedImportsTest.java @@ -329,6 +329,18 @@ public static Collection parameters() { "public class Test implements Foo, Bar, Baz, Bork {}", }, }, + { + { + "import java.lang.Foo;", // + "interface Test { private static void foo() {} }", + }, + { + "interface Test { private static void foo() {} }", + }, + { + "interface Test { private static void foo() {} }", + }, + }, }; ImmutableList.Builder builder = ImmutableList.builder(); for (String[][] inputAndOutput : inputsOutputs) {