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.DetailAST;
023import com.puppycrawl.tools.checkstyle.api.TokenTypes;
024import com.puppycrawl.tools.checkstyle.api.Utils;
025import com.puppycrawl.tools.checkstyle.checks.AbstractOptionCheck;
026
027/**
028 * <p>Checks the padding of an empty for initializer; that is whether a
029 * space is required at an empty for initializer, or such spaces are
030 * forbidden. No check occurs if there is a line wrap at the initializer, as in
031 * </p>
032 * <pre class="body">
033for (
034      ; i &lt; j; i++, j--)
035   </pre>
036 * <p>
037 * The policy to verify is specified using the {@link PadOption} class and
038 * defaults to {@link PadOption#NOSPACE}.
039 * </p>
040 * <p>
041 * An example of how to configure the check is:
042 * </p>
043 * <pre>
044 * &lt;module name="EmptyForInitializerPad"/&gt;
045 * </pre>
046 *
047 * @author lkuehne
048 * @version 1.0
049 */
050public class EmptyForInitializerPadCheck
051    extends AbstractOptionCheck<PadOption>
052{
053    /**
054     * Sets the paren pad otion to nospace.
055     */
056    public EmptyForInitializerPadCheck()
057    {
058        super(PadOption.NOSPACE, PadOption.class);
059    }
060
061    @Override
062    public int[] getDefaultTokens()
063    {
064        return new int[] {TokenTypes.FOR_INIT,
065        };
066    }
067
068    @Override
069    public void visitToken(DetailAST ast)
070    {
071        if (ast.getChildCount() == 0) {
072            //empty for initializer. test pad before semi.
073            final DetailAST semi = ast.getNextSibling();
074            final int semiLineIdx = semi.getLineNo() - 1;
075            final String line = getLines()[semiLineIdx];
076            final int before = semi.getColumnNo() - 1;
077            //don't check if semi at beginning of line
078            if (!Utils.whitespaceBefore(before, line)) {
079                final PadOption option = getAbstractOption();
080                if ((PadOption.NOSPACE == option)
081                    && (Character.isWhitespace(line.charAt(before))))
082                {
083                    log(semi.getLineNo(), before, "ws.preceded", ";");
084                }
085                else if ((PadOption.SPACE == option)
086                         && !Character.isWhitespace(line.charAt(before)))
087                {
088                    log(semi.getLineNo(), before, "ws.notPreceded", ";");
089                }
090            }
091        }
092    }
093}