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 * <p> 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 * </p> 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 * <p> 49 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 50 * </p> 51 * <p> 52 * Violation Message Keys: 53 * </p> 54 * <ul> 55 * <li> 56 * {@code ws.notPreceded} 57 * </li> 58 * <li> 59 * {@code ws.preceded} 60 * </li> 61 * </ul> 62 * 63 * @since 3.4 64 */ 65 @StatelessCheck 66 public class EmptyForInitializerPadCheck 67 extends AbstractCheck { 68 69 /** 70 * A key is pointing to the warning message text in "messages.properties" 71 * file. 72 */ 73 public static final String MSG_PRECEDED = "ws.preceded"; 74 75 /** 76 * A key is pointing to the warning message text in "messages.properties" 77 * file. 78 */ 79 public static final String MSG_NOT_PRECEDED = "ws.notPreceded"; 80 81 /** Semicolon literal. */ 82 private static final String SEMICOLON = ";"; 83 84 /** Specify policy on how to pad an empty for iterator. */ 85 private PadOption option = PadOption.NOSPACE; 86 87 /** 88 * Setter to specify policy on how to pad an empty for iterator. 89 * 90 * @param optionStr string to decode option from 91 * @throws IllegalArgumentException if unable to decode 92 * @since 3.4 93 */ 94 public void setOption(String optionStr) { 95 option = PadOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH)); 96 } 97 98 @Override 99 public int[] getDefaultTokens() { 100 return getRequiredTokens(); 101 } 102 103 @Override 104 public int[] getAcceptableTokens() { 105 return getRequiredTokens(); 106 } 107 108 @Override 109 public int[] getRequiredTokens() { 110 return new int[] {TokenTypes.FOR_INIT}; 111 } 112 113 @Override 114 public void visitToken(DetailAST ast) { 115 if (!ast.hasChildren()) { 116 final int lineIdx = ast.getLineNo() - 1; 117 final int[] line = getLineCodePoints(lineIdx); 118 final int before = ast.getColumnNo() - 1; 119 // don't check if semi at beginning of line 120 if (ast.getColumnNo() > 0 && !CodePointUtil.hasWhitespaceBefore(before, line)) { 121 if (option == PadOption.NOSPACE 122 && CommonUtil.isCodePointWhitespace(line, before)) { 123 log(ast, MSG_PRECEDED, SEMICOLON); 124 } 125 else if (option == PadOption.SPACE 126 && !CommonUtil.isCodePointWhitespace(line, before)) { 127 log(ast, MSG_NOT_PRECEDED, SEMICOLON); 128 } 129 } 130 } 131 } 132 133 }