Skip to content

Commit a676288

Browse files
klueverError Prone Team
authored andcommitted
Methods inside local classes are not javadoccable.
PiperOrigin-RevId: 886936126
1 parent e30934a commit a676288

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

core/src/main/java/com/google/errorprone/bugpatterns/javadoc/Utils.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@
4040
import com.sun.source.util.TreePath;
4141
import com.sun.source.util.TreePathScanner;
4242
import com.sun.tools.javac.api.JavacTrees;
43+
import com.sun.tools.javac.code.Symbol;
44+
import com.sun.tools.javac.code.Symbol.ClassSymbol;
4345
import com.sun.tools.javac.code.Symbol.MethodSymbol;
46+
import com.sun.tools.javac.code.Symbol.PackageSymbol;
4447
import com.sun.tools.javac.tree.DCTree.DCDocComment;
4548
import com.sun.tools.javac.tree.DocCommentTable;
4649
import com.sun.tools.javac.tree.JCTree;
@@ -159,24 +162,31 @@ public Void visitPackage(PackageTree packageTree, Void unused) {
159162

160163
@Override
161164
public Void visitClass(ClassTree classTree, Void unused) {
162-
if (!(getSymbol(classTree).owner instanceof MethodSymbol)) {
165+
Symbol owner = getSymbol(classTree).owner;
166+
if (owner instanceof PackageSymbol || owner instanceof ClassSymbol) {
163167
javadoccablePositions.put(ASTHelpers.getStartPosition(classTree), getCurrentPath());
164168
}
165169
return super.visitClass(classTree, null);
166170
}
167171

168172
@Override
169173
public Void visitMethod(MethodTree methodTree, Void unused) {
170-
if (!ASTHelpers.isGeneratedConstructor(methodTree)) {
174+
Symbol owner = getSymbol(methodTree).owner;
175+
if (!ASTHelpers.isGeneratedConstructor(methodTree)
176+
&& owner instanceof ClassSymbol
177+
&& !isLocalOrAnonymousClass(owner)) {
171178
javadoccablePositions.put(ASTHelpers.getStartPosition(methodTree), getCurrentPath());
172179
}
173180
return super.visitMethod(methodTree, null);
174181
}
175182

176183
@Override
177184
public Void visitVariable(VariableTree variableTree, Void unused) {
178-
ElementKind kind = getSymbol(variableTree).getKind();
179-
if (kind == ElementKind.FIELD && !ASTHelpers.isRecord(getSymbol(variableTree))) {
185+
Symbol symbol = getSymbol(variableTree);
186+
ElementKind kind = symbol.getKind();
187+
if (kind == ElementKind.FIELD
188+
&& !ASTHelpers.isRecord(symbol)
189+
&& !isLocalOrAnonymousClass(symbol.owner)) {
180190
javadoccablePositions.put(ASTHelpers.getStartPosition(variableTree), getCurrentPath());
181191
}
182192
// For enum constants, skip past the desugared class declaration.
@@ -202,5 +212,9 @@ public Void visitModule(ModuleTree moduleTree, Void unused) {
202212
return ImmutableMap.copyOf(javadoccablePositions);
203213
}
204214

215+
private static boolean isLocalOrAnonymousClass(Symbol sym) {
216+
return sym.isAnonymous() || sym.owner instanceof MethodSymbol;
217+
}
218+
205219
private Utils() {}
206220
}

core/src/test/java/com/google/errorprone/bugpatterns/javadoc/AlmostJavadocTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,6 @@ public void nonJavadoccablePositionMethod() {
277277
public class Test {
278278
void foo() {
279279
class Foo {
280-
// TODO(kak): this shouldn't fire here!
281-
// BUG: Diagnostic contains: AlmostJavadoc
282280
/* Foo {@link Test}. */
283281
void bar() {}
284282
}

core/src/test/java/com/google/errorprone/bugpatterns/javadoc/NotJavadocTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,14 @@ class A {}
7474

7575
@Test
7676
public void nestedClassWithMethod() {
77-
// TODO(kak): we should also fix the "javadocs" on the method inside the local class
7877
compilationHelper
7978
.addSourceLines(
8079
"Test.java",
8180
"""
8281
class Test {
8382
void test() {
8483
class A {
84+
// BUG: Diagnostic contains: local class
8585
/** Not Javadoc. */
8686
void method() {}
8787
}
@@ -124,15 +124,14 @@ void method() {}
124124
}
125125
}
126126
""")
127-
// TODO(kak): we should also fix the "javadocs" on the method inside the local class
128127
.addOutputLines(
129128
"Test.java",
130129
"""
131130
class Test {
132131
void test() {
133132
/* Not Javadoc. */
134133
class A {
135-
/** Not Javadoc. */
134+
/* Not Javadoc. */
136135
void method() {}
137136
}
138137
}

0 commit comments

Comments
 (0)