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