001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2020 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.CommonUtil; 029 030/** 031 * <p> 032 * Checks the padding of an empty for initializer; that is whether a white 033 * space is required at an empty for initializer, or such white space is 034 * forbidden. No check occurs if there is a line wrap at the initializer, as in 035 * </p> 036 * <pre> 037 * for ( 038 * ; i < j; i++, j--) 039 * </pre> 040 * <ul> 041 * <li> 042 * Property {@code option} - Specify policy on how to pad an empty for iterator. 043 * Type is {@code com.puppycrawl.tools.checkstyle.checks.whitespace.PadOption}. 044 * Default value is {@code nospace}. 045 * </li> 046 * </ul> 047 * <p> 048 * To configure the check: 049 * </p> 050 * <pre> 051 * <module name="EmptyForInitializerPad"/> 052 * </pre> 053 * <p> 054 * To configure the check to require white space at an empty for iterator: 055 * </p> 056 * <pre> 057 * <module name="EmptyForInitializerPad"> 058 * <property name="option" value="space"/> 059 * </module> 060 * </pre> 061 * <p> 062 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 063 * </p> 064 * <p> 065 * Violation Message Keys: 066 * </p> 067 * <ul> 068 * <li> 069 * {@code ws.notPreceded} 070 * </li> 071 * <li> 072 * {@code ws.preceded} 073 * </li> 074 * </ul> 075 * 076 * @since 3.4 077 */ 078@StatelessCheck 079public class EmptyForInitializerPadCheck 080 extends AbstractCheck { 081 082 /** 083 * A key is pointing to the warning message text in "messages.properties" 084 * file. 085 */ 086 public static final String MSG_PRECEDED = "ws.preceded"; 087 088 /** 089 * A key is pointing to the warning message text in "messages.properties" 090 * file. 091 */ 092 public static final String MSG_NOT_PRECEDED = "ws.notPreceded"; 093 094 /** Semicolon literal. */ 095 private static final String SEMICOLON = ";"; 096 097 /** Specify policy on how to pad an empty for iterator. */ 098 private PadOption option = PadOption.NOSPACE; 099 100 /** 101 * Setter to specify policy on how to pad an empty for iterator. 102 * 103 * @param optionStr string to decode option from 104 * @throws IllegalArgumentException if unable to decode 105 */ 106 public void setOption(String optionStr) { 107 option = PadOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH)); 108 } 109 110 @Override 111 public int[] getDefaultTokens() { 112 return getRequiredTokens(); 113 } 114 115 @Override 116 public int[] getAcceptableTokens() { 117 return getRequiredTokens(); 118 } 119 120 @Override 121 public int[] getRequiredTokens() { 122 return new int[] {TokenTypes.FOR_INIT}; 123 } 124 125 @Override 126 public void visitToken(DetailAST ast) { 127 if (!ast.hasChildren()) { 128 // empty for initializer. test pad before semi. 129 final DetailAST semi = ast.getNextSibling(); 130 final int semiLineIdx = semi.getLineNo() - 1; 131 final String line = getLines()[semiLineIdx]; 132 final int before = semi.getColumnNo() - 1; 133 // don't check if semi at beginning of line 134 if (!CommonUtil.hasWhitespaceBefore(before, line)) { 135 if (option == PadOption.NOSPACE 136 && Character.isWhitespace(line.charAt(before))) { 137 log(ast, MSG_PRECEDED, SEMICOLON); 138 } 139 else if (option == PadOption.SPACE 140 && !Character.isWhitespace(line.charAt(before))) { 141 log(ast, MSG_NOT_PRECEDED, SEMICOLON); 142 } 143 } 144 } 145 } 146 147}