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.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   * <ul>
42   * <li>
43   * Property {@code tokens} - tokens to check
44   * Type is {@code anyTokenTypesSet}.
45   * Default value is
46   * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LABELED_STAT">
47   * LABELED_STAT</a>.
48   * </li>
49   * </ul>
50   *
51   * <p>
52   * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
53   * </p>
54   *
55   * <p>
56   * Violation Message Keys:
57   * </p>
58   * <ul>
59   * <li>
60   * {@code illegal.token}
61   * </li>
62   * </ul>
63   *
64   * @since 3.2
65   */
66  @StatelessCheck
67  public class IllegalTokenCheck
68      extends AbstractCheck {
69  
70      /**
71       * A key is pointing to the warning message text in "messages.properties"
72       * file.
73       */
74      public static final String MSG_KEY = "illegal.token";
75  
76      @Override
77      public int[] getDefaultTokens() {
78          return new int[] {
79              TokenTypes.LABELED_STAT,
80          };
81      }
82  
83      @Override
84      public int[] getAcceptableTokens() {
85          return TokenUtil.getAllTokenIds();
86      }
87  
88      @Override
89      public int[] getRequiredTokens() {
90          return CommonUtil.EMPTY_INT_ARRAY;
91      }
92  
93      @Override
94      public boolean isCommentNodesRequired() {
95          return true;
96      }
97  
98      @Override
99      public void visitToken(DetailAST ast) {
100         log(
101             ast,
102             MSG_KEY,
103             convertToString(ast)
104         );
105     }
106 
107     /**
108      * Converts given AST node to string representation.
109      *
110      * @param ast node to be represented as string
111      * @return string representation of AST node
112      */
113     private static String convertToString(DetailAST ast) {
114         final String tokenText;
115         switch (ast.getType()) {
116             case TokenTypes.LABELED_STAT:
117                 tokenText = ast.getFirstChild().getText() + ast.getText();
118                 break;
119             // multiline tokens need to become singlelined
120             case TokenTypes.COMMENT_CONTENT:
121                 tokenText = JavadocUtil.escapeAllControlChars(ast.getText());
122                 break;
123             default:
124                 tokenText = ast.getText();
125                 break;
126         }
127         return tokenText;
128     }
129 
130 }