001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2024 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 * <pre> 038 * for ( 039 * ; i < j; i++, j--) 040 * </pre> 041 * <ul> 042 * <li> 043 * Property {@code option} - Specify policy on how to pad an empty for iterator. 044 * Type is {@code com.puppycrawl.tools.checkstyle.checks.whitespace.PadOption}. 045 * Default value is {@code nospace}. 046 * </li> 047 * </ul> 048 * 049 * <p> 050 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 051 * </p> 052 * 053 * <p> 054 * Violation Message Keys: 055 * </p> 056 * <ul> 057 * <li> 058 * {@code ws.notPreceded} 059 * </li> 060 * <li> 061 * {@code ws.preceded} 062 * </li> 063 * </ul> 064 * 065 * @since 3.4 066 */ 067@StatelessCheck 068public class EmptyForInitializerPadCheck 069 extends AbstractCheck { 070 071 /** 072 * A key is pointing to the warning message text in "messages.properties" 073 * file. 074 */ 075 public static final String MSG_PRECEDED = "ws.preceded"; 076 077 /** 078 * A key is pointing to the warning message text in "messages.properties" 079 * file. 080 */ 081 public static final String MSG_NOT_PRECEDED = "ws.notPreceded"; 082 083 /** Semicolon literal. */ 084 private static final String SEMICOLON = ";"; 085 086 /** Specify policy on how to pad an empty for iterator. */ 087 private PadOption option = PadOption.NOSPACE; 088 089 /** 090 * Setter to specify policy on how to pad an empty for iterator. 091 * 092 * @param optionStr string to decode option from 093 * @throws IllegalArgumentException if unable to decode 094 * @since 3.4 095 */ 096 public void setOption(String optionStr) { 097 option = PadOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH)); 098 } 099 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}