View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2025 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      @Override
21      public int[] getDefaultTokens() {
22          return new int[]{
23              TokenTypes.RESOURCE_SPECIFICATION,
24          };
25      }
26  
27      @Override
28      public int[] getAcceptableTokens() {
29          return this.getDefaultTokens();
30      }
31  
32      @Override
33      public int[] getRequiredTokens() {
34          return this.getDefaultTokens();
35      }
36  
37      @Override
38      public void visitToken(final DetailAST ast) {
39          final int semicolons = ast.getChildCount(TokenTypes.SEMI);
40          if (semicolons > 0) {
41              this.log(
42                  ast.getLineNo(),
43                  "Extra semicolon in the end of try-with-resources head."
44              );
45          }
46      }
47  }