1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2024 the original author or authors.
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ///////////////////////////////////////////////////////////////////////////////////////////////
19
20 package com.puppycrawl.tools.checkstyle.checks.javadoc;
21
22 import java.util.Optional;
23
24 import com.puppycrawl.tools.checkstyle.StatelessCheck;
25 import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
26 import com.puppycrawl.tools.checkstyle.api.DetailAST;
27 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
28 import com.puppycrawl.tools.checkstyle.utils.CheckUtil;
29 import com.puppycrawl.tools.checkstyle.utils.JavadocUtil;
30
31 /**
32 * <p>
33 * Checks for missing package definition Javadoc comments in package-info.java files.
34 * </p>
35 * <p>
36 * Rationale: description and other related documentation for a package can be written up
37 * in the package-info.java file and it gets used in the production of the Javadocs.
38 * See <a
39 * href="https://docs.oracle.com/javase/8/docs/technotes/tools/windows/javadoc.html#packagecomment"
40 * >link</a> for more info.
41 * </p>
42 * <p>
43 * This check specifically only validates package definitions. It will not validate any methods or
44 * interfaces declared in the package-info.java file.
45 * </p>
46 * <p>
47 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
48 * </p>
49 * <p>
50 * Violation Message Keys:
51 * </p>
52 * <ul>
53 * <li>
54 * {@code package.javadoc.missing}
55 * </li>
56 * </ul>
57 *
58 * @since 8.22
59 */
60 @StatelessCheck
61 public class MissingJavadocPackageCheck extends AbstractCheck {
62
63 /**
64 * A key is pointing to the warning message text in "messages.properties"
65 * file.
66 */
67 public static final String MSG_PKG_JAVADOC_MISSING = "package.javadoc.missing";
68
69 @Override
70 public int[] getDefaultTokens() {
71 return getRequiredTokens();
72 }
73
74 @Override
75 public int[] getAcceptableTokens() {
76 return getRequiredTokens();
77 }
78
79 @Override
80 public int[] getRequiredTokens() {
81 return new int[] {TokenTypes.PACKAGE_DEF};
82 }
83
84 @Override
85 public boolean isCommentNodesRequired() {
86 return true;
87 }
88
89 @Override
90 public void visitToken(DetailAST ast) {
91 if (CheckUtil.isPackageInfo(getFilePath()) && !hasJavadoc(ast)) {
92 log(ast, MSG_PKG_JAVADOC_MISSING);
93 }
94 }
95
96 /**
97 * Checks that there is javadoc before ast.
98 * Because of <a href="https://github.com/checkstyle/checkstyle/issues/4392">parser bug</a>
99 * parser can place javadoc comment either as previous sibling of package definition
100 * or (if there is annotation between package def and javadoc) inside package definition tree.
101 * So we should look for javadoc in both places.
102 *
103 * @param ast {@link TokenTypes#PACKAGE_DEF} token to check
104 * @return true if there is javadoc, false otherwise
105 */
106 private static boolean hasJavadoc(DetailAST ast) {
107 final boolean hasJavadocBefore = ast.getPreviousSibling() != null
108 && isJavadoc(ast.getPreviousSibling());
109 return hasJavadocBefore || hasJavadocAboveAnnotation(ast);
110 }
111
112 /**
113 * Checks javadoc existence in annotations list.
114 *
115 * @param ast package def
116 * @return true if there is a javadoc, false otherwise
117 */
118 private static boolean hasJavadocAboveAnnotation(DetailAST ast) {
119 final Optional<DetailAST> firstAnnotationChild = Optional.of(ast.getFirstChild())
120 .map(DetailAST::getFirstChild)
121 .map(DetailAST::getFirstChild);
122 boolean result = false;
123 if (firstAnnotationChild.isPresent()) {
124 for (DetailAST child = firstAnnotationChild.orElseThrow(); child != null;
125 child = child.getNextSibling()) {
126 if (isJavadoc(child)) {
127 result = true;
128 break;
129 }
130 }
131 }
132 return result;
133 }
134
135 /**
136 * Checks that ast is a javadoc comment.
137 *
138 * @param ast token to check
139 * @return true if ast is a javadoc comment, false otherwise
140 */
141 private static boolean isJavadoc(DetailAST ast) {
142 return ast.getType() == TokenTypes.BLOCK_COMMENT_BEGIN && JavadocUtil.isJavadocComment(ast);
143 }
144 }