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.whitespace;
021
022import com.puppycrawl.tools.checkstyle.api.DetailAST;
023import com.puppycrawl.tools.checkstyle.api.TokenTypes;
024
025/**
026 * <p>
027 * Checks the policy on the padding of parentheses for typecasts. That is, whether a space
028 * is required after a left parenthesis and before a right parenthesis, or such
029 * spaces are forbidden.
030 * </p>
031 * <ul>
032 * <li>
033 * Property {@code option} - Specify policy on how to pad parentheses.
034 * Default value is {@code nospace}.
035 * </li>
036 * </ul>
037 * <p>
038 * To configure the check:
039 * </p>
040 * <pre>
041 * &lt;module name=&quot;TypecastParenPad&quot;/&gt;
042 * </pre>
043 * <p>
044 * To configure the check to require spaces:
045 * </p>
046 * <pre>
047 * &lt;module name=&quot;TypecastParenPad&quot;&gt;
048 *   &lt;property name=&quot;option&quot; value=&quot;space&quot;/&gt;
049 * &lt;/module&gt;
050 * </pre>
051 *
052 * @since 3.2
053 */
054public class TypecastParenPadCheck extends AbstractParenPadCheck {
055
056    @Override
057    public int[] getRequiredTokens() {
058        return new int[] {TokenTypes.RPAREN, TokenTypes.TYPECAST};
059    }
060
061    @Override
062    public int[] getDefaultTokens() {
063        return getRequiredTokens();
064    }
065
066    @Override
067    public int[] getAcceptableTokens() {
068        return getRequiredTokens();
069    }
070
071    @Override
072    public void visitToken(DetailAST ast) {
073        // Strange logic in this method to guard against checking RPAREN tokens
074        // that are not associated with a TYPECAST token.
075        if (ast.getType() == TokenTypes.TYPECAST) {
076            processLeft(ast);
077        }
078        else if (ast.getParent().getType() == TokenTypes.TYPECAST
079                 && ast.getParent().findFirstToken(TokenTypes.RPAREN) == ast) {
080            processRight(ast);
081        }
082    }
083
084}