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.ScopeUtil;
028
029/**
030 * <p>
031 * Checks if unnecessary semicolon is used after type declaration.
032 * </p>
033 * <p>
034 * This check is not applicable to nested type declarations,
035 * <a
036 * href="https://checkstyle.org/checks/coding/unnecessarysemicolonaftertypememberdeclaration.html">
037 * UnnecessarySemicolonAfterTypeMemberDeclaration</a> is responsible for it.
038 * </p>
039 * <ul>
040 * <li>
041 * Property {@code tokens} - tokens to check
042 * Type is {@code java.lang.String[]}.
043 * Validation type is {@code tokenSet}.
044 * Default value is:
045 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CLASS_DEF">
046 * CLASS_DEF</a>,
047 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#INTERFACE_DEF">
048 * INTERFACE_DEF</a>,
049 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ENUM_DEF">
050 * ENUM_DEF</a>,
051 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ANNOTATION_DEF">
052 * ANNOTATION_DEF</a>,
053 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#RECORD_DEF">
054 * RECORD_DEF</a>.
055 * </li>
056 * </ul>
057 * <p>
058 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
059 * </p>
060 * <p>
061 * Violation Message Keys:
062 * </p>
063 * <ul>
064 * <li>
065 * {@code unnecessary.semicolon}
066 * </li>
067 * </ul>
068 *
069 * @since 8.31
070 */
071@StatelessCheck
072public final class UnnecessarySemicolonAfterOuterTypeDeclarationCheck extends AbstractCheck {
073
074    /**
075     * A key is pointing to the warning message text in "messages.properties"
076     * file.
077     */
078    public static final String MSG_SEMI = "unnecessary.semicolon";
079
080    @Override
081    public int[] getDefaultTokens() {
082        return getAcceptableTokens();
083    }
084
085    @Override
086    public int[] getAcceptableTokens() {
087        return new int[] {
088            TokenTypes.CLASS_DEF,
089            TokenTypes.INTERFACE_DEF,
090            TokenTypes.ENUM_DEF,
091            TokenTypes.ANNOTATION_DEF,
092            TokenTypes.RECORD_DEF,
093        };
094    }
095
096    @Override
097    public int[] getRequiredTokens() {
098        return CommonUtil.EMPTY_INT_ARRAY;
099    }
100
101    @Override
102    public void visitToken(DetailAST ast) {
103        final DetailAST nextSibling = ast.getNextSibling();
104        if (nextSibling != null
105                && ScopeUtil.isOuterMostType(ast)
106                && nextSibling.getType() == TokenTypes.SEMI) {
107            log(nextSibling, MSG_SEMI);
108        }
109    }
110}