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