001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2026 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 * <div>
032 * Checks for illegal tokens. By default, labels are prohibited.
033 * </div>
034 *
035 * <p>
036 * Rationale: Certain language features can harm readability, lead to
037 * confusion or are not obvious to novice developers. Other features
038 * may be discouraged in certain frameworks, such as not having
039 * native methods in Enterprise JavaBeans components.
040 * </p>
041 *
042 * @since 3.2
043 */
044@StatelessCheck
045public class IllegalTokenCheck
046    extends AbstractCheck {
047
048    /**
049     * A key is pointing to the warning message text in "messages.properties"
050     * file.
051     */
052    public static final String MSG_KEY = "illegal.token";
053
054    /**
055     * Creates a new {@code IllegalTokenCheck} instance.
056     */
057    public IllegalTokenCheck() {
058        // no code by default
059    }
060
061    @Override
062    public int[] getDefaultTokens() {
063        return new int[] {
064            TokenTypes.LABELED_STAT,
065        };
066    }
067
068    @Override
069    public int[] getAcceptableTokens() {
070        return TokenUtil.getAllTokenIds();
071    }
072
073    @Override
074    public int[] getRequiredTokens() {
075        return CommonUtil.EMPTY_INT_ARRAY;
076    }
077
078    @Override
079    public boolean isCommentNodesRequired() {
080        return true;
081    }
082
083    @Override
084    public void visitToken(DetailAST ast) {
085        log(
086            ast,
087            MSG_KEY,
088            convertToString(ast)
089        );
090    }
091
092    /**
093     * Converts given AST node to string representation.
094     *
095     * @param ast node to be represented as string
096     * @return string representation of AST node
097     */
098    private static String convertToString(DetailAST ast) {
099        return switch (ast.getType()) {
100            case TokenTypes.LABELED_STAT -> ast.getFirstChild().getText() + ast.getText();
101            // multiline tokens need to become singlelined
102            case TokenTypes.COMMENT_CONTENT -> JavadocUtil.escapeAllControlChars(ast.getText());
103            default -> ast.getText();
104        };
105    }
106
107}