Skip to content

Commit

Permalink
NotJavadoc: accept Javadoc on module declarations.
Browse files Browse the repository at this point in the history
Also fix BugCheckerRefactoringTestHelper to not choke on module definitions!

Fixes external #3923

PiperOrigin-RevId: 535155482
  • Loading branch information
graememorgan authored and Error Prone Team committed May 25, 2023
1 parent 05d9da7 commit c27ceac
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.ModuleTree;
import com.sun.source.tree.NewClassTree;
import com.sun.source.tree.PackageTree;
import com.sun.source.tree.Tree;
Expand Down Expand Up @@ -119,6 +120,12 @@ public Void visitVariable(VariableTree variableTree, Void unused) {
}
return super.visitVariable(variableTree, null);
}

@Override
public Void visitModule(ModuleTree moduleTree, Void unused) {
javadoccablePositions.put(getStartPosition(moduleTree), moduleTree);
return super.visitModule(moduleTree, null);
}
}.scan(tree, null);
return ImmutableMap.copyOf(javadoccablePositions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,15 @@ public void packageLevel() {
.expectUnchanged()
.doTest(TEXT_MATCH);
}

@Test
public void moduleLevel() {
helper
.addInputLines(
"module-info.java", //
"/** Module javadoc */",
"module foo {}")
.expectUnchanged()
.doTest(TEXT_MATCH);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,12 @@ private static String getFullyQualifiedName(JCCompilationUnit tree) {
return Iterators.getOnlyElement(types).sym.getQualifiedName().toString();
}

// Fallback: if no class is declared, then assume we're looking at a `package-info.java`.
return tree.getPackage().packge.package_info.toString();
// Fallback: if no class is declared, then assume we're looking at a `package-info.java`..
if (tree.getPackage() != null) {
return tree.getPackage().packge.package_info.toString();
}
// ..or a `module-info.java`.
return tree.getModuleDecl().sym.getQualifiedName().toString();
}

/** To assert the proper {@code .addInput().addOutput()} chain. */
Expand Down

0 comments on commit c27ceac

Please sign in to comment.