View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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   * This check is not applicable to nested type declarations,
36   * <a
37   * href="https://checkstyle.org/checks/coding/unnecessarysemicolonaftertypememberdeclaration.html">
38   * UnnecessarySemicolonAfterTypeMemberDeclaration</a> is responsible for it.
39   * </p>
40   * <ul>
41   * <li>
42   * Property {@code tokens} - tokens to check
43   * Type is {@code java.lang.String[]}.
44   * Validation type is {@code tokenSet}.
45   * Default value is:
46   * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CLASS_DEF">
47   * CLASS_DEF</a>,
48   * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#INTERFACE_DEF">
49   * INTERFACE_DEF</a>,
50   * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ENUM_DEF">
51   * ENUM_DEF</a>,
52   * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ANNOTATION_DEF">
53   * ANNOTATION_DEF</a>,
54   * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#RECORD_DEF">
55   * RECORD_DEF</a>.
56   * </li>
57   * </ul>
58   *
59   * <p>
60   * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
61   * </p>
62   *
63   * <p>
64   * Violation Message Keys:
65   * </p>
66   * <ul>
67   * <li>
68   * {@code unnecessary.semicolon}
69   * </li>
70   * </ul>
71   *
72   * @since 8.31
73   */
74  @StatelessCheck
75  public final class UnnecessarySemicolonAfterOuterTypeDeclarationCheck extends AbstractCheck {
76  
77      /**
78       * A key is pointing to the warning message text in "messages.properties"
79       * file.
80       */
81      public static final String MSG_SEMI = "unnecessary.semicolon";
82  
83      @Override
84      public int[] getDefaultTokens() {
85          return getAcceptableTokens();
86      }
87  
88      @Override
89      public int[] getAcceptableTokens() {
90          return new int[] {
91              TokenTypes.CLASS_DEF,
92              TokenTypes.INTERFACE_DEF,
93              TokenTypes.ENUM_DEF,
94              TokenTypes.ANNOTATION_DEF,
95              TokenTypes.RECORD_DEF,
96          };
97      }
98  
99      @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 }