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