Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only emit package-info's if the source file's path is 'package-info.java' #268

Merged
merged 1 commit into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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