Skip to content

Commit

Permalink
Only emit package-info's if the source file's path is 'package-info.j…
Browse files Browse the repository at this point in the history
…ava'

To avoid emitting duplicate symbols when there are other files that contain no
classes, and to match javac's behaviour.

PiperOrigin-RevId: 545473342
  • Loading branch information
cushon authored and Javac Team committed Jul 4, 2023
1 parent 701bb99 commit 7c64f04
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
16 changes: 15 additions & 1 deletion java/com/google/turbine/binder/CompUnitPreprocessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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<String, ClassSymbol> preprocessChildren(
SourceFile source,
ImmutableList.Builder<SourceBoundClass> types,
Expand Down
2 changes: 2 additions & 0 deletions javatests/com/google/turbine/lower/testdata/package_info.test
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ package p;
package p;
class Test {}

=== Empty.java ===
package p;
4 changes: 2 additions & 2 deletions javatests/com/google/turbine/main/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 7c64f04

Please sign in to comment.