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