001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2015 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 com.puppycrawl.tools.checkstyle.api.DetailAST; 023import com.puppycrawl.tools.checkstyle.api.TokenTypes; 024import com.puppycrawl.tools.checkstyle.checks.AbstractOptionCheck; 025import com.puppycrawl.tools.checkstyle.utils.CommonUtils; 026 027/** 028 * <p>Checks the padding of an empty for initializer; that is whether a 029 * space is required at an empty for initializer, or such spaces are 030 * forbidden. No check occurs if there is a line wrap at the initializer, as in 031 * </p> 032 * <pre class="body"> 033for ( 034 ; i < j; i++, j--) 035 </pre> 036 * <p> 037 * The policy to verify is specified using the {@link PadOption} class and 038 * defaults to {@link PadOption#NOSPACE}. 039 * </p> 040 * <p> 041 * An example of how to configure the check is: 042 * </p> 043 * <pre> 044 * <module name="EmptyForInitializerPad"/> 045 * </pre> 046 * 047 * @author lkuehne 048 */ 049public class EmptyForInitializerPadCheck 050 extends AbstractOptionCheck<PadOption> { 051 052 /** 053 * A key is pointing to the warning message text in "messages.properties" 054 * file. 055 */ 056 public static final String MSG_PRECEDED = "ws.preceded"; 057 058 /** 059 * A key is pointing to the warning message text in "messages.properties" 060 * file. 061 */ 062 public static final String MSG_NOT_PRECEDED = "ws.notPreceded"; 063 064 /** 065 * Sets the paren pad option to nospace. 066 */ 067 public EmptyForInitializerPadCheck() { 068 super(PadOption.NOSPACE, PadOption.class); 069 } 070 071 @Override 072 public int[] getDefaultTokens() { 073 return getAcceptableTokens(); 074 } 075 076 @Override 077 public int[] getAcceptableTokens() { 078 return new int[] {TokenTypes.FOR_INIT}; 079 } 080 081 @Override 082 public int[] getRequiredTokens() { 083 return getAcceptableTokens(); 084 } 085 086 @Override 087 public void visitToken(DetailAST ast) { 088 if (ast.getChildCount() == 0) { 089 //empty for initializer. test pad before semi. 090 final DetailAST semi = ast.getNextSibling(); 091 final int semiLineIdx = semi.getLineNo() - 1; 092 final String line = getLines()[semiLineIdx]; 093 final int before = semi.getColumnNo() - 1; 094 //don't check if semi at beginning of line 095 if (!CommonUtils.hasWhitespaceBefore(before, line)) { 096 final PadOption option = getAbstractOption(); 097 if (option == PadOption.NOSPACE 098 && Character.isWhitespace(line.charAt(before))) { 099 log(semi.getLineNo(), before, MSG_PRECEDED, SEMICOLON); 100 } 101 else if (option == PadOption.SPACE 102 && !Character.isWhitespace(line.charAt(before))) { 103 log(semi.getLineNo(), before, MSG_NOT_PRECEDED, SEMICOLON); 104 } 105 } 106 } 107 } 108}