001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2023 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 * Example:
056 * </p>
057 * <pre>
058 * for ( ; i &lt; 1; i++ );  // violation semicolon is preceded with whitespace
059 * for (; i &lt; 2; i++ );   // ok
060 * for (;i&lt;2;i++);        // ok
061 * for ( ;i&lt;2;i++);       // violation semicolon is preceded with whitespace
062 * for (
063 *       ; i &lt; 2; i++ );  // ok
064 * </pre>
065 * <p>
066 * To configure the check to require white space at an empty for iterator:
067 * </p>
068 * <pre>
069 * &lt;module name=&quot;EmptyForInitializerPad&quot;&gt;
070 *   &lt;property name=&quot;option&quot; value=&quot;space&quot;/&gt;
071 * &lt;/module&gt;
072 * </pre>
073 * <p>
074 * Example:
075 * </p>
076 * <pre>
077 * for ( ; i &lt; 2; i++ );   // ok
078 * for (; i &lt; 2; i++ );    // violation semicolon is not preceded with whitespace
079 * for (;i&lt;2;i++);         // violation semicolon is not preceded with whitespace
080 * for ( ;i&lt;2;i++);        // ok
081 * for (
082 *       ; i &lt; 2; i++ );   // ok
083 * </pre>
084 * <p>
085 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
086 * </p>
087 * <p>
088 * Violation Message Keys:
089 * </p>
090 * <ul>
091 * <li>
092 * {@code ws.notPreceded}
093 * </li>
094 * <li>
095 * {@code ws.preceded}
096 * </li>
097 * </ul>
098 *
099 * @since 3.4
100 */
101@StatelessCheck
102public class EmptyForInitializerPadCheck
103    extends AbstractCheck {
104
105    /**
106     * A key is pointing to the warning message text in "messages.properties"
107     * file.
108     */
109    public static final String MSG_PRECEDED = "ws.preceded";
110
111    /**
112     * A key is pointing to the warning message text in "messages.properties"
113     * file.
114     */
115    public static final String MSG_NOT_PRECEDED = "ws.notPreceded";
116
117    /** Semicolon literal. */
118    private static final String SEMICOLON = ";";
119
120    /** Specify policy on how to pad an empty for iterator. */
121    private PadOption option = PadOption.NOSPACE;
122
123    /**
124     * Setter to specify policy on how to pad an empty for iterator.
125     *
126     * @param optionStr string to decode option from
127     * @throws IllegalArgumentException if unable to decode
128     */
129    public void setOption(String optionStr) {
130        option = PadOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH));
131    }
132
133    @Override
134    public int[] getDefaultTokens() {
135        return getRequiredTokens();
136    }
137
138    @Override
139    public int[] getAcceptableTokens() {
140        return getRequiredTokens();
141    }
142
143    @Override
144    public int[] getRequiredTokens() {
145        return new int[] {TokenTypes.FOR_INIT};
146    }
147
148    @Override
149    public void visitToken(DetailAST ast) {
150        if (!ast.hasChildren()) {
151            final int lineIdx = ast.getLineNo() - 1;
152            final int[] line = getLineCodePoints(lineIdx);
153            final int before = ast.getColumnNo() - 1;
154            // don't check if semi at beginning of line
155            if (ast.getColumnNo() > 0 && !CodePointUtil.hasWhitespaceBefore(before, line)) {
156                if (option == PadOption.NOSPACE
157                    && CommonUtil.isCodePointWhitespace(line, before)) {
158                    log(ast, MSG_PRECEDED, SEMICOLON);
159                }
160                else if (option == PadOption.SPACE
161                         && !CommonUtil.isCodePointWhitespace(line, before)) {
162                    log(ast, MSG_NOT_PRECEDED, SEMICOLON);
163                }
164            }
165        }
166    }
167
168}