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 * <p> 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 * </p> 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 * <p> 049 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 050 * </p> 051 * <p> 052 * Violation Message Keys: 053 * </p> 054 * <ul> 055 * <li> 056 * {@code ws.notPreceded} 057 * </li> 058 * <li> 059 * {@code ws.preceded} 060 * </li> 061 * </ul> 062 * 063 * @since 3.4 064 */ 065@StatelessCheck 066public class EmptyForInitializerPadCheck 067 extends AbstractCheck { 068 069 /** 070 * A key is pointing to the warning message text in "messages.properties" 071 * file. 072 */ 073 public static final String MSG_PRECEDED = "ws.preceded"; 074 075 /** 076 * A key is pointing to the warning message text in "messages.properties" 077 * file. 078 */ 079 public static final String MSG_NOT_PRECEDED = "ws.notPreceded"; 080 081 /** Semicolon literal. */ 082 private static final String SEMICOLON = ";"; 083 084 /** Specify policy on how to pad an empty for iterator. */ 085 private PadOption option = PadOption.NOSPACE; 086 087 /** 088 * Setter to specify policy on how to pad an empty for iterator. 089 * 090 * @param optionStr string to decode option from 091 * @throws IllegalArgumentException if unable to decode 092 * @since 3.4 093 */ 094 public void setOption(String optionStr) { 095 option = PadOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH)); 096 } 097 098 @Override 099 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}