001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2022 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 * Type is {@code com.puppycrawl.tools.checkstyle.checks.whitespace.PadOption}.
035 * Default value is {@code nospace}.
036 * </li>
037 * </ul>
038 * <p>
039 * To configure the check:
040 * </p>
041 * <pre>
042 * &lt;module name=&quot;TypecastParenPad&quot;/&gt;
043 * </pre>
044 * <p>
045 * Example:
046 * </p>
047 * <pre>
048 * class Foo {
049 *
050 *   float f1 = 3.14f;
051 *
052 *   int n = ( int ) f1; // violation, space after left parenthesis and before right parenthesis
053 *
054 *   double d = 1.234567;
055 *
056 *   float f2 = (float ) d; // violation, space before right parenthesis
057 *
058 *   float f3 = (float) d; // OK
059 *
060 *   float f4 = ( float) d; // violation, space after left parenthesis
061 *
062 * }
063 * </pre>
064 * <p>
065 * To configure the check to require spaces:
066 * </p>
067 * <pre>
068 * &lt;module name=&quot;TypecastParenPad&quot;&gt;
069 *   &lt;property name=&quot;option&quot; value=&quot;space&quot;/&gt;
070 * &lt;/module&gt;
071 * </pre>
072 * <p>
073 * Example:
074 * </p>
075 * <pre>
076 * class Bar {
077 *
078 *   double d1 = 3.14;
079 *
080 *   int n = ( int ) d1; // OK
081 *
082 *   int m = (int ) d1; // violation, no space after left parenthesis
083 *
084 *   double d2 = 9.8;
085 *
086 *   int x = (int) d2; // violation, no space after left parenthesis and before right parenthesis
087 *
088 *   int y = ( int) d2; // violation, no space before right parenthesis
089 *
090 * }
091 * </pre>
092 * <p>
093 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
094 * </p>
095 * <p>
096 * Violation Message Keys:
097 * </p>
098 * <ul>
099 * <li>
100 * {@code ws.followed}
101 * </li>
102 * <li>
103 * {@code ws.notFollowed}
104 * </li>
105 * <li>
106 * {@code ws.notPreceded}
107 * </li>
108 * <li>
109 * {@code ws.preceded}
110 * </li>
111 * </ul>
112 *
113 * @since 3.2
114 */
115public class TypecastParenPadCheck extends AbstractParenPadCheck {
116
117    @Override
118    public int[] getRequiredTokens() {
119        return new int[] {TokenTypes.RPAREN, TokenTypes.TYPECAST};
120    }
121
122    @Override
123    public int[] getDefaultTokens() {
124        return getRequiredTokens();
125    }
126
127    @Override
128    public int[] getAcceptableTokens() {
129        return getRequiredTokens();
130    }
131
132    @Override
133    public void visitToken(DetailAST ast) {
134        // Strange logic in this method to guard against checking RPAREN tokens
135        // that are not associated with a TYPECAST token.
136        if (ast.getType() == TokenTypes.TYPECAST) {
137            processLeft(ast);
138        }
139        else if (ast.getParent().getType() == TokenTypes.TYPECAST
140                 && ast.getParent().findFirstToken(TokenTypes.RPAREN) == ast) {
141            processRight(ast);
142        }
143    }
144
145}