diff --git a/java/com/google/turbine/binder/CompUnitPreprocessor.java b/java/com/google/turbine/binder/CompUnitPreprocessor.java index 25ba48a4..98be8988 100644 --- a/java/com/google/turbine/binder/CompUnitPreprocessor.java +++ b/java/com/google/turbine/binder/CompUnitPreprocessor.java @@ -36,6 +36,8 @@ import com.google.turbine.tree.Tree.PkgDecl; import com.google.turbine.tree.Tree.TyDecl; import com.google.turbine.tree.TurbineModifier; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.HashSet; import java.util.List; import java.util.Optional; @@ -105,7 +107,7 @@ public static PreprocessedCompUnit preprocess(CompUnit unit) { // "While the file could technically contain the source code // for one or more package-private (default-access) classes, // it would be very bad form." -- JLS 7.4.1 - if (!unit.pkg().get().annos().isEmpty() || unit.decls().isEmpty()) { + if (isPackageInfo(unit)) { decls = Iterables.concat(decls, ImmutableList.of(packageInfoTree(unit.pkg().get()))); } } else { @@ -124,6 +126,18 @@ public static PreprocessedCompUnit preprocess(CompUnit unit) { unit.imports(), types.build(), unit.mod(), unit.source(), packageName); } + private static boolean isPackageInfo(CompUnit unit) { + String path = unit.source().path(); + if (path == null) { + return false; + } + Path fileName = Paths.get(path).getFileName(); + if (fileName == null) { + return false; + } + return fileName.toString().equals("package-info.java"); + } + private static ImmutableMap preprocessChildren( SourceFile source, ImmutableList.Builder types, diff --git a/javatests/com/google/turbine/lower/testdata/package_info.test b/javatests/com/google/turbine/lower/testdata/package_info.test index a2416a46..6b0fc32e 100644 --- a/javatests/com/google/turbine/lower/testdata/package_info.test +++ b/javatests/com/google/turbine/lower/testdata/package_info.test @@ -10,3 +10,5 @@ package p; package p; class Test {} +=== Empty.java === +package p; \ No newline at end of file diff --git a/javatests/com/google/turbine/main/MainTest.java b/javatests/com/google/turbine/main/MainTest.java index c894d9d3..f65e6c05 100644 --- a/javatests/com/google/turbine/main/MainTest.java +++ b/javatests/com/google/turbine/main/MainTest.java @@ -106,7 +106,7 @@ public void sourceJarClash() throws IOException { @Test public void packageInfo() throws IOException { - Path src = temporaryFolder.newFile("package-info.jar").toPath(); + Path src = temporaryFolder.newFile("package-info.java").toPath(); MoreFiles.asCharSink(src, UTF_8).write("@Deprecated package test;"); Path output = temporaryFolder.newFile("output.jar").toPath(); @@ -467,7 +467,7 @@ public static byte[] dump() { @Test public void classGeneration() throws IOException { - Path src = temporaryFolder.newFile("package-info.jar").toPath(); + Path src = temporaryFolder.newFile("package-info.java").toPath(); MoreFiles.asCharSink(src, UTF_8).write("@Deprecated package test;"); File resources = temporaryFolder.newFile("resources.jar"); Main.compile(