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