001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2026 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 * <div> 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 * </div> 036 * {@snippet lang="text" : 037 * for (Iterator foo = very.long.line.iterator(); 038 * foo.hasNext(); 039 * ) 040 * } 041 * 042 * @since 3.0 043 */ 044@StatelessCheck 045public class EmptyForIteratorPadCheck extends AbstractCheck { 046 047 /** 048 * A key is pointing to the warning message text in "messages.properties" file. 049 */ 050 public static final String MSG_WS_FOLLOWED = "ws.followed"; 051 052 /** 053 * A key is pointing to the warning message text in "messages.properties" file. 054 */ 055 public static final String MSG_WS_NOT_FOLLOWED = "ws.notFollowed"; 056 057 /** Semicolon literal. */ 058 private static final String SEMICOLON = ";"; 059 060 /** Specify policy on how to pad an empty for iterator. */ 061 private PadOption option = PadOption.NOSPACE; 062 063 /** 064 * Creates a new {@code EmptyForIteratorPadCheck} instance. 065 */ 066 public EmptyForIteratorPadCheck() { 067 // no code by default 068 } 069 070 /** 071 * Setter to specify policy on how to pad an empty for iterator. 072 * 073 * @param optionStr string to decode option from 074 * @throws IllegalArgumentException if unable to decode 075 * @since 3.0 076 */ 077 public void setOption(String optionStr) { 078 option = PadOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH)); 079 } 080 081 @Override 082 public int[] getDefaultTokens() { 083 return getRequiredTokens(); 084 } 085 086 @Override 087 public int[] getAcceptableTokens() { 088 return getRequiredTokens(); 089 } 090 091 @Override 092 public int[] getRequiredTokens() { 093 return new int[] {TokenTypes.FOR_ITERATOR}; 094 } 095 096 @Override 097 public void visitToken(DetailAST ast) { 098 if (!ast.hasChildren()) { 099 // empty for iterator. test pad after semi. 100 final DetailAST semi = ast.getPreviousSibling(); 101 final int[] line = getLineCodePoints(semi.getLineNo() - 1); 102 final int after = semi.getColumnNo() + 1; 103 // don't check if at end of line 104 if (after < line.length) { 105 if (option == PadOption.NOSPACE 106 && CommonUtil.isCodePointWhitespace(line, after)) { 107 log(ast, MSG_WS_FOLLOWED, SEMICOLON); 108 } 109 else if (option == PadOption.SPACE 110 && !CommonUtil.isCodePointWhitespace(line, after)) { 111 log(ast, MSG_WS_NOT_FOLLOWED, SEMICOLON); 112 } 113 } 114 } 115 } 116 117}