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.Check;
025import com.puppycrawl.tools.checkstyle.api.DetailAST;
026import com.puppycrawl.tools.checkstyle.api.TokenTypes;
027
028/**
029 * <p>
030 * Checks that there is no whitespace before a token.
031 * More specifically, it checks that it is not preceded with whitespace,
032 * or (if line breaks are allowed) all characters on the line before are
033 * whitespace. To allow line breaks before a token, set property
034 * allowLineBreaks to true.
035 * </p>
036 * <p> By default the check will check the following operators:
037 *  {@link TokenTypes#SEMI SEMI},
038 *  {@link TokenTypes#POST_DEC POST_DEC},
039 *  {@link TokenTypes#POST_INC POST_INC}.
040 * {@link TokenTypes#DOT DOT} is also an acceptable token in a configuration
041 * of this check.
042 * </p>
043 *
044 * <p>
045 * An example of how to configure the check is:
046 * </p>
047 * <pre>
048 * &lt;module name="NoWhitespaceBefore"/&gt;
049 * </pre>
050 * <p> An example of how to configure the check to allow line breaks before
051 * a {@link TokenTypes#DOT DOT} token is:
052 * </p>
053 * <pre>
054 * &lt;module name="NoWhitespaceBefore"&gt;
055 *     &lt;property name="tokens" value="DOT"/&gt;
056 *     &lt;property name="allowLineBreaks" value="true"/&gt;
057 * &lt;/module&gt;
058 * </pre>
059 * @author Rick Giles
060 * @author lkuehne
061 */
062public class NoWhitespaceBeforeCheck
063    extends Check {
064
065    /**
066     * A key is pointing to the warning message text in "messages.properties"
067     * file.
068     */
069    public static final String MSG_KEY = "ws.preceded";
070
071    /** Whether whitespace is allowed if the AST is at a linebreak. */
072    private boolean allowLineBreaks;
073
074    @Override
075    public int[] getDefaultTokens() {
076        return new int[] {
077            TokenTypes.COMMA,
078            TokenTypes.SEMI,
079            TokenTypes.POST_INC,
080            TokenTypes.POST_DEC,
081        };
082    }
083
084    @Override
085    public int[] getAcceptableTokens() {
086        return new int[] {
087            TokenTypes.COMMA,
088            TokenTypes.SEMI,
089            TokenTypes.POST_INC,
090            TokenTypes.POST_DEC,
091            TokenTypes.DOT,
092            TokenTypes.GENERIC_START,
093            TokenTypes.GENERIC_END,
094        };
095    }
096
097    @Override
098    public int[] getRequiredTokens() {
099        return ArrayUtils.EMPTY_INT_ARRAY;
100    }
101
102    @Override
103    public void visitToken(DetailAST ast) {
104        final String line = getLine(ast.getLineNo() - 1);
105        final int before = ast.getColumnNo() - 1;
106
107        if ((before < 0 || Character.isWhitespace(line.charAt(before)))
108                && !isInEmptyForInitializer(ast)) {
109
110            boolean flag = !allowLineBreaks;
111            // verify all characters before '.' are whitespace
112            for (int i = 0; !flag && i < before; i++) {
113                if (!Character.isWhitespace(line.charAt(i))) {
114                    flag = true;
115                }
116            }
117            if (flag) {
118                log(ast.getLineNo(), before, MSG_KEY, ast.getText());
119            }
120        }
121    }
122
123    /**
124     * Checks that semicolon is in empty for initializer.
125     * @param semicolonAst DetailAST of semicolon.
126     * @return true if semicolon is in empty for initializer.
127     */
128    private static boolean isInEmptyForInitializer(DetailAST semicolonAst) {
129        boolean result = false;
130        if (semicolonAst.getType() == TokenTypes.SEMI) {
131            final DetailAST sibling = semicolonAst.getPreviousSibling();
132            if (sibling != null
133                    && sibling.getType() == TokenTypes.FOR_INIT
134                    && sibling.getChildCount() == 0) {
135                result = true;
136            }
137        }
138        return result;
139    }
140
141    /**
142     * Control whether whitespace is flagged at line breaks.
143     * @param allowLineBreaks whether whitespace should be
144     *     flagged at line breaks.
145     */
146    public void setAllowLineBreaks(boolean allowLineBreaks) {
147        this.allowLineBreaks = allowLineBreaks;
148    }
149}