View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.checks.coding;
21  
22  import com.puppycrawl.tools.checkstyle.StatelessCheck;
23  import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
24  import com.puppycrawl.tools.checkstyle.api.DetailAST;
25  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
26  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
27  import com.puppycrawl.tools.checkstyle.utils.ScopeUtil;
28  
29  /**
30   * <div>
31   * Checks if unnecessary semicolon is used after type declaration.
32   * </div>
33   *
34   * <p>
35   * Notes:
36   * This check is not applicable to nested type declarations,
37   * <a
38   * href="https://checkstyle.org/checks/coding/unnecessarysemicolonaftertypememberdeclaration.html">
39   * UnnecessarySemicolonAfterTypeMemberDeclaration</a> is responsible for it.
40   * </p>
41   *
42   * @since 8.31
43   */
44  @StatelessCheck
45  public final class UnnecessarySemicolonAfterOuterTypeDeclarationCheck extends AbstractCheck {
46  
47      /**
48       * A key is pointing to the warning message text in "messages.properties"
49       * file.
50       */
51      public static final String MSG_SEMI = "unnecessary.semicolon";
52  
53      @Override
54      public int[] getDefaultTokens() {
55          return getAcceptableTokens();
56      }
57  
58      @Override
59      public int[] getAcceptableTokens() {
60          return new int[] {
61              TokenTypes.CLASS_DEF,
62              TokenTypes.INTERFACE_DEF,
63              TokenTypes.ENUM_DEF,
64              TokenTypes.ANNOTATION_DEF,
65              TokenTypes.RECORD_DEF,
66          };
67      }
68  
69      @Override
70      public int[] getRequiredTokens() {
71          return CommonUtil.EMPTY_INT_ARRAY;
72      }
73  
74      @Override
75      public void visitToken(DetailAST ast) {
76          final DetailAST nextSibling = ast.getNextSibling();
77          if (nextSibling != null
78                  && ScopeUtil.isOuterMostType(ast)
79                  && nextSibling.getType() == TokenTypes.SEMI) {
80              log(nextSibling, MSG_SEMI);
81          }
82      }
83  }