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