1 /////////////////////////////////////////////////////////////////////////////////////////////// 2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules. 3 // Copyright (C) 2001-2025 the original author or authors. 4 // 5 // This library is free software; you can redistribute it and/or 6 // modify it under the terms of the GNU Lesser General Public 7 // License as published by the Free Software Foundation; either 8 // version 2.1 of the License, or (at your option) any later version. 9 // 10 // This library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 // Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public 16 // License along with this library; if not, write to the Free Software 17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 /////////////////////////////////////////////////////////////////////////////////////////////// 19 20 package com.puppycrawl.tools.checkstyle.checks.whitespace; 21 22 import java.util.Locale; 23 24 import com.puppycrawl.tools.checkstyle.StatelessCheck; 25 import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 26 import com.puppycrawl.tools.checkstyle.api.DetailAST; 27 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 28 import com.puppycrawl.tools.checkstyle.utils.CommonUtil; 29 30 /** 31 * <div> 32 * Checks the padding of an empty for iterator; that is whether a white 33 * space is required at an empty for iterator, or such white space is 34 * forbidden. No check occurs if there is a line wrap at the iterator, as in 35 * </div> 36 * <div class="wrapper"><pre class="prettyprint"><code class="language-java"> 37 * for (Iterator foo = very.long.line.iterator(); 38 * foo.hasNext(); 39 * ) 40 * </code></pre></div> 41 * 42 * @since 3.0 43 */ 44 @StatelessCheck 45 public class EmptyForIteratorPadCheck 46 extends AbstractCheck { 47 48 /** 49 * A key is pointing to the warning message text in "messages.properties" 50 * file. 51 */ 52 public static final String MSG_WS_FOLLOWED = "ws.followed"; 53 54 /** 55 * A key is pointing to the warning message text in "messages.properties" 56 * file. 57 */ 58 public static final String MSG_WS_NOT_FOLLOWED = "ws.notFollowed"; 59 60 /** Semicolon literal. */ 61 private static final String SEMICOLON = ";"; 62 63 /** Specify policy on how to pad an empty for iterator. */ 64 private PadOption option = PadOption.NOSPACE; 65 66 /** 67 * Setter to specify policy on how to pad an empty for iterator. 68 * 69 * @param optionStr string to decode option from 70 * @throws IllegalArgumentException if unable to decode 71 * @since 3.0 72 */ 73 public void setOption(String optionStr) { 74 option = PadOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH)); 75 } 76 77 @Override 78 public int[] getDefaultTokens() { 79 return getRequiredTokens(); 80 } 81 82 @Override 83 public int[] getAcceptableTokens() { 84 return getRequiredTokens(); 85 } 86 87 @Override 88 public int[] getRequiredTokens() { 89 return new int[] {TokenTypes.FOR_ITERATOR}; 90 } 91 92 @Override 93 public void visitToken(DetailAST ast) { 94 if (!ast.hasChildren()) { 95 // empty for iterator. test pad after semi. 96 final DetailAST semi = ast.getPreviousSibling(); 97 final int[] line = getLineCodePoints(semi.getLineNo() - 1); 98 final int after = semi.getColumnNo() + 1; 99 // don't check if at end of line 100 if (after < line.length) { 101 if (option == PadOption.NOSPACE 102 && CommonUtil.isCodePointWhitespace(line, after)) { 103 log(ast, MSG_WS_FOLLOWED, SEMICOLON); 104 } 105 else if (option == PadOption.SPACE 106 && !CommonUtil.isCodePointWhitespace(line, after)) { 107 log(ast, MSG_WS_NOT_FOLLOWED, SEMICOLON); 108 } 109 } 110 } 111 } 112 113 }