From c27ceac562884c038e3394843ec149bb355a82ff Mon Sep 17 00:00:00 2001 From: ghm Date: Thu, 25 May 2023 01:20:00 -0700 Subject: [PATCH] NotJavadoc: accept Javadoc on module declarations. Also fix BugCheckerRefactoringTestHelper to not choke on module definitions! Fixes external #3923 PiperOrigin-RevId: 535155482 --- .../errorprone/bugpatterns/javadoc/NotJavadoc.java | 7 +++++++ .../bugpatterns/javadoc/NotJavadocTest.java | 11 +++++++++++ .../errorprone/BugCheckerRefactoringTestHelper.java | 8 ++++++-- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/javadoc/NotJavadoc.java b/core/src/main/java/com/google/errorprone/bugpatterns/javadoc/NotJavadoc.java index 5e93936a03f..465c04a931d 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/javadoc/NotJavadoc.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/javadoc/NotJavadoc.java @@ -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; @@ -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); } diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/javadoc/NotJavadocTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/javadoc/NotJavadocTest.java index ee7d98a1a18..0cf56242856 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/javadoc/NotJavadocTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/javadoc/NotJavadocTest.java @@ -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); + } } diff --git a/test_helpers/src/main/java/com/google/errorprone/BugCheckerRefactoringTestHelper.java b/test_helpers/src/main/java/com/google/errorprone/BugCheckerRefactoringTestHelper.java index 4c4e4841039..caa2768ed0d 100644 --- a/test_helpers/src/main/java/com/google/errorprone/BugCheckerRefactoringTestHelper.java +++ b/test_helpers/src/main/java/com/google/errorprone/BugCheckerRefactoringTestHelper.java @@ -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. */