View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2026 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.qulice.checkstyle;
6   
7   import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
8   import com.puppycrawl.tools.checkstyle.api.DetailAST;
9   import com.puppycrawl.tools.checkstyle.api.TokenTypes;
10  
11  /**
12   * Checks that try-with-resources does not end with a semicolon. Implementation
13   * relies on existence of semicolon inside of RESOURCE_SPECIFICATION token
14   * as interpreted by Checkstyle.
15   *
16   * @since 0.15
17   */
18  public final class FinalSemicolonInTryWithResourcesCheck extends AbstractCheck {
19  
20      /**
21       * Default constructor.
22       */
23      public FinalSemicolonInTryWithResourcesCheck() {
24          // nothing to initialize
25      }
26  
27      @Override
28      public int[] getDefaultTokens() {
29          return new int[]{
30              TokenTypes.RESOURCE_SPECIFICATION,
31          };
32      }
33  
34      @Override
35      public int[] getAcceptableTokens() {
36          return this.getDefaultTokens();
37      }
38  
39      @Override
40      public int[] getRequiredTokens() {
41          return this.getDefaultTokens();
42      }
43  
44      @Override
45      public void visitToken(final DetailAST ast) {
46          final int semicolons = ast.getChildCount(TokenTypes.SEMI);
47          if (semicolons > 0) {
48              this.log(
49                  ast.getLineNo(),
50                  "Extra semicolon in the end of try-with-resources head."
51              );
52          }
53      }
54  }