001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2020 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 * <p> 030 * Checks if unnecessary semicolon is used in last resource declaration. 031 * </p> 032 * <ul> 033 * <li> 034 * Property {@code allowWhenNoBraceAfterSemicolon} - 035 * Allow unnecessary semicolon if closing paren is not on the same line. 036 * Default value is {@code true}. 037 * </li> 038 * </ul> 039 * <p> 040 * To configure the check: 041 * </p> 042 * <pre> 043 * <module name="UnnecessarySemicolonInTryWithResources"/> 044 * </pre> 045 * <p> 046 * Example of violations 047 * </p> 048 * <pre> 049 * class A { 050 * void method() throws IOException { 051 * try(Reader r1 = new PipedReader();){} // violation 052 * try(Reader r4 = new PipedReader();Reader r5 = new PipedReader() 053 * ;){} // violation 054 * try(Reader r6 = new PipedReader(); 055 * Reader r7 056 * = new PipedReader(); 057 * ){} 058 * } 059 * } 060 * </pre> 061 * <p> 062 * To configure the check to detect unnecessary semicolon 063 * if closing paren is not on same line 064 * </p> 065 * <pre> 066 * <module name="UnnecessarySemicolonInTryWithResources"> 067 * <property name="allowWhenNoBraceAfterSemicolon" value="false"/> 068 * </module> 069 * </pre> 070 * <p> 071 * Example of exclusion 072 * </p> 073 * <pre> 074 * class A { 075 * void method() throws IOException { 076 * try(Reader r1 = new PipedReader();){} // violation 077 * try(Reader r6 = new PipedReader(); 078 * Reader r7 = new PipedReader(); // violation 079 * ){} 080 * } 081 * } 082 * </pre> 083 * 084 * @since 8.22 085 */ 086@StatelessCheck 087public final class UnnecessarySemicolonInTryWithResourcesCheck extends AbstractCheck { 088 089 /** 090 * A key is pointing to the warning message text in "messages.properties" 091 * file. 092 */ 093 public static final String MSG_SEMI = "unnecessary.semicolon"; 094 095 /** Allow unnecessary semicolon if closing paren is not on the same line. */ 096 private boolean allowWhenNoBraceAfterSemicolon = true; 097 098 @Override 099 public int[] getDefaultTokens() { 100 return getRequiredTokens(); 101 } 102 103 @Override 104 public int[] getAcceptableTokens() { 105 return getRequiredTokens(); 106 } 107 108 @Override 109 public int[] getRequiredTokens() { 110 return new int[] { 111 TokenTypes.RESOURCE_SPECIFICATION, 112 }; 113 } 114 115 /** 116 * Setter to allow unnecessary semicolon if closing paren is not on the same line. 117 * 118 * @param allowWhenNoBraceAfterSemicolon a value to set. 119 */ 120 public void setAllowWhenNoBraceAfterSemicolon(boolean allowWhenNoBraceAfterSemicolon) { 121 this.allowWhenNoBraceAfterSemicolon = allowWhenNoBraceAfterSemicolon; 122 } 123 124 @Override 125 public void visitToken(DetailAST ast) { 126 final DetailAST closingParen = ast.getLastChild(); 127 final DetailAST tokenBeforeCloseParen = closingParen.getPreviousSibling(); 128 if (tokenBeforeCloseParen.getType() == TokenTypes.SEMI 129 && (!allowWhenNoBraceAfterSemicolon 130 || TokenUtil.areOnSameLine(closingParen, tokenBeforeCloseParen))) { 131 log(tokenBeforeCloseParen, MSG_SEMI); 132 } 133 } 134 135}