View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 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.coding;
21  
22  import com.puppycrawl.tools.checkstyle.StatelessCheck;
23  import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
24  import com.puppycrawl.tools.checkstyle.api.DetailAST;
25  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
26  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
27  import com.puppycrawl.tools.checkstyle.utils.JavadocUtil;
28  import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
29  
30  /**
31   * <div>
32   * Checks for illegal tokens. By default, labels are prohibited.
33   * </div>
34   *
35   * <p>
36   * Rationale: Certain language features can harm readability, lead to
37   * confusion or are not obvious to novice developers. Other features
38   * may be discouraged in certain frameworks, such as not having
39   * native methods in Enterprise JavaBeans components.
40   * </p>
41   *
42   * @since 3.2
43   */
44  @StatelessCheck
45  public class IllegalTokenCheck
46      extends AbstractCheck {
47  
48      /**
49       * A key is pointing to the warning message text in "messages.properties"
50       * file.
51       */
52      public static final String MSG_KEY = "illegal.token";
53  
54      @Override
55      public int[] getDefaultTokens() {
56          return new int[] {
57              TokenTypes.LABELED_STAT,
58          };
59      }
60  
61      @Override
62      public int[] getAcceptableTokens() {
63          return TokenUtil.getAllTokenIds();
64      }
65  
66      @Override
67      public int[] getRequiredTokens() {
68          return CommonUtil.EMPTY_INT_ARRAY;
69      }
70  
71      @Override
72      public boolean isCommentNodesRequired() {
73          return true;
74      }
75  
76      @Override
77      public void visitToken(DetailAST ast) {
78          log(
79              ast,
80              MSG_KEY,
81              convertToString(ast)
82          );
83      }
84  
85      /**
86       * Converts given AST node to string representation.
87       *
88       * @param ast node to be represented as string
89       * @return string representation of AST node
90       */
91      private static String convertToString(DetailAST ast) {
92          return switch (ast.getType()) {
93              case TokenTypes.LABELED_STAT -> ast.getFirstChild().getText() + ast.getText();
94              // multiline tokens need to become singlelined
95              case TokenTypes.COMMENT_CONTENT -> JavadocUtil.escapeAllControlChars(ast.getText());
96              default -> ast.getText();
97          };
98      }
99  
100 }