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.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 * To configure the check: 050 * </p> 051 * <pre> 052 * <module name="EmptyForIteratorPad"/> 053 * </pre> 054 * <p> 055 * Example: 056 * </p> 057 * <pre> 058 * for (Iterator it = map.entrySet().iterator(); it.hasNext();); // ok 059 * for (Iterator it = map.entrySet().iterator(); it.hasNext(); ); // violation since whitespace 060 * //after semicolon 061 * 062 * for (Iterator foo = very.long.line.iterator(); 063 * foo.hasNext(); 064 * ); // ok 065 * </pre> 066 * <p> 067 * To configure the check to require white space at an empty for iterator: 068 * </p> 069 * <pre> 070 * <module name="EmptyForIteratorPad"> 071 * <property name="option" value="space"/> 072 * </module> 073 * </pre> 074 * <p> 075 * Example: 076 * </p> 077 * <pre> 078 * for (Iterator it = map.entrySet().iterator(); it.hasNext();); // violation as there is no 079 * // whitespace after semicolon 080 * 081 * for (Iterator it = map.entrySet().iterator(); it.hasNext(); ); // ok 082 * 083 * for (Iterator foo = very.long.line.iterator(); 084 * foo.hasNext(); 085 * ); // violation as there is no whitespace after semicolon 086 * </pre> 087 * <p> 088 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 089 * </p> 090 * <p> 091 * Violation Message Keys: 092 * </p> 093 * <ul> 094 * <li> 095 * {@code ws.followed} 096 * </li> 097 * <li> 098 * {@code ws.notFollowed} 099 * </li> 100 * </ul> 101 * 102 * @since 3.0 103 */ 104@StatelessCheck 105public class EmptyForIteratorPadCheck 106 extends AbstractCheck { 107 108 /** 109 * A key is pointing to the warning message text in "messages.properties" 110 * file. 111 */ 112 public static final String MSG_WS_FOLLOWED = "ws.followed"; 113 114 /** 115 * A key is pointing to the warning message text in "messages.properties" 116 * file. 117 */ 118 public static final String MSG_WS_NOT_FOLLOWED = "ws.notFollowed"; 119 120 /** Semicolon literal. */ 121 private static final String SEMICOLON = ";"; 122 123 /** Specify policy on how to pad an empty for iterator. */ 124 private PadOption option = PadOption.NOSPACE; 125 126 /** 127 * Setter to specify policy on how to pad an empty for iterator. 128 * 129 * @param optionStr string to decode option from 130 * @throws IllegalArgumentException if unable to decode 131 */ 132 public void setOption(String optionStr) { 133 option = PadOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH)); 134 } 135 136 @Override 137 public int[] getDefaultTokens() { 138 return getRequiredTokens(); 139 } 140 141 @Override 142 public int[] getAcceptableTokens() { 143 return getRequiredTokens(); 144 } 145 146 @Override 147 public int[] getRequiredTokens() { 148 return new int[] {TokenTypes.FOR_ITERATOR}; 149 } 150 151 @Override 152 public void visitToken(DetailAST ast) { 153 if (!ast.hasChildren()) { 154 // empty for iterator. test pad after semi. 155 final DetailAST semi = ast.getPreviousSibling(); 156 final int[] line = getLineCodePoints(semi.getLineNo() - 1); 157 final int after = semi.getColumnNo() + 1; 158 // don't check if at end of line 159 if (after < line.length) { 160 if (option == PadOption.NOSPACE 161 && CommonUtil.isCodePointWhitespace(line, after)) { 162 log(ast, MSG_WS_FOLLOWED, SEMICOLON); 163 } 164 else if (option == PadOption.SPACE 165 && !CommonUtil.isCodePointWhitespace(line, after)) { 166 log(ast, MSG_WS_NOT_FOLLOWED, SEMICOLON); 167 } 168 } 169 } 170 } 171 172}