1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2019 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   * <p>
32   * Checks for illegal tokens. By default labels are prohibited.
33   * </p>
34   * <p>
35   * Rationale: Certain language features can harm readability, lead to
36   * confusion or are not obvious to novice developers. Other features
37   * may be discouraged in certain frameworks, such as not having
38   * native methods in EJB components.
39   * </p>
40   * <p>
41   * An example of how to configure the check is:
42   * </p>
43   * <pre>
44   * &lt;module name="IllegalToken"/&gt;
45   * </pre>
46   * <p> An example of how to configure the check to forbid
47   * a {@link TokenTypes#LITERAL_NATIVE LITERAL_NATIVE} token is:
48   * </p>
49   * <pre>
50   * &lt;module name="IllegalToken"&gt;
51   *     &lt;property name="tokens" value="LITERAL_NATIVE"/&gt;
52   * &lt;/module&gt;
53   * </pre>
54   */
55  @StatelessCheck
56  public class IllegalTokenCheck
57      extends AbstractCheck {
58  
59      /**
60       * A key is pointing to the warning message text in "messages.properties"
61       * file.
62       */
63      public static final String MSG_KEY = "illegal.token";
64  
65      @Override
66      public int[] getDefaultTokens() {
67          return new int[] {
68              TokenTypes.LABELED_STAT,
69          };
70      }
71  
72      @Override
73      public int[] getAcceptableTokens() {
74          return TokenUtil.getAllTokenIds();
75      }
76  
77      @Override
78      public int[] getRequiredTokens() {
79          return CommonUtil.EMPTY_INT_ARRAY;
80      }
81  
82      @Override
83      public boolean isCommentNodesRequired() {
84          return true;
85      }
86  
87      @Override
88      public void visitToken(DetailAST ast) {
89          log(
90              ast,
91              MSG_KEY,
92              convertToString(ast)
93          );
94      }
95  
96      /**
97       * Converts given AST node to string representation.
98       * @param ast node to be represented as string
99       * @return string representation of AST node
100      */
101     private static String convertToString(DetailAST ast) {
102         final String tokenText;
103         switch (ast.getType()) {
104             case TokenTypes.LABELED_STAT:
105                 tokenText = ast.getFirstChild().getText() + ast.getText();
106                 break;
107             // multiline tokens need to become singlelined
108             case TokenTypes.COMMENT_CONTENT:
109                 tokenText = JavadocUtil.escapeAllControlChars(ast.getText());
110                 break;
111             default:
112                 tokenText = ast.getText();
113                 break;
114         }
115         return tokenText;
116     }
117 
118 }