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 import com.puppycrawl.tools.checkstyle.utils.CommonUtil; 29 30 /** 31 * <p>Checks the padding of an empty for initializer; that is whether a 32 * space is required at an empty for initializer, or such spaces are 33 * forbidden. No check occurs if there is a line wrap at the initializer, as in 34 * </p> 35 * <pre class="body"> 36 for ( 37 ; i < j; i++, j--) 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="EmptyForInitializerPad"/> 48 * </pre> 49 * 50 */ 51 @StatelessCheck 52 public class EmptyForInitializerPadCheck 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_PRECEDED = "ws.preceded"; 60 61 /** 62 * A key is pointing to the warning message text in "messages.properties" 63 * file. 64 */ 65 public static final String MSG_NOT_PRECEDED = "ws.notPreceded"; 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_INIT}; 95 } 96 97 @Override 98 public void visitToken(DetailAST ast) { 99 if (ast.getChildCount() == 0) { 100 //empty for initializer. test pad before semi. 101 final DetailAST semi = ast.getNextSibling(); 102 final int semiLineIdx = semi.getLineNo() - 1; 103 final String line = getLines()[semiLineIdx]; 104 final int before = semi.getColumnNo() - 1; 105 //don't check if semi at beginning of line 106 if (!CommonUtil.hasWhitespaceBefore(before, line)) { 107 if (option == PadOption.NOSPACE 108 && Character.isWhitespace(line.charAt(before))) { 109 log(ast, MSG_PRECEDED, SEMICOLON); 110 } 111 else if (option == PadOption.SPACE 112 && !Character.isWhitespace(line.charAt(before))) { 113 log(ast, MSG_NOT_PRECEDED, SEMICOLON); 114 } 115 } 116 } 117 } 118 119 }