001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2024 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 iterator; that is whether a white
033 * space is required at an empty for iterator, or such white space is
034 * forbidden. No check occurs if there is a line wrap at the iterator, as in
035 * </p>
036 * <pre>
037 * for (Iterator foo = very.long.line.iterator();
038 *     foo.hasNext();
039 *    )
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 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
050 * </p>
051 * <p>
052 * Violation Message Keys:
053 * </p>
054 * <ul>
055 * <li>
056 * {@code ws.followed}
057 * </li>
058 * <li>
059 * {@code ws.notFollowed}
060 * </li>
061 * </ul>
062 *
063 * @since 3.0
064 */
065@StatelessCheck
066public class EmptyForIteratorPadCheck
067    extends AbstractCheck {
068
069    /**
070     * A key is pointing to the warning message text in "messages.properties"
071     * file.
072     */
073    public static final String MSG_WS_FOLLOWED = "ws.followed";
074
075    /**
076     * A key is pointing to the warning message text in "messages.properties"
077     * file.
078     */
079    public static final String MSG_WS_NOT_FOLLOWED = "ws.notFollowed";
080
081    /** Semicolon literal. */
082    private static final String SEMICOLON = ";";
083
084    /** Specify policy on how to pad an empty for iterator. */
085    private PadOption option = PadOption.NOSPACE;
086
087    /**
088     * Setter to specify policy on how to pad an empty for iterator.
089     *
090     * @param optionStr string to decode option from
091     * @throws IllegalArgumentException if unable to decode
092     * @since 3.0
093     */
094    public void setOption(String optionStr) {
095        option = PadOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH));
096    }
097
098    @Override
099    public int[] getDefaultTokens() {
100        return getRequiredTokens();
101    }
102
103    @Override
104    public int[] getAcceptableTokens() {
105        return getRequiredTokens();
106    }
107
108    @Override
109    public int[] getRequiredTokens() {
110        return new int[] {TokenTypes.FOR_ITERATOR};
111    }
112
113    @Override
114    public void visitToken(DetailAST ast) {
115        if (!ast.hasChildren()) {
116            // empty for iterator. test pad after semi.
117            final DetailAST semi = ast.getPreviousSibling();
118            final int[] line = getLineCodePoints(semi.getLineNo() - 1);
119            final int after = semi.getColumnNo() + 1;
120            // don't check if at end of line
121            if (after < line.length) {
122                if (option == PadOption.NOSPACE
123                    && CommonUtil.isCodePointWhitespace(line, after)) {
124                    log(ast, MSG_WS_FOLLOWED, SEMICOLON);
125                }
126                else if (option == PadOption.SPACE
127                         && !CommonUtil.isCodePointWhitespace(line, after)) {
128                    log(ast, MSG_WS_NOT_FOLLOWED, SEMICOLON);
129                }
130            }
131        }
132    }
133
134}