001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2020 the original author or authors.
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.checks.coding;
021
022import com.puppycrawl.tools.checkstyle.StatelessCheck;
023import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
024import com.puppycrawl.tools.checkstyle.api.DetailAST;
025import com.puppycrawl.tools.checkstyle.api.TokenTypes;
026import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
027import com.puppycrawl.tools.checkstyle.utils.JavadocUtil;
028import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
029
030/**
031 * <p>
032 * Checks for illegal tokens. By default labels are prohibited.
033 * </p>
034 * <p>
035 * Rationale: Certain language features can harm readability, lead to
036 * confusion or are not obvious to novice developers. Other features
037 * may be discouraged in certain frameworks, such as not having
038 * native methods in Enterprise JavaBeans components.
039 * </p>
040 * <ul>
041 * <li>
042 * Property {@code tokens} - tokens to check
043 * Default value is:
044 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LABELED_STAT">
045 * LABELED_STAT</a>.
046 * </li>
047 * </ul>
048 * <p>
049 * To configure the check:
050 * </p>
051 * <pre>
052 * &lt;module name=&quot;IllegalToken&quot;/&gt;
053 * </pre>
054 * <p>Example:</p>
055 * <pre>
056 * public void myTest() {
057 *     outer: // violation
058 *     for (int i = 0; i &lt; 5; i++) {
059 *         if (i == 1) {
060 *             break outer;
061 *         }
062 *     }
063 * }
064 * </pre>
065 * <p>
066 * To configure the check to report violation on token LITERAL_NATIVE:
067 * </p>
068 * <pre>
069 * &lt;module name=&quot;IllegalToken&quot;&gt;
070 *   &lt;property name=&quot;tokens&quot; value=&quot;LITERAL_NATIVE&quot;/&gt;
071 * &lt;/module&gt;
072 * </pre>
073 * <p>Example:</p>
074 * <pre>
075 * public native void myTest(); // violation
076 * </pre>
077 *
078 * @since 3.2
079 */
080@StatelessCheck
081public class IllegalTokenCheck
082    extends AbstractCheck {
083
084    /**
085     * A key is pointing to the warning message text in "messages.properties"
086     * file.
087     */
088    public static final String MSG_KEY = "illegal.token";
089
090    @Override
091    public int[] getDefaultTokens() {
092        return new int[] {
093            TokenTypes.LABELED_STAT,
094        };
095    }
096
097    @Override
098    public int[] getAcceptableTokens() {
099        return TokenUtil.getAllTokenIds();
100    }
101
102    @Override
103    public int[] getRequiredTokens() {
104        return CommonUtil.EMPTY_INT_ARRAY;
105    }
106
107    @Override
108    public boolean isCommentNodesRequired() {
109        return true;
110    }
111
112    @Override
113    public void visitToken(DetailAST ast) {
114        log(
115            ast,
116            MSG_KEY,
117            convertToString(ast)
118        );
119    }
120
121    /**
122     * Converts given AST node to string representation.
123     *
124     * @param ast node to be represented as string
125     * @return string representation of AST node
126     */
127    private static String convertToString(DetailAST ast) {
128        final String tokenText;
129        switch (ast.getType()) {
130            case TokenTypes.LABELED_STAT:
131                tokenText = ast.getFirstChild().getText() + ast.getText();
132                break;
133            // multiline tokens need to become singlelined
134            case TokenTypes.COMMENT_CONTENT:
135                tokenText = JavadocUtil.escapeAllControlChars(ast.getText());
136                break;
137            default:
138                tokenText = ast.getText();
139                break;
140        }
141        return tokenText;
142    }
143
144}