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