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.TokenUtil;
27  
28  /**
29   * <div>
30   * Checks if unnecessary semicolon is used in last resource declaration.
31   * </div>
32   * <ul>
33   * <li>
34   * Property {@code allowWhenNoBraceAfterSemicolon} -
35   * Allow unnecessary semicolon if closing parenthesis is not on the same line.
36   * Type is {@code boolean}.
37   * Default value is {@code true}.
38   * </li>
39   * </ul>
40   *
41   * <p>
42   * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
43   * </p>
44   *
45   * <p>
46   * Violation Message Keys:
47   * </p>
48   * <ul>
49   * <li>
50   * {@code unnecessary.semicolon}
51   * </li>
52   * </ul>
53   *
54   * @since 8.22
55   */
56  @StatelessCheck
57  public final class UnnecessarySemicolonInTryWithResourcesCheck extends AbstractCheck {
58  
59      /**
60       * A key is pointing to the warning message text in "messages.properties"
61       * file.
62       */
63      public static final String MSG_SEMI = "unnecessary.semicolon";
64  
65      /** Allow unnecessary semicolon if closing parenthesis is not on the same line. */
66      private boolean allowWhenNoBraceAfterSemicolon = true;
67  
68      @Override
69      public int[] getDefaultTokens() {
70          return getRequiredTokens();
71      }
72  
73      @Override
74      public int[] getAcceptableTokens() {
75          return getRequiredTokens();
76      }
77  
78      @Override
79      public int[] getRequiredTokens() {
80          return new int[] {
81              TokenTypes.RESOURCE_SPECIFICATION,
82          };
83      }
84  
85      /**
86       * Setter to allow unnecessary semicolon if closing parenthesis is not on the same line.
87       *
88       * @param allowWhenNoBraceAfterSemicolon a value to set.
89       * @since 8.22
90       */
91      public void setAllowWhenNoBraceAfterSemicolon(boolean allowWhenNoBraceAfterSemicolon) {
92          this.allowWhenNoBraceAfterSemicolon = allowWhenNoBraceAfterSemicolon;
93      }
94  
95      @Override
96      public void visitToken(DetailAST ast) {
97          final DetailAST closingParen = ast.getLastChild();
98          final DetailAST tokenBeforeCloseParen = closingParen.getPreviousSibling();
99          if (tokenBeforeCloseParen.getType() == TokenTypes.SEMI
100             && (!allowWhenNoBraceAfterSemicolon
101                 || TokenUtil.areOnSameLine(closingParen, tokenBeforeCloseParen))) {
102             log(tokenBeforeCloseParen, MSG_SEMI);
103         }
104     }
105 
106 }