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 java.util.Locale;
023
024import com.puppycrawl.tools.checkstyle.StatelessCheck;
025import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
026import com.puppycrawl.tools.checkstyle.api.DetailAST;
027import com.puppycrawl.tools.checkstyle.api.TokenTypes;
028import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
029
030/**
031 * <p>
032 * Checks the padding between the identifier of a method definition,
033 * constructor definition, method call, or constructor invocation;
034 * and the left parenthesis of the parameter list.
035 * That is, if the identifier and left parenthesis are on the same line,
036 * checks whether a space is required immediately after the identifier or
037 * such a space is forbidden.
038 * If they are not on the same line, reports an error, unless configured to
039 * allow line breaks.
040 * </p>
041 * <p> By default the check will check the following tokens:
042 *  {@link TokenTypes#CTOR_DEF CTOR_DEF},
043 *  {@link TokenTypes#LITERAL_NEW LITERAL_NEW},
044 *  {@link TokenTypes#METHOD_CALL METHOD_CALL},
045 *  {@link TokenTypes#METHOD_DEF METHOD_DEF},
046 *  {@link TokenTypes#SUPER_CTOR_CALL SUPER_CTOR_CALL}.
047 * </p>
048 * <p>
049 * An example of how to configure the check is:
050 * </p>
051 * <pre>
052 * &lt;module name="MethodParamPad"/&gt;
053 * </pre>
054 * <p> An example of how to configure the check to require a space
055 * after the identifier of a method definition, except if the left
056 * parenthesis occurs on a new line, is:
057 * </p>
058 * <pre>
059 * &lt;module name="MethodParamPad"&gt;
060 *     &lt;property name="tokens" value="METHOD_DEF"/&gt;
061 *     &lt;property name="option" value="space"/&gt;
062 *     &lt;property name="allowLineBreaks" value="true"/&gt;
063 * &lt;/module&gt;
064 * </pre>
065 * @author Rick Giles
066 */
067
068@StatelessCheck
069public class MethodParamPadCheck
070    extends AbstractCheck {
071
072    /**
073     * A key is pointing to the warning message text in "messages.properties"
074     * file.
075     */
076    public static final String MSG_LINE_PREVIOUS = "line.previous";
077
078    /**
079     * A key is pointing to the warning message text in "messages.properties"
080     * file.
081     */
082    public static final String MSG_WS_PRECEDED = "ws.preceded";
083
084    /**
085     * A key is pointing to the warning message text in "messages.properties"
086     * file.
087     */
088    public static final String MSG_WS_NOT_PRECEDED = "ws.notPreceded";
089
090    /**
091     * Whether whitespace is allowed if the method identifier is at a
092     * linebreak.
093     */
094    private boolean allowLineBreaks;
095
096    /** The policy to enforce. */
097    private PadOption option = PadOption.NOSPACE;
098
099    @Override
100    public int[] getDefaultTokens() {
101        return getAcceptableTokens();
102    }
103
104    @Override
105    public int[] getAcceptableTokens() {
106        return new int[] {
107            TokenTypes.CTOR_DEF,
108            TokenTypes.LITERAL_NEW,
109            TokenTypes.METHOD_CALL,
110            TokenTypes.METHOD_DEF,
111            TokenTypes.SUPER_CTOR_CALL,
112            TokenTypes.ENUM_CONSTANT_DEF,
113        };
114    }
115
116    @Override
117    public int[] getRequiredTokens() {
118        return CommonUtils.EMPTY_INT_ARRAY;
119    }
120
121    @Override
122    public void visitToken(DetailAST ast) {
123        final DetailAST parenAST;
124        if (ast.getType() == TokenTypes.METHOD_CALL) {
125            parenAST = ast;
126        }
127        else {
128            parenAST = ast.findFirstToken(TokenTypes.LPAREN);
129            // array construction => parenAST == null
130        }
131
132        if (parenAST != null) {
133            final String line = getLines()[parenAST.getLineNo() - 1];
134            if (CommonUtils.hasWhitespaceBefore(parenAST.getColumnNo(), line)) {
135                if (!allowLineBreaks) {
136                    log(parenAST, MSG_LINE_PREVIOUS, parenAST.getText());
137                }
138            }
139            else {
140                final int before = parenAST.getColumnNo() - 1;
141                if (option == PadOption.NOSPACE
142                    && Character.isWhitespace(line.charAt(before))) {
143                    log(parenAST, MSG_WS_PRECEDED, parenAST.getText());
144                }
145                else if (option == PadOption.SPACE
146                         && !Character.isWhitespace(line.charAt(before))) {
147                    log(parenAST, MSG_WS_NOT_PRECEDED, parenAST.getText());
148                }
149            }
150        }
151    }
152
153    /**
154     * Control whether whitespace is flagged at line breaks.
155     * @param allowLineBreaks whether whitespace should be
156     *     flagged at line breaks.
157     */
158    public void setAllowLineBreaks(boolean allowLineBreaks) {
159        this.allowLineBreaks = allowLineBreaks;
160    }
161
162    /**
163     * Set the option to enforce.
164     * @param optionStr string to decode option from
165     * @throws IllegalArgumentException if unable to decode
166     */
167    public void setOption(String optionStr) {
168        try {
169            option = PadOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH));
170        }
171        catch (IllegalArgumentException iae) {
172            throw new IllegalArgumentException("unable to parse " + optionStr, iae);
173        }
174    }
175
176}