View Javadoc
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  import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
28  
29  /**
30   * <div>
31   * Checks if the javadoc has
32   * <a href="https://docs.oracle.com/en/java/javase/14/docs/specs/javadoc/doc-comment-spec.html#leading-asterisks">
33   * leading asterisks</a> on each line.
34   * </div>
35   *
36   * <p>
37   * The check does not require asterisks on the first line, nor on the last line if it is blank.
38   * All other lines in a Javadoc should start with {@code *}, including blank lines and code blocks.
39   * </p>
40   * <ul>
41   * <li>
42   * Property {@code violateExecutionOnNonTightHtml} - Control when to print violations if the
43   * Javadoc being examined by this check violates the tight html rules defined at
44   * <a href="https://checkstyle.org/writingjavadocchecks.html#Tight-HTML_rules">Tight-HTML Rules</a>.
45   * Type is {@code boolean}.
46   * Default value is {@code false}.
47   * </li>
48   * </ul>
49   *
50   * <p>
51   * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
52   * </p>
53   *
54   * <p>
55   * Violation Message Keys:
56   * </p>
57   * <ul>
58   * <li>
59   * {@code javadoc.missed.html.close}
60   * </li>
61   * <li>
62   * {@code javadoc.missing.asterisk}
63   * </li>
64   * <li>
65   * {@code javadoc.parse.rule.error}
66   * </li>
67   * <li>
68   * {@code javadoc.unclosedHtml}
69   * </li>
70   * <li>
71   * {@code javadoc.wrong.singleton.html.tag}
72   * </li>
73   * </ul>
74   *
75   * @since 8.38
76   */
77  @StatelessCheck
78  public class JavadocMissingLeadingAsteriskCheck extends AbstractJavadocCheck {
79  
80      /**
81       * A key is pointing to the warning message text in "messages.properties"
82       * file.
83       */
84      public static final String MSG_MISSING_ASTERISK = "javadoc.missing.asterisk";
85  
86      @Override
87      public int[] getRequiredJavadocTokens() {
88          return new int[] {
89              JavadocTokenTypes.NEWLINE,
90          };
91      }
92  
93      @Override
94      public int[] getAcceptableJavadocTokens() {
95          return getRequiredJavadocTokens();
96      }
97  
98      @Override
99      public int[] getDefaultJavadocTokens() {
100         return getRequiredJavadocTokens();
101     }
102 
103     @Override
104     public void visitJavadocToken(DetailNode detailNode) {
105         DetailNode nextSibling = getNextNode(detailNode);
106 
107         // Till https://github.com/checkstyle/checkstyle/issues/9005
108         // Due to bug in the Javadoc parser there may be phantom description nodes.
109         while (TokenUtil.isOfType(nextSibling.getType(),
110                 JavadocTokenTypes.DESCRIPTION, JavadocTokenTypes.WS)) {
111             nextSibling = getNextNode(nextSibling);
112         }
113 
114         if (!isLeadingAsterisk(nextSibling) && !isLastLine(nextSibling)) {
115             log(nextSibling.getLineNumber(), MSG_MISSING_ASTERISK);
116         }
117     }
118 
119     /**
120      * Gets next node in the ast (sibling or parent sibling for the last node).
121      *
122      * @param detailNode the node to process
123      * @return next node.
124      */
125     private static DetailNode getNextNode(DetailNode detailNode) {
126         DetailNode node = JavadocUtil.getFirstChild(detailNode);
127         if (node == null) {
128             node = JavadocUtil.getNextSibling(detailNode);
129             if (node == null) {
130                 DetailNode parent = detailNode;
131                 do {
132                     parent = parent.getParent();
133                     node = JavadocUtil.getNextSibling(parent);
134                 } while (node == null);
135             }
136         }
137         return node;
138     }
139 
140     /**
141      * Checks whether the given node is a leading asterisk.
142      *
143      * @param detailNode the node to process
144      * @return {@code true} if the node is {@link JavadocTokenTypes#LEADING_ASTERISK}
145      */
146     private static boolean isLeadingAsterisk(DetailNode detailNode) {
147         return detailNode.getType() == JavadocTokenTypes.LEADING_ASTERISK;
148     }
149 
150     /**
151      * Checks whether this node is the end of a Javadoc comment,
152      * optionally preceded by blank text.
153      *
154      * @param detailNode the node to process
155      * @return {@code true} if the node is {@link JavadocTokenTypes#EOF}
156      */
157     private static boolean isLastLine(DetailNode detailNode) {
158         final DetailNode node;
159         if (CommonUtil.isBlank(detailNode.getText())) {
160             node = getNextNode(detailNode);
161         }
162         else {
163             node = detailNode;
164         }
165         return node.getType() == JavadocTokenTypes.EOF;
166     }
167 
168 }