View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2018 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.CommonUtils;
27  import com.puppycrawl.tools.checkstyle.utils.JavadocUtils;
28  import com.puppycrawl.tools.checkstyle.utils.TokenUtils;
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   * @author <a href="mailto:simon@redhillconsulting.com.au">Simon Harris</a>
55   * @author Rick Giles
56   */
57  @StatelessCheck
58  public class IllegalTokenCheck
59      extends AbstractCheck {
60  
61      /**
62       * A key is pointing to the warning message text in "messages.properties"
63       * file.
64       */
65      public static final String MSG_KEY = "illegal.token";
66  
67      @Override
68      public int[] getDefaultTokens() {
69          return new int[] {
70              TokenTypes.LABELED_STAT,
71          };
72      }
73  
74      @Override
75      public int[] getAcceptableTokens() {
76          return TokenUtils.getAllTokenIds();
77      }
78  
79      @Override
80      public int[] getRequiredTokens() {
81          return CommonUtils.EMPTY_INT_ARRAY;
82      }
83  
84      @Override
85      public boolean isCommentNodesRequired() {
86          return true;
87      }
88  
89      @Override
90      public void visitToken(DetailAST ast) {
91          log(
92              ast.getLineNo(),
93              ast.getColumnNo(),
94              MSG_KEY,
95              convertToString(ast)
96          );
97      }
98  
99      /**
100      * Converts given AST node to string representation.
101      * @param ast node to be represented as string
102      * @return string representation of AST node
103      */
104     private static String convertToString(DetailAST ast) {
105         final String tokenText;
106         switch (ast.getType()) {
107             case TokenTypes.LABELED_STAT:
108                 tokenText = ast.getFirstChild().getText() + ast.getText();
109                 break;
110             // multiline tokens need to become singlelined
111             case TokenTypes.COMMENT_CONTENT:
112                 tokenText = JavadocUtils.escapeAllControlChars(ast.getText());
113                 break;
114             default:
115                 tokenText = ast.getText();
116                 break;
117         }
118         return tokenText;
119     }
120 
121 }