001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2014  Oliver Burn
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////////////////////////////////////////////////////////////////////////////////
019package com.puppycrawl.tools.checkstyle.checks.whitespace;
020
021import com.puppycrawl.tools.checkstyle.api.DetailAST;
022import com.puppycrawl.tools.checkstyle.api.TokenTypes;
023
024/**
025 * <p>Checks the padding of parentheses for typecasts. That is whether a space
026 * is required after a left parenthesis and before a right parenthesis, or such
027 * spaces are forbidden.
028 * </p>
029 * <p>
030 * The policy to verify is specified using the {@link PadOption} class and
031 * defaults to {@link PadOption#NOSPACE}.
032 * </p>
033 * <p>
034 * An example of how to configure the check is:
035 * </p>
036 * <pre>
037 * &lt;module name="TypecastParenPad"/&gt;
038 * </pre>
039 * <p>
040 * An example of how to configure the check to require spaces for the
041 * parentheses of constructor, method, and super constructor invocations is:
042 * </p>
043 * <pre>
044 * &lt;module name="TypecastParenPad"&gt;
045 *     &lt;property name="option" value="space"/&gt;
046 * &lt;/module&gt;
047 * </pre>
048 * @author Oliver Burn
049 * @version 1.0
050 */
051public class TypecastParenPadCheck extends AbstractParenPadCheck
052{
053    @Override
054    public int[] getRequiredTokens()
055    {
056        return new int[] {TokenTypes.RPAREN, TokenTypes.TYPECAST};
057    }
058
059    @Override
060    public int[] getDefaultTokens()
061    {
062        return getRequiredTokens();
063    }
064
065    @Override
066    public void visitToken(DetailAST ast)
067    {
068        // Strange logic in this method to guard against checking RPAREN tokens
069        // that are not associated with a TYPECAST token.
070        if (ast.getType() == TokenTypes.TYPECAST) {
071            processLeft(ast);
072        }
073        else if ((ast.getParent() != null)
074                 && (ast.getParent().getType() == TokenTypes.TYPECAST)
075                 && (ast.getParent().findFirstToken(TokenTypes.RPAREN)
076                     == ast))
077        {
078            processRight(ast);
079        }
080    }
081}