001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2022 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/config_coding.html#UnnecessarySemicolonAfterTypeMemberDeclaration">
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 * To configure the check:
059 * </p>
060 * <pre>
061 * &lt;module name=&quot;UnnecessarySemicolonAfterOuterTypeDeclaration&quot;/&gt;
062 * </pre>
063 * <p>Example:</p>
064 * <pre>
065 * class A {
066 *
067 *     class Nested {
068 *
069 *     }; // OK, nested type declarations are ignored
070 *
071 * }; // violation
072 *
073 * interface B {
074 *
075 * }; // violation
076 *
077 * enum C {
078 *
079 * }; // violation
080 *
081 * {@literal @}interface D {
082 *
083 * }; // violation
084 * </pre>
085 * <p>
086 * To configure the check to detect unnecessary semicolon only after top level class definitions:
087 * </p>
088 * <pre>
089 * &lt;module name=&quot;UnnecessarySemicolonAfterOuterTypeDeclaration&quot;&gt;
090 *   &lt;property name=&quot;tokens&quot; value=&quot;CLASS_DEF&quot;/&gt;
091 * &lt;/module&gt;
092 * </pre>
093 * <p>Example:</p>
094 * <pre>
095 * class A {
096 *
097 * }; // violation
098 *
099 * interface B {
100 *
101 * }; // OK
102 *
103 * enum C {
104 *
105 * }; // OK
106 *
107 * {@literal @}interface D {
108 *
109 * }; // OK
110 * </pre>
111 * <p>
112 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
113 * </p>
114 * <p>
115 * Violation Message Keys:
116 * </p>
117 * <ul>
118 * <li>
119 * {@code unnecessary.semicolon}
120 * </li>
121 * </ul>
122 *
123 * @since 8.31
124 */
125@StatelessCheck
126public final class UnnecessarySemicolonAfterOuterTypeDeclarationCheck extends AbstractCheck {
127
128    /**
129     * A key is pointing to the warning message text in "messages.properties"
130     * file.
131     */
132    public static final String MSG_SEMI = "unnecessary.semicolon";
133
134    @Override
135    public int[] getDefaultTokens() {
136        return getAcceptableTokens();
137    }
138
139    @Override
140    public int[] getAcceptableTokens() {
141        return new int[] {
142            TokenTypes.CLASS_DEF,
143            TokenTypes.INTERFACE_DEF,
144            TokenTypes.ENUM_DEF,
145            TokenTypes.ANNOTATION_DEF,
146            TokenTypes.RECORD_DEF,
147        };
148    }
149
150    @Override
151    public int[] getRequiredTokens() {
152        return CommonUtil.EMPTY_INT_ARRAY;
153    }
154
155    @Override
156    public void visitToken(DetailAST ast) {
157        final DetailAST nextSibling = ast.getNextSibling();
158        if (nextSibling != null
159                && ScopeUtil.isOuterMostType(ast)
160                && nextSibling.getType() == TokenTypes.SEMI) {
161            log(nextSibling, MSG_SEMI);
162        }
163    }
164}