001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2018 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>Checks the padding of an empty for initializer; that is whether a 032 * space is required at an empty for initializer, or such spaces are 033 * forbidden. No check occurs if there is a line wrap at the initializer, as in 034 * </p> 035 * <pre class="body"> 036for ( 037 ; i < j; i++, j--) 038 </pre> 039 * <p> 040 * The policy to verify is specified using the {@link PadOption} class and 041 * defaults to {@link PadOption#NOSPACE}. 042 * </p> 043 * <p> 044 * An example of how to configure the check is: 045 * </p> 046 * <pre> 047 * <module name="EmptyForInitializerPad"/> 048 * </pre> 049 * 050 */ 051@StatelessCheck 052public class EmptyForInitializerPadCheck 053 extends AbstractCheck { 054 055 /** 056 * A key is pointing to the warning message text in "messages.properties" 057 * file. 058 */ 059 public static final String MSG_PRECEDED = "ws.preceded"; 060 061 /** 062 * A key is pointing to the warning message text in "messages.properties" 063 * file. 064 */ 065 public static final String MSG_NOT_PRECEDED = "ws.notPreceded"; 066 067 /** Semicolon literal. */ 068 private static final String SEMICOLON = ";"; 069 070 /** The policy to enforce. */ 071 private PadOption option = PadOption.NOSPACE; 072 073 /** 074 * Set the option to enforce. 075 * @param optionStr string to decode option from 076 * @throws IllegalArgumentException if unable to decode 077 */ 078 public void setOption(String optionStr) { 079 try { 080 option = PadOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH)); 081 } 082 catch (IllegalArgumentException iae) { 083 throw new IllegalArgumentException("unable to parse " + optionStr, iae); 084 } 085 } 086 087 @Override 088 public int[] getDefaultTokens() { 089 return getRequiredTokens(); 090 } 091 092 @Override 093 public int[] getAcceptableTokens() { 094 return getRequiredTokens(); 095 } 096 097 @Override 098 public int[] getRequiredTokens() { 099 return new int[] {TokenTypes.FOR_INIT}; 100 } 101 102 @Override 103 public void visitToken(DetailAST ast) { 104 if (ast.getChildCount() == 0) { 105 //empty for initializer. test pad before semi. 106 final DetailAST semi = ast.getNextSibling(); 107 final int semiLineIdx = semi.getLineNo() - 1; 108 final String line = getLines()[semiLineIdx]; 109 final int before = semi.getColumnNo() - 1; 110 //don't check if semi at beginning of line 111 if (!CommonUtil.hasWhitespaceBefore(before, line)) { 112 if (option == PadOption.NOSPACE 113 && Character.isWhitespace(line.charAt(before))) { 114 log(semi.getLineNo(), before, MSG_PRECEDED, SEMICOLON); 115 } 116 else if (option == PadOption.SPACE 117 && !Character.isWhitespace(line.charAt(before))) { 118 log(semi.getLineNo(), before, MSG_NOT_PRECEDED, SEMICOLON); 119 } 120 } 121 } 122 } 123 124}