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