001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2024 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 * Type is {@code anyTokenTypesSet}.
044 * Default value is
045 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LABELED_STAT">
046 * LABELED_STAT</a>.
047 * </li>
048 * </ul>
049 * <p>
050 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
051 * </p>
052 * <p>
053 * Violation Message Keys:
054 * </p>
055 * <ul>
056 * <li>
057 * {@code illegal.token}
058 * </li>
059 * </ul>
060 *
061 * @since 3.2
062 */
063@StatelessCheck
064public class IllegalTokenCheck
065    extends AbstractCheck {
066
067    /**
068     * A key is pointing to the warning message text in "messages.properties"
069     * file.
070     */
071    public static final String MSG_KEY = "illegal.token";
072
073    @Override
074    public int[] getDefaultTokens() {
075        return new int[] {
076            TokenTypes.LABELED_STAT,
077        };
078    }
079
080    @Override
081    public int[] getAcceptableTokens() {
082        return TokenUtil.getAllTokenIds();
083    }
084
085    @Override
086    public int[] getRequiredTokens() {
087        return CommonUtil.EMPTY_INT_ARRAY;
088    }
089
090    @Override
091    public boolean isCommentNodesRequired() {
092        return true;
093    }
094
095    @Override
096    public void visitToken(DetailAST ast) {
097        log(
098            ast,
099            MSG_KEY,
100            convertToString(ast)
101        );
102    }
103
104    /**
105     * Converts given AST node to string representation.
106     *
107     * @param ast node to be represented as string
108     * @return string representation of AST node
109     */
110    private static String convertToString(DetailAST ast) {
111        final String tokenText;
112        switch (ast.getType()) {
113            case TokenTypes.LABELED_STAT:
114                tokenText = ast.getFirstChild().getText() + ast.getText();
115                break;
116            // multiline tokens need to become singlelined
117            case TokenTypes.COMMENT_CONTENT:
118                tokenText = JavadocUtil.escapeAllControlChars(ast.getText());
119                break;
120            default:
121                tokenText = ast.getText();
122                break;
123        }
124        return tokenText;
125    }
126
127}