001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2026 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; 028import com.puppycrawl.tools.checkstyle.utils.CodePointUtil; 029import com.puppycrawl.tools.checkstyle.utils.CommonUtil; 030 031/** 032 * <div> 033 * Checks the padding of an empty for initializer; that is whether a white 034 * space is required at an empty for initializer, or such white space is 035 * forbidden. No check occurs if there is a line wrap at the initializer, as in 036 * </div> 037 * <div class="wrapper"><pre class="prettyprint"><code class="language-java"> 038 * for ( 039 * ; i < j; i++, j--) 040 * </code></pre></div> 041 * 042 * @since 3.4 043 */ 044@StatelessCheck 045public class EmptyForInitializerPadCheck 046 extends AbstractCheck { 047 048 /** 049 * A key is pointing to the warning message text in "messages.properties" 050 * file. 051 */ 052 public static final String MSG_PRECEDED = "ws.preceded"; 053 054 /** 055 * A key is pointing to the warning message text in "messages.properties" 056 * file. 057 */ 058 public static final String MSG_NOT_PRECEDED = "ws.notPreceded"; 059 060 /** Semicolon literal. */ 061 private static final String SEMICOLON = ";"; 062 063 /** Specify policy on how to pad an empty for iterator. */ 064 private PadOption option = PadOption.NOSPACE; 065 066 /** 067 * Creates a new {@code EmptyForInitializerPadCheck} instance. 068 */ 069 public EmptyForInitializerPadCheck() { 070 // no code by default 071 } 072 073 /** 074 * Setter to specify policy on how to pad an empty for iterator. 075 * 076 * @param optionStr string to decode option from 077 * @throws IllegalArgumentException if unable to decode 078 * @since 3.4 079 */ 080 public void setOption(String optionStr) { 081 option = PadOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH)); 082 } 083 084 @Override 085 public int[] getDefaultTokens() { 086 return getRequiredTokens(); 087 } 088 089 @Override 090 public int[] getAcceptableTokens() { 091 return getRequiredTokens(); 092 } 093 094 @Override 095 public int[] getRequiredTokens() { 096 return new int[] {TokenTypes.FOR_INIT}; 097 } 098 099 @Override 100 public void visitToken(DetailAST ast) { 101 if (!ast.hasChildren()) { 102 final int lineIdx = ast.getLineNo() - 1; 103 final int[] line = getLineCodePoints(lineIdx); 104 final int before = ast.getColumnNo() - 1; 105 // don't check if semi at beginning of line 106 if (ast.getColumnNo() > 0 && !CodePointUtil.hasWhitespaceBefore(before, line)) { 107 if (option == PadOption.NOSPACE 108 && CommonUtil.isCodePointWhitespace(line, before)) { 109 log(ast, MSG_PRECEDED, SEMICOLON); 110 } 111 else if (option == PadOption.SPACE 112 && !CommonUtil.isCodePointWhitespace(line, before)) { 113 log(ast, MSG_NOT_PRECEDED, SEMICOLON); 114 } 115 } 116 } 117 } 118 119}