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 * <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 * Type is {@code boolean}.
037 * Default value is {@code true}.
038 * </li>
039 * </ul>
040 * <p>
041 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
042 * </p>
043 * <p>
044 * Violation Message Keys:
045 * </p>
046 * <ul>
047 * <li>
048 * {@code unnecessary.semicolon}
049 * </li>
050 * </ul>
051 *
052 * @since 8.22
053 */
054@StatelessCheck
055public final class UnnecessarySemicolonInTryWithResourcesCheck extends AbstractCheck {
056
057    /**
058     * A key is pointing to the warning message text in "messages.properties"
059     * file.
060     */
061    public static final String MSG_SEMI = "unnecessary.semicolon";
062
063    /** Allow unnecessary semicolon if closing paren is not on the same line. */
064    private boolean allowWhenNoBraceAfterSemicolon = true;
065
066    @Override
067    public int[] getDefaultTokens() {
068        return getRequiredTokens();
069    }
070
071    @Override
072    public int[] getAcceptableTokens() {
073        return getRequiredTokens();
074    }
075
076    @Override
077    public int[] getRequiredTokens() {
078        return new int[] {
079            TokenTypes.RESOURCE_SPECIFICATION,
080        };
081    }
082
083    /**
084     * Setter to allow unnecessary semicolon if closing paren is not on the same line.
085     *
086     * @param allowWhenNoBraceAfterSemicolon a value to set.
087     * @since 8.22
088     */
089    public void setAllowWhenNoBraceAfterSemicolon(boolean allowWhenNoBraceAfterSemicolon) {
090        this.allowWhenNoBraceAfterSemicolon = allowWhenNoBraceAfterSemicolon;
091    }
092
093    @Override
094    public void visitToken(DetailAST ast) {
095        final DetailAST closingParen = ast.getLastChild();
096        final DetailAST tokenBeforeCloseParen = closingParen.getPreviousSibling();
097        if (tokenBeforeCloseParen.getType() == TokenTypes.SEMI
098            && (!allowWhenNoBraceAfterSemicolon
099                || TokenUtil.areOnSameLine(closingParen, tokenBeforeCloseParen))) {
100            log(tokenBeforeCloseParen, MSG_SEMI);
101        }
102    }
103
104}