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