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 com.puppycrawl.tools.checkstyle.StatelessCheck;
23 import com.puppycrawl.tools.checkstyle.api.DetailNode;
24 import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes;
25 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
26 import com.puppycrawl.tools.checkstyle.utils.JavadocUtil;
27
28 /**
29 * <div>
30 * Checks that the block tag is followed by description.
31 * </div>
32 *
33 * <ul>
34 * <li>
35 * Property {@code violateExecutionOnNonTightHtml} - Control when to print violations
36 * if the Javadoc being examined by this check violates the tight html rules defined at
37 * <a href="https://checkstyle.org/writingjavadocchecks.html#Tight-HTML_rules">Tight-HTML Rules</a>.
38 * Type is {@code boolean}.
39 * Default value is {@code false}.
40 * </li>
41 * <li>
42 * Property {@code javadocTokens} - javadoc tokens to check
43 * Type is {@code java.lang.String[]}.
44 * Validation type is {@code tokenSet}.
45 * Default value is
46 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/JavadocTokenTypes.html#PARAM_LITERAL">
47 * PARAM_LITERAL</a>,
48 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/JavadocTokenTypes.html#RETURN_LITERAL">
49 * RETURN_LITERAL</a>,
50 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/JavadocTokenTypes.html#THROWS_LITERAL">
51 * THROWS_LITERAL</a>,
52 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/JavadocTokenTypes.html#EXCEPTION_LITERAL">
53 * EXCEPTION_LITERAL</a>,
54 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/JavadocTokenTypes.html#DEPRECATED_LITERAL">
55 * DEPRECATED_LITERAL</a>.
56 * </li>
57 * </ul>
58 *
59 * <p>
60 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
61 * </p>
62 *
63 * <p>
64 * Violation Message Keys:
65 * </p>
66 * <ul>
67 * <li>
68 * {@code javadoc.missed.html.close}
69 * </li>
70 * <li>
71 * {@code javadoc.parse.rule.error}
72 * </li>
73 * <li>
74 * {@code javadoc.unclosedHtml}
75 * </li>
76 * <li>
77 * {@code javadoc.wrong.singleton.html.tag}
78 * </li>
79 * <li>
80 * {@code non.empty.atclause}
81 * </li>
82 * </ul>
83 *
84 * @since 6.0
85 */
86 @StatelessCheck
87 public class NonEmptyAtclauseDescriptionCheck extends AbstractJavadocCheck {
88
89 /**
90 * A key is pointing to the warning message text in "messages.properties"
91 * file.
92 */
93 public static final String MSG_KEY = "non.empty.atclause";
94
95 @Override
96 public int[] getDefaultJavadocTokens() {
97 return new int[] {
98 JavadocTokenTypes.PARAM_LITERAL,
99 JavadocTokenTypes.RETURN_LITERAL,
100 JavadocTokenTypes.THROWS_LITERAL,
101 JavadocTokenTypes.EXCEPTION_LITERAL,
102 JavadocTokenTypes.DEPRECATED_LITERAL,
103 };
104 }
105
106 @Override
107 public void visitJavadocToken(DetailNode ast) {
108 if (isEmptyTag(ast.getParent())) {
109 log(ast.getLineNumber(), MSG_KEY);
110 }
111 }
112
113 /**
114 * Tests if block tag is empty.
115 *
116 * @param tagNode block tag.
117 * @return true, if block tag is empty.
118 */
119 private static boolean isEmptyTag(DetailNode tagNode) {
120 final DetailNode tagDescription =
121 JavadocUtil.findFirstToken(tagNode, JavadocTokenTypes.DESCRIPTION);
122 return tagDescription == null
123 || hasOnlyEmptyText(tagDescription);
124 }
125
126 /**
127 * Tests if description node is empty (has only new lines and blank strings).
128 *
129 * @param description description node.
130 * @return true, if description node has only new lines and blank strings.
131 */
132 private static boolean hasOnlyEmptyText(DetailNode description) {
133 boolean result = true;
134 for (DetailNode child : description.getChildren()) {
135 if (child.getType() != JavadocTokenTypes.LEADING_ASTERISK
136 && !CommonUtil.isBlank(child.getText())) {
137 result = false;
138 break;
139 }
140 }
141 return result;
142 }
143
144 }