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 java.util.Locale; 023 024import org.apache.commons.beanutils.ConversionException; 025import org.apache.commons.lang3.ArrayUtils; 026 027import com.puppycrawl.tools.checkstyle.api.Check; 028import com.puppycrawl.tools.checkstyle.api.DetailAST; 029import com.puppycrawl.tools.checkstyle.api.TokenTypes; 030import com.puppycrawl.tools.checkstyle.utils.CommonUtils; 031 032/** 033 * <p> 034 * Checks the padding between the identifier of a method definition, 035 * constructor definition, method call, or constructor invocation; 036 * and the left parenthesis of the parameter list. 037 * That is, if the identifier and left parenthesis are on the same line, 038 * checks whether a space is required immediately after the identifier or 039 * such a space is forbidden. 040 * If they are not on the same line, reports an error, unless configured to 041 * allow line breaks. 042 * </p> 043 * <p> By default the check will check the following tokens: 044 * {@link TokenTypes#CTOR_DEF CTOR_DEF}, 045 * {@link TokenTypes#LITERAL_NEW LITERAL_NEW}, 046 * {@link TokenTypes#METHOD_CALL METHOD_CALL}, 047 * {@link TokenTypes#METHOD_DEF METHOD_DEF}, 048 * {@link TokenTypes#SUPER_CTOR_CALL SUPER_CTOR_CALL}. 049 * </p> 050 * <p> 051 * An example of how to configure the check is: 052 * </p> 053 * <pre> 054 * <module name="MethodParamPad"/> 055 * </pre> 056 * <p> An example of how to configure the check to require a space 057 * after the identifier of a method definition, except if the left 058 * parenthesis occurs on a new line, is: 059 * </p> 060 * <pre> 061 * <module name="MethodParamPad"> 062 * <property name="tokens" value="METHOD_DEF"/> 063 * <property name="option" value="space"/> 064 * <property name="allowLineBreaks" value="true"/> 065 * </module> 066 * </pre> 067 * @author Rick Giles 068 */ 069 070public class MethodParamPadCheck 071 extends Check { 072 073 /** 074 * A key is pointing to the warning message text in "messages.properties" 075 * file. 076 */ 077 public static final String LINE_PREVIOUS = "line.previous"; 078 079 /** 080 * A key is pointing to the warning message text in "messages.properties" 081 * file. 082 */ 083 public static final String WS_PRECEDED = "ws.preceded"; 084 085 /** 086 * A key is pointing to the warning message text in "messages.properties" 087 * file. 088 */ 089 public static final String WS_NOT_PRECEDED = "ws.notPreceded"; 090 091 /** 092 * Whether whitespace is allowed if the method identifier is at a 093 * linebreak. 094 */ 095 private boolean allowLineBreaks; 096 097 /** The policy to enforce. */ 098 private PadOption option = PadOption.NOSPACE; 099 100 @Override 101 public int[] getDefaultTokens() { 102 return getAcceptableTokens(); 103 } 104 105 @Override 106 public int[] getAcceptableTokens() { 107 return new int[] { 108 TokenTypes.CTOR_DEF, 109 TokenTypes.LITERAL_NEW, 110 TokenTypes.METHOD_CALL, 111 TokenTypes.METHOD_DEF, 112 TokenTypes.SUPER_CTOR_CALL, 113 }; 114 } 115 116 @Override 117 public int[] getRequiredTokens() { 118 return ArrayUtils.EMPTY_INT_ARRAY; 119 } 120 121 @Override 122 public void visitToken(DetailAST ast) { 123 final DetailAST parenAST; 124 if (ast.getType() == TokenTypes.METHOD_CALL) { 125 parenAST = ast; 126 } 127 else { 128 parenAST = ast.findFirstToken(TokenTypes.LPAREN); 129 // array construction => parenAST == null 130 if (parenAST == null) { 131 return; 132 } 133 } 134 135 final String line = getLines()[parenAST.getLineNo() - 1]; 136 if (CommonUtils.hasWhitespaceBefore(parenAST.getColumnNo(), line)) { 137 if (!allowLineBreaks) { 138 log(parenAST, LINE_PREVIOUS, parenAST.getText()); 139 } 140 } 141 else { 142 final int before = parenAST.getColumnNo() - 1; 143 if (option == PadOption.NOSPACE 144 && Character.isWhitespace(line.charAt(before))) { 145 log(parenAST, WS_PRECEDED, parenAST.getText()); 146 } 147 else if (option == PadOption.SPACE 148 && !Character.isWhitespace(line.charAt(before))) { 149 log(parenAST, WS_NOT_PRECEDED, parenAST.getText()); 150 } 151 } 152 } 153 154 /** 155 * Control whether whitespace is flagged at line breaks. 156 * @param allowLineBreaks whether whitespace should be 157 * flagged at line breaks. 158 */ 159 public void setAllowLineBreaks(boolean allowLineBreaks) { 160 this.allowLineBreaks = allowLineBreaks; 161 } 162 163 /** 164 * Set the option to enforce. 165 * @param optionStr string to decode option from 166 * @throws ConversionException if unable to decode 167 */ 168 public void setOption(String optionStr) { 169 try { 170 option = PadOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH)); 171 } 172 catch (IllegalArgumentException iae) { 173 throw new ConversionException("unable to parse " + optionStr, iae); 174 } 175 } 176}