001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2014  Oliver Burn
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.Check;
023import com.puppycrawl.tools.checkstyle.api.DetailAST;
024import com.puppycrawl.tools.checkstyle.api.TokenTypes;
025
026/**
027 * <p>
028 * Checks that there is no whitespace before a token.
029 * More specifically, it checks that it is not preceded with whitespace,
030 * or (if linebreaks are allowed) all characters on the line before are
031 * whitespace. To allow linebreaks before a token, set property
032 * allowLineBreaks to true.
033 * </p>
034 * <p> By default the check will check the following operators:
035 *  {@link TokenTypes#SEMI SEMI},
036 *  {@link TokenTypes#POST_DEC POST_DEC},
037 *  {@link TokenTypes#POST_INC POST_INC}.
038 * {@link TokenTypes#DOT DOT} is also an acceptable token in a configuration
039 * of this check.
040 * </p>
041 *
042 * <p>
043 * An example of how to configure the check is:
044 * </p>
045 * <pre>
046 * &lt;module name="NoWhitespaceBefore"/&gt;
047 * </pre>
048 * <p> An example of how to configure the check to allow linebreaks before
049 * a {@link TokenTypes#DOT DOT} token is:
050 * </p>
051 * <pre>
052 * &lt;module name="NoWhitespaceBefore"&gt;
053 *     &lt;property name="tokens" value="DOT"/&gt;
054 *     &lt;property name="allowLineBreaks" value="true"/&gt;
055 * &lt;/module&gt;
056 * </pre>
057 * @author Rick Giles
058 * @author lkuehne
059 * @version 1.0
060 */
061public class NoWhitespaceBeforeCheck
062    extends Check
063{
064    /** Whether whitespace is allowed if the AST is at a linebreak */
065    private boolean mAllowLineBreaks;
066
067    @Override
068    public int[] getDefaultTokens()
069    {
070        return new int[] {
071            TokenTypes.SEMI,
072            TokenTypes.POST_INC,
073            TokenTypes.POST_DEC,
074        };
075    }
076
077    @Override
078    public int[] getAcceptableTokens()
079    {
080        return new int[] {
081            TokenTypes.SEMI,
082            TokenTypes.POST_INC,
083            TokenTypes.POST_DEC,
084            TokenTypes.DOT,
085        };
086    }
087
088    @Override
089    public void visitToken(DetailAST aAST)
090    {
091        final String[] lines = getLines();
092        final String line = lines[aAST.getLineNo() - 1];
093        final int before = aAST.getColumnNo() - 1;
094
095        if ((before < 0) || Character.isWhitespace(line.charAt(before))) {
096
097            // empty FOR initializer?
098            if (aAST.getType() == TokenTypes.SEMI) {
099                final DetailAST sibling = aAST.getPreviousSibling();
100                if ((sibling != null)
101                        && (sibling.getType() == TokenTypes.FOR_INIT)
102                        && (sibling.getChildCount() == 0))
103                {
104                    return;
105                }
106            }
107
108            boolean flag = !mAllowLineBreaks;
109            // verify all characters before '.' are whitespace
110            for (int i = 0; !flag && (i < before); i++) {
111                if (!Character.isWhitespace(line.charAt(i))) {
112                    flag = true;
113                }
114            }
115            if (flag) {
116                log(aAST.getLineNo(), before, "ws.preceded", aAST.getText());
117            }
118        }
119    }
120
121    /**
122     * Control whether whitespace is flagged at linebreaks.
123     * @param aAllowLineBreaks whether whitespace should be
124     * flagged at linebreaks.
125     */
126    public void setAllowLineBreaks(boolean aAllowLineBreaks)
127    {
128        mAllowLineBreaks = aAllowLineBreaks;
129    }
130}