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   * <ul>
42   * <li>
43   * Property {@code tokens} - tokens to check
44   * Type is {@code java.lang.String[]}.
45   * Validation type is {@code tokenSet}.
46   * Default value is:
47   * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CLASS_DEF">
48   * CLASS_DEF</a>,
49   * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#INTERFACE_DEF">
50   * INTERFACE_DEF</a>,
51   * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ENUM_DEF">
52   * ENUM_DEF</a>,
53   * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ANNOTATION_DEF">
54   * ANNOTATION_DEF</a>,
55   * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#RECORD_DEF">
56   * RECORD_DEF</a>.
57   * </li>
58   * </ul>
59   *
60   * <p>
61   * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
62   * </p>
63   *
64   * <p>
65   * Violation Message Keys:
66   * </p>
67   * <ul>
68   * <li>
69   * {@code unnecessary.semicolon}
70   * </li>
71   * </ul>
72   *
73   * @since 8.31
74   */
75  @StatelessCheck
76  public final class UnnecessarySemicolonAfterOuterTypeDeclarationCheck extends AbstractCheck {
77  
78      /**
79       * A key is pointing to the warning message text in "messages.properties"
80       * file.
81       */
82      public static final String MSG_SEMI = "unnecessary.semicolon";
83  
84      @Override
85      public int[] getDefaultTokens() {
86          return getAcceptableTokens();
87      }
88  
89      @Override
90      public int[] getAcceptableTokens() {
91          return new int[] {
92              TokenTypes.CLASS_DEF,
93              TokenTypes.INTERFACE_DEF,
94              TokenTypes.ENUM_DEF,
95              TokenTypes.ANNOTATION_DEF,
96              TokenTypes.RECORD_DEF,
97          };
98      }
99  
100     @Override
101     public int[] getRequiredTokens() {
102         return CommonUtil.EMPTY_INT_ARRAY;
103     }
104 
105     @Override
106     public void visitToken(DetailAST ast) {
107         final DetailAST nextSibling = ast.getNextSibling();
108         if (nextSibling != null
109                 && ScopeUtil.isOuterMostType(ast)
110                 && nextSibling.getType() == TokenTypes.SEMI) {
111             log(nextSibling, MSG_SEMI);
112         }
113     }
114 }