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 java.util.Locale;
023
024import org.apache.commons.beanutils.ConversionException;
025
026import com.puppycrawl.tools.checkstyle.api.Check;
027import com.puppycrawl.tools.checkstyle.api.DetailAST;
028import com.puppycrawl.tools.checkstyle.api.TokenTypes;
029import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
030
031/**
032 * <p>Checks the padding of an empty for initializer; that is whether a
033 * space is required at an empty for initializer, or such spaces are
034 * forbidden. No check occurs if there is a line wrap at the initializer, as in
035 * </p>
036 * <pre class="body">
037for (
038      ; i &lt; j; i++, j--)
039   </pre>
040 * <p>
041 * The policy to verify is specified using the {@link PadOption} class and
042 * defaults to {@link PadOption#NOSPACE}.
043 * </p>
044 * <p>
045 * An example of how to configure the check is:
046 * </p>
047 * <pre>
048 * &lt;module name="EmptyForInitializerPad"/&gt;
049 * </pre>
050 *
051 * @author lkuehne
052 */
053public class EmptyForInitializerPadCheck
054    extends Check {
055
056    /**
057     * A key is pointing to the warning message text in "messages.properties"
058     * file.
059     */
060    public static final String MSG_PRECEDED = "ws.preceded";
061
062    /**
063     * A key is pointing to the warning message text in "messages.properties"
064     * file.
065     */
066    public static final String MSG_NOT_PRECEDED = "ws.notPreceded";
067
068    /** Semicolon literal. */
069    private static final String SEMICOLON = ";";
070
071    /** The policy to enforce. */
072    private PadOption option = PadOption.NOSPACE;
073
074    /**
075     * Set the option to enforce.
076     * @param optionStr string to decode option from
077     * @throws ConversionException if unable to decode
078     */
079    public void setOption(String optionStr) {
080        try {
081            option = PadOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH));
082        }
083        catch (IllegalArgumentException iae) {
084            throw new ConversionException("unable to parse " + optionStr, iae);
085        }
086    }
087
088    @Override
089    public int[] getDefaultTokens() {
090        return getAcceptableTokens();
091    }
092
093    @Override
094    public int[] getAcceptableTokens() {
095        return new int[] {TokenTypes.FOR_INIT};
096    }
097
098    @Override
099    public int[] getRequiredTokens() {
100        return getAcceptableTokens();
101    }
102
103    @Override
104    public void visitToken(DetailAST ast) {
105        if (ast.getChildCount() == 0) {
106            //empty for initializer. test pad before semi.
107            final DetailAST semi = ast.getNextSibling();
108            final int semiLineIdx = semi.getLineNo() - 1;
109            final String line = getLines()[semiLineIdx];
110            final int before = semi.getColumnNo() - 1;
111            //don't check if semi at beginning of line
112            if (!CommonUtils.hasWhitespaceBefore(before, line)) {
113                if (option == PadOption.NOSPACE
114                    && Character.isWhitespace(line.charAt(before))) {
115                    log(semi.getLineNo(), before, MSG_PRECEDED, SEMICOLON);
116                }
117                else if (option == PadOption.SPACE
118                         && !Character.isWhitespace(line.charAt(before))) {
119                    log(semi.getLineNo(), before, MSG_NOT_PRECEDED, SEMICOLON);
120                }
121            }
122        }
123    }
124}