1 /////////////////////////////////////////////////////////////////////////////////////////////// 2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules. 3 // Copyright (C) 2001-2024 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.CodePointUtil; 29 import com.puppycrawl.tools.checkstyle.utils.CommonUtil; 30 31 /** 32 * <div> 33 * Checks the padding of an empty for initializer; that is whether a white 34 * space is required at an empty for initializer, or such white space is 35 * forbidden. No check occurs if there is a line wrap at the initializer, as in 36 * </div> 37 * <pre> 38 * for ( 39 * ; i < j; i++, j--) 40 * </pre> 41 * <ul> 42 * <li> 43 * Property {@code option} - Specify policy on how to pad an empty for iterator. 44 * Type is {@code com.puppycrawl.tools.checkstyle.checks.whitespace.PadOption}. 45 * Default value is {@code nospace}. 46 * </li> 47 * </ul> 48 * 49 * <p> 50 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 51 * </p> 52 * 53 * <p> 54 * Violation Message Keys: 55 * </p> 56 * <ul> 57 * <li> 58 * {@code ws.notPreceded} 59 * </li> 60 * <li> 61 * {@code ws.preceded} 62 * </li> 63 * </ul> 64 * 65 * @since 3.4 66 */ 67 @StatelessCheck 68 public class EmptyForInitializerPadCheck 69 extends AbstractCheck { 70 71 /** 72 * A key is pointing to the warning message text in "messages.properties" 73 * file. 74 */ 75 public static final String MSG_PRECEDED = "ws.preceded"; 76 77 /** 78 * A key is pointing to the warning message text in "messages.properties" 79 * file. 80 */ 81 public static final String MSG_NOT_PRECEDED = "ws.notPreceded"; 82 83 /** Semicolon literal. */ 84 private static final String SEMICOLON = ";"; 85 86 /** Specify policy on how to pad an empty for iterator. */ 87 private PadOption option = PadOption.NOSPACE; 88 89 /** 90 * Setter to specify policy on how to pad an empty for iterator. 91 * 92 * @param optionStr string to decode option from 93 * @throws IllegalArgumentException if unable to decode 94 * @since 3.4 95 */ 96 public void setOption(String optionStr) { 97 option = PadOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH)); 98 } 99 100 @Override 101 public int[] getDefaultTokens() { 102 return getRequiredTokens(); 103 } 104 105 @Override 106 public int[] getAcceptableTokens() { 107 return getRequiredTokens(); 108 } 109 110 @Override 111 public int[] getRequiredTokens() { 112 return new int[] {TokenTypes.FOR_INIT}; 113 } 114 115 @Override 116 public void visitToken(DetailAST ast) { 117 if (!ast.hasChildren()) { 118 final int lineIdx = ast.getLineNo() - 1; 119 final int[] line = getLineCodePoints(lineIdx); 120 final int before = ast.getColumnNo() - 1; 121 // don't check if semi at beginning of line 122 if (ast.getColumnNo() > 0 && !CodePointUtil.hasWhitespaceBefore(before, line)) { 123 if (option == PadOption.NOSPACE 124 && CommonUtil.isCodePointWhitespace(line, before)) { 125 log(ast, MSG_PRECEDED, SEMICOLON); 126 } 127 else if (option == PadOption.SPACE 128 && !CommonUtil.isCodePointWhitespace(line, before)) { 129 log(ast, MSG_NOT_PRECEDED, SEMICOLON); 130 } 131 } 132 } 133 } 134 135 }