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