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