|
| 1 | +/* |
| 2 | + * Copyright 2026 The Error Prone Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.errorprone.bugpatterns.javadoc; |
| 18 | + |
| 19 | +import static com.google.errorprone.BugPattern.LinkType.CUSTOM; |
| 20 | +import static com.google.errorprone.BugPattern.SeverityLevel.WARNING; |
| 21 | +import static com.google.errorprone.matchers.Description.NO_MATCH; |
| 22 | +import static com.google.errorprone.util.ASTHelpers.enclosingClass; |
| 23 | +import static com.google.errorprone.util.ASTHelpers.findSuperMethods; |
| 24 | +import static com.google.errorprone.util.ASTHelpers.getSymbol; |
| 25 | +import static com.google.errorprone.util.ASTHelpers.isEffectivelyPrivate; |
| 26 | + |
| 27 | +import com.google.common.collect.ImmutableSet; |
| 28 | +import com.google.errorprone.BugPattern; |
| 29 | +import com.google.errorprone.VisitorState; |
| 30 | +import com.google.errorprone.bugpatterns.BugChecker; |
| 31 | +import com.google.errorprone.bugpatterns.BugChecker.ClassTreeMatcher; |
| 32 | +import com.google.errorprone.bugpatterns.BugChecker.MethodTreeMatcher; |
| 33 | +import com.google.errorprone.bugpatterns.BugChecker.VariableTreeMatcher; |
| 34 | +import com.google.errorprone.fixes.SuggestedFix; |
| 35 | +import com.google.errorprone.matchers.Description; |
| 36 | +import com.sun.source.doctree.DocCommentTree; |
| 37 | +import com.sun.source.tree.AssignmentTree; |
| 38 | +import com.sun.source.tree.ClassTree; |
| 39 | +import com.sun.source.tree.ExpressionStatementTree; |
| 40 | +import com.sun.source.tree.MethodTree; |
| 41 | +import com.sun.source.tree.ReturnTree; |
| 42 | +import com.sun.source.tree.StatementTree; |
| 43 | +import com.sun.source.tree.Tree; |
| 44 | +import com.sun.source.tree.VariableTree; |
| 45 | +import com.sun.tools.javac.api.JavacTrees; |
| 46 | +import com.sun.tools.javac.code.Symbol; |
| 47 | +import com.sun.tools.javac.code.Symbol.ClassSymbol; |
| 48 | +import com.sun.tools.javac.code.Symbol.MethodSymbol; |
| 49 | +import java.util.Collections; |
| 50 | +import java.util.List; |
| 51 | +import javax.lang.model.element.ElementKind; |
| 52 | +import javax.lang.model.element.Modifier; |
| 53 | + |
| 54 | +/** A {@link BugChecker}; see the associated {@link BugPattern} annotation for details. */ |
| 55 | +@BugPattern( |
| 56 | + summary = "Public types and public/protected members must have Javadoc comments.", |
| 57 | + severity = WARNING, |
| 58 | + linkType = CUSTOM, |
| 59 | + link = "https://google.github.io/styleguide/javaguide.html#s7.1.1-where-required", |
| 60 | + documentSuppression = false) |
| 61 | +public final class MissingJavadoc extends BugChecker |
| 62 | + implements ClassTreeMatcher, MethodTreeMatcher, VariableTreeMatcher { |
| 63 | + |
| 64 | + @Override |
| 65 | + public Description matchClass(ClassTree classTree, VisitorState state) { |
| 66 | + return checkJavadoc(classTree, getSymbol(classTree), state); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public Description matchMethod(MethodTree methodTree, VisitorState state) { |
| 71 | + MethodSymbol symbol = getSymbol(methodTree); |
| 72 | + if (symbol == null) { |
| 73 | + return NO_MATCH; |
| 74 | + } |
| 75 | + if (!findSuperMethods(symbol, state.getTypes()).isEmpty()) { |
| 76 | + return NO_MATCH; |
| 77 | + } |
| 78 | + if (isSimpleGetterOrSetter(methodTree)) { |
| 79 | + return NO_MATCH; |
| 80 | + } |
| 81 | + return checkJavadoc(methodTree, symbol, state); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public Description matchVariable(VariableTree variableTree, VisitorState state) { |
| 86 | + Symbol symbol = getSymbol(variableTree); |
| 87 | + if (symbol == null || symbol.getKind() != ElementKind.FIELD) { |
| 88 | + return NO_MATCH; |
| 89 | + } |
| 90 | + return checkJavadoc(variableTree, symbol, state); |
| 91 | + } |
| 92 | + |
| 93 | + private Description checkJavadoc(Tree tree, Symbol symbol, VisitorState state) { |
| 94 | + if (state.errorProneOptions().isTestOnlyTarget()) { |
| 95 | + return NO_MATCH; |
| 96 | + } |
| 97 | + if (isEffectivelyPrivate(symbol)) { |
| 98 | + return NO_MATCH; |
| 99 | + } |
| 100 | + if (!isEffectivelyPublicOrProtected(symbol)) { |
| 101 | + return NO_MATCH; |
| 102 | + } |
| 103 | + DocCommentTree docCommentTree = |
| 104 | + JavacTrees.instance(state.context).getDocCommentTree(state.getPath()); |
| 105 | + if (docCommentTree != null) { |
| 106 | + return NO_MATCH; |
| 107 | + } |
| 108 | + Description.Builder description = buildDescription(tree); |
| 109 | + if (tree instanceof ClassTree classTree |
| 110 | + && classTree.getSimpleName().toString().endsWith("Builder")) { |
| 111 | + ClassSymbol enclosing = enclosingClass(symbol); |
| 112 | + if (enclosing != null) { |
| 113 | + String suggestedJavadoc = |
| 114 | + String.format("/** A builder for {@link %s}. */\n", enclosing.getSimpleName()); |
| 115 | + description |
| 116 | + .setMessage( |
| 117 | + "Builder classes require Javadoc comments. Consider adding: %s", |
| 118 | + suggestedJavadoc.trim()) |
| 119 | + .addFix(SuggestedFix.prefixWith(tree, suggestedJavadoc)); |
| 120 | + } |
| 121 | + } |
| 122 | + return description.build(); |
| 123 | + } |
| 124 | + |
| 125 | + private static boolean isSimpleGetterOrSetter(MethodTree methodTree) { |
| 126 | + if (methodTree.getBody() == null) { |
| 127 | + return false; |
| 128 | + } |
| 129 | + List<? extends StatementTree> statements = methodTree.getBody().getStatements(); |
| 130 | + if (statements.size() != 1) { |
| 131 | + return false; |
| 132 | + } |
| 133 | + StatementTree stmt = statements.get(0); |
| 134 | + return switch (stmt) { |
| 135 | + case ReturnTree returnTree -> true; |
| 136 | + case ExpressionStatementTree expressionStatementTree -> |
| 137 | + expressionStatementTree.getExpression() instanceof AssignmentTree; |
| 138 | + default -> false; |
| 139 | + }; |
| 140 | + } |
| 141 | + |
| 142 | + private static final ImmutableSet<Modifier> PUBLIC_OR_PROTECTED = |
| 143 | + ImmutableSet.of(Modifier.PUBLIC, Modifier.PROTECTED); |
| 144 | + |
| 145 | + private static boolean isEffectivelyPublicOrProtected(Symbol symbol) { |
| 146 | + Symbol current = symbol; |
| 147 | + while (current != null) { |
| 148 | + if (current.getKind() == ElementKind.PACKAGE) { |
| 149 | + break; |
| 150 | + } |
| 151 | + if (Collections.disjoint(current.getModifiers(), PUBLIC_OR_PROTECTED)) { |
| 152 | + return false; |
| 153 | + } |
| 154 | + current = current.owner; |
| 155 | + } |
| 156 | + return true; |
| 157 | + } |
| 158 | +} |
0 commit comments