View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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;
21  
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import com.puppycrawl.tools.checkstyle.StatelessCheck;
26  import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
27  import com.puppycrawl.tools.checkstyle.api.DetailAST;
28  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
29  import com.puppycrawl.tools.checkstyle.utils.JavadocUtil;
30  
31  /**
32   * <div>
33   * Checks if the
34   * <a href="https://www.oracle.com/java/technologies/javase/codeconventions-comments.html">
35   * multi-line comments</a> have leading asterisks on each line.
36   * </div>
37   *
38   * <p>
39   * Every line in multi-line comment should have leading asterisk including blank line.
40   * </p>
41   *
42   * @since 13.9.0
43   */
44  @StatelessCheck
45  public class MultilineCommentLeadingAsteriskPresenceCheck extends AbstractCheck {
46  
47      /**
48       * A key is pointing to the warning message text in "messages.properties"
49       * file.
50       */
51      public static final String MSG_MISSING_ASTERISK = "multiline.comment.missing.asterisk";
52  
53      /**
54       * Creates a new {@code MultilineCommentLeadingAsteriskPresenceCheck} instance.
55       */
56      public MultilineCommentLeadingAsteriskPresenceCheck() {
57          // no code by default
58      }
59  
60      @Override
61      public int[] getRequiredTokens() {
62          return new int[] {
63              TokenTypes.BLOCK_COMMENT_BEGIN,
64          };
65      }
66  
67      @Override
68      public int[] getAcceptableTokens() {
69          return getRequiredTokens();
70      }
71  
72      @Override
73      public int[] getDefaultTokens() {
74          return getRequiredTokens();
75      }
76  
77      @Override
78      public boolean isCommentNodesRequired() {
79          return true;
80      }
81  
82      @Override
83      public void visitToken(DetailAST ast) {
84          if (!JavadocUtil.isJavadocComment(ast)) {
85              final String commentText = JavadocUtil.getBlockCommentContent(ast);
86              int lineNumber = ast.getLineNo();
87              final int commentEndLineNumber = ast.getLastChild().getLineNo();
88              final List<String> lines = commentText.lines().toList();
89              final List<Integer> nonAsteriskLineNumbers = new ArrayList<>();
90              for (int cur = 1; cur < lines.size(); cur++) {
91                  lineNumber++;
92                  final String line = lines.get(cur).trim();
93                  // is block comment end
94                  if (lineNumber == commentEndLineNumber && line.isEmpty()) {
95                      continue;
96                  }
97                  if (!line.startsWith("*")) {
98                      nonAsteriskLineNumbers.add(lineNumber);
99                  }
100             }
101 
102             if (!nonAsteriskLineNumbers.isEmpty()) {
103                 final String allLineNumbers = nonAsteriskLineNumbers.toString();
104                 log(ast, MSG_MISSING_ASTERISK, allLineNumbers.substring(1,
105                         allLineNumbers.length() - 1));
106             }
107         }
108     }
109 
110 }