001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2024 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018/////////////////////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle.checks.coding; 021 022import com.puppycrawl.tools.checkstyle.StatelessCheck; 023import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 024import com.puppycrawl.tools.checkstyle.api.DetailAST; 025import com.puppycrawl.tools.checkstyle.api.TokenTypes; 026import com.puppycrawl.tools.checkstyle.utils.TokenUtil; 027 028/** 029 * <div> 030 * Checks if unnecessary semicolon is used in last resource declaration. 031 * </div> 032 * <ul> 033 * <li> 034 * Property {@code allowWhenNoBraceAfterSemicolon} - 035 * Allow unnecessary semicolon if closing parenthesis is not on the same line. 036 * Type is {@code boolean}. 037 * Default value is {@code true}. 038 * </li> 039 * </ul> 040 * 041 * <p> 042 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 043 * </p> 044 * 045 * <p> 046 * Violation Message Keys: 047 * </p> 048 * <ul> 049 * <li> 050 * {@code unnecessary.semicolon} 051 * </li> 052 * </ul> 053 * 054 * @since 8.22 055 */ 056@StatelessCheck 057public final class UnnecessarySemicolonInTryWithResourcesCheck extends AbstractCheck { 058 059 /** 060 * A key is pointing to the warning message text in "messages.properties" 061 * file. 062 */ 063 public static final String MSG_SEMI = "unnecessary.semicolon"; 064 065 /** Allow unnecessary semicolon if closing parenthesis is not on the same line. */ 066 private boolean allowWhenNoBraceAfterSemicolon = true; 067 068 @Override 069 public int[] getDefaultTokens() { 070 return getRequiredTokens(); 071 } 072 073 @Override 074 public int[] getAcceptableTokens() { 075 return getRequiredTokens(); 076 } 077 078 @Override 079 public int[] getRequiredTokens() { 080 return new int[] { 081 TokenTypes.RESOURCE_SPECIFICATION, 082 }; 083 } 084 085 /** 086 * Setter to allow unnecessary semicolon if closing parenthesis is not on the same line. 087 * 088 * @param allowWhenNoBraceAfterSemicolon a value to set. 089 * @since 8.22 090 */ 091 public void setAllowWhenNoBraceAfterSemicolon(boolean allowWhenNoBraceAfterSemicolon) { 092 this.allowWhenNoBraceAfterSemicolon = allowWhenNoBraceAfterSemicolon; 093 } 094 095 @Override 096 public void visitToken(DetailAST ast) { 097 final DetailAST closingParen = ast.getLastChild(); 098 final DetailAST tokenBeforeCloseParen = closingParen.getPreviousSibling(); 099 if (tokenBeforeCloseParen.getType() == TokenTypes.SEMI 100 && (!allowWhenNoBraceAfterSemicolon 101 || TokenUtil.areOnSameLine(closingParen, tokenBeforeCloseParen))) { 102 log(tokenBeforeCloseParen, MSG_SEMI); 103 } 104 } 105 106}