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