001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2015 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 com.puppycrawl.tools.checkstyle.api.DetailAST; 023import com.puppycrawl.tools.checkstyle.api.TokenTypes; 024import com.puppycrawl.tools.checkstyle.checks.AbstractOptionCheck; 025 026/** 027 * <p>Checks the padding of an empty for iterator; that is whether a 028 * space is required at an empty for iterator, or such spaces are 029 * forbidden. No check occurs if there is a line wrap at the iterator, as in 030 * </p> 031 * <pre class="body"> 032for (Iterator foo = very.long.line.iterator(); 033 foo.hasNext(); 034 ) 035 </pre> 036 * <p> 037 * The policy to verify is specified using the {@link PadOption} class and 038 * defaults to {@link PadOption#NOSPACE}. 039 * </p> 040 * <p> 041 * An example of how to configure the check is: 042 * </p> 043 * <pre> 044 * <module name="EmptyForIteratorPad"/> 045 * </pre> 046 * 047 * @author Rick Giles 048 */ 049public class EmptyForIteratorPadCheck 050 extends AbstractOptionCheck<PadOption> { 051 052 /** 053 * A key is pointing to the warning message text in "messages.properties" 054 * file. 055 */ 056 public static final String WS_FOLLOWED = "ws.followed"; 057 058 /** 059 * A key is pointing to the warning message text in "messages.properties" 060 * file. 061 */ 062 public static final String WS_NOT_FOLLOWED = "ws.notFollowed"; 063 064 /** 065 * Sets the paren pad option to nospace. 066 */ 067 public EmptyForIteratorPadCheck() { 068 super(PadOption.NOSPACE, PadOption.class); 069 } 070 071 @Override 072 public int[] getDefaultTokens() { 073 return getAcceptableTokens(); 074 } 075 076 @Override 077 public int[] getAcceptableTokens() { 078 return new int[] {TokenTypes.FOR_ITERATOR}; 079 } 080 081 @Override 082 public int[] getRequiredTokens() { 083 return getAcceptableTokens(); 084 } 085 086 @Override 087 public void visitToken(DetailAST ast) { 088 if (ast.getChildCount() == 0) { 089 //empty for iterator. test pad after semi. 090 final DetailAST semi = ast.getPreviousSibling(); 091 final String line = getLines()[semi.getLineNo() - 1]; 092 final int after = semi.getColumnNo() + 1; 093 //don't check if at end of line 094 if (after < line.length()) { 095 if (getAbstractOption() == PadOption.NOSPACE 096 && Character.isWhitespace(line.charAt(after))) { 097 log(semi.getLineNo(), after, WS_FOLLOWED, SEMICOLON); 098 } 099 else if (getAbstractOption() == PadOption.SPACE 100 && !Character.isWhitespace(line.charAt(after))) { 101 log(semi.getLineNo(), after, WS_NOT_FOLLOWED, SEMICOLON); 102 } 103 } 104 } 105 } 106}