View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2025 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.qulice.checkstyle;
6   
7   import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
8   import com.puppycrawl.tools.checkstyle.api.DetailAST;
9   import com.puppycrawl.tools.checkstyle.api.TokenTypes;
10  import java.util.regex.Matcher;
11  import java.util.regex.Pattern;
12  
13  /**
14   * Multi line comment checker.
15   * Used by the checkstyle process multiple times as a singleton.
16   * @since 0.23.1
17   */
18  public final class MultiLineCommentCheck extends AbstractCheck {
19      /**
20       * Pattern for check.
21       * It is not final as it is initialized from the configuration.
22       */
23      private Pattern format = Pattern.compile("^$");
24  
25      /**
26       * The message to report for a match.
27       * It is not final as it is initialized from the configuration.
28       */
29      private String message = "";
30  
31      /**
32       * Comment line.
33       * It is not final because the visitToken method is called many times
34       * during the class under test and the field is reinitialized with a new object.
35       */
36      @SuppressWarnings("PMD.AvoidStringBufferField")
37      private StringBuilder text;
38  
39      @Override
40      public boolean isCommentNodesRequired() {
41          return true;
42      }
43  
44      @Override
45      public int[] getDefaultTokens() {
46          return new int[]{
47              TokenTypes.BLOCK_COMMENT_BEGIN,
48              TokenTypes.COMMENT_CONTENT,
49              TokenTypes.BLOCK_COMMENT_END,
50          };
51      }
52  
53      @Override
54      public int[] getAcceptableTokens() {
55          return this.getDefaultTokens();
56      }
57  
58      @Override
59      public int[] getRequiredTokens() {
60          return this.getDefaultTokens();
61      }
62  
63      @Override
64      public void visitToken(final DetailAST ast) {
65          if (ast.getType() == TokenTypes.BLOCK_COMMENT_BEGIN) {
66              this.text = new StringBuilder(ast.getText());
67          } else if (ast.getType() == TokenTypes.COMMENT_CONTENT) {
68              this.text.append(ast.getText());
69          } else {
70              this.text.append(ast.getText());
71              final Matcher matcher = this.format.matcher(this.text.toString());
72              if (matcher.matches()) {
73                  this.log(ast, this.message);
74              }
75          }
76      }
77  
78      /**
79       * The method is called from checkstyle to configure this class.
80       * The parameter is set from the checks.xml file
81       * <module name="com.qulice.checkstyle.MultiLineCommentCheck"/> and
82       * <property name="format" value=" this regexp "/> property
83       *
84       * @param fmt Validatig regexp.
85       */
86      public void setFormat(final String fmt) {
87          this.format = Pattern.compile(fmt);
88      }
89  
90      /**
91       * The method is called from checkstyle to configure this class.
92       * The parameter is set from the checks.xml file
93       * <module name="com.qulice.checkstyle.MultiLineCommentCheck"/> and
94       * <property name="message" value="First sentence in a comment should start with ....."/>
95       * property
96       *
97       * @param msg Error message.
98       */
99      public void setMessage(final String msg) {
100         this.message = msg;
101     }
102 }