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   * @since 0.15
16   */
17  public final class FinalSemicolonInTryWithResourcesCheck extends AbstractCheck {
18  
19      @Override
20      public int[] getDefaultTokens() {
21          return new int[]{
22              TokenTypes.RESOURCE_SPECIFICATION,
23          };
24      }
25  
26      @Override
27      public int[] getAcceptableTokens() {
28          return this.getDefaultTokens();
29      }
30  
31      @Override
32      public int[] getRequiredTokens() {
33          return this.getDefaultTokens();
34      }
35  
36      @Override
37      public void visitToken(final DetailAST ast) {
38          final int semicolons = ast.getChildCount(TokenTypes.SEMI);
39          if (semicolons > 0) {
40              this.log(
41                  ast.getLineNo(),
42                  "Extra semicolon in the end of try-with-resources head."
43              );
44          }
45      }
46  }