001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2018 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 053 @Override 054 public int[] getRequiredTokens() { 055 return new int[] {TokenTypes.RPAREN, TokenTypes.TYPECAST}; 056 } 057 058 @Override 059 public int[] getDefaultTokens() { 060 return getRequiredTokens(); 061 } 062 063 @Override 064 public int[] getAcceptableTokens() { 065 return getRequiredTokens(); 066 } 067 068 @Override 069 public void visitToken(DetailAST ast) { 070 // Strange logic in this method to guard against checking RPAREN tokens 071 // that are not associated with a TYPECAST token. 072 if (ast.getType() == TokenTypes.TYPECAST) { 073 processLeft(ast); 074 } 075 else if (ast.getParent().getType() == TokenTypes.TYPECAST 076 && ast.getParent().findFirstToken(TokenTypes.RPAREN) == ast) { 077 processRight(ast); 078 } 079 } 080 081}