1 //////////////////////////////////////////////////////////////////////////////// 2 // checkstyle: Checks Java source code for adherence to a set of rules. 3 // Copyright (C) 2001-2019 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 29 /** 30 * <p>Checks the padding of an empty for iterator; that is whether a 31 * space is required at an empty for iterator, or such spaces are 32 * forbidden. No check occurs if there is a line wrap at the iterator, as in 33 * </p> 34 * <pre class="body"> 35 for (Iterator foo = very.long.line.iterator(); 36 foo.hasNext(); 37 ) 38 </pre> 39 * <p> 40 * The policy to verify is specified using the {@link PadOption} class and 41 * defaults to {@link PadOption#NOSPACE}. 42 * </p> 43 * <p> 44 * An example of how to configure the check is: 45 * </p> 46 * <pre> 47 * <module name="EmptyForIteratorPad"/> 48 * </pre> 49 * 50 */ 51 @StatelessCheck 52 public class EmptyForIteratorPadCheck 53 extends AbstractCheck { 54 55 /** 56 * A key is pointing to the warning message text in "messages.properties" 57 * file. 58 */ 59 public static final String MSG_WS_FOLLOWED = "ws.followed"; 60 61 /** 62 * A key is pointing to the warning message text in "messages.properties" 63 * file. 64 */ 65 public static final String MSG_WS_NOT_FOLLOWED = "ws.notFollowed"; 66 67 /** Semicolon literal. */ 68 private static final String SEMICOLON = ";"; 69 70 /** The policy to enforce. */ 71 private PadOption option = PadOption.NOSPACE; 72 73 /** 74 * Set the option to enforce. 75 * @param optionStr string to decode option from 76 * @throws IllegalArgumentException if unable to decode 77 */ 78 public void setOption(String optionStr) { 79 option = PadOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH)); 80 } 81 82 @Override 83 public int[] getDefaultTokens() { 84 return getRequiredTokens(); 85 } 86 87 @Override 88 public int[] getAcceptableTokens() { 89 return getRequiredTokens(); 90 } 91 92 @Override 93 public int[] getRequiredTokens() { 94 return new int[] {TokenTypes.FOR_ITERATOR}; 95 } 96 97 @Override 98 public void visitToken(DetailAST ast) { 99 if (ast.getChildCount() == 0) { 100 //empty for iterator. test pad after semi. 101 final DetailAST semi = ast.getPreviousSibling(); 102 final String line = getLines()[semi.getLineNo() - 1]; 103 final int after = semi.getColumnNo() + 1; 104 //don't check if at end of line 105 if (after < line.length()) { 106 if (option == PadOption.NOSPACE 107 && Character.isWhitespace(line.charAt(after))) { 108 log(ast, MSG_WS_FOLLOWED, SEMICOLON); 109 } 110 else if (option == PadOption.SPACE 111 && !Character.isWhitespace(line.charAt(after))) { 112 log(ast, MSG_WS_NOT_FOLLOWED, SEMICOLON); 113 } 114 } 115 } 116 } 117 118 }