001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2017 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.AbstractCheck; 023import com.puppycrawl.tools.checkstyle.api.DetailAST; 024import com.puppycrawl.tools.checkstyle.api.TokenTypes; 025import com.puppycrawl.tools.checkstyle.utils.CommonUtils; 026 027/** 028 * <p> 029 * Checks that there is no whitespace before a token. 030 * More specifically, it checks that it is not preceded with whitespace, 031 * or (if line breaks are allowed) all characters on the line before are 032 * whitespace. To allow line breaks before a token, set property 033 * allowLineBreaks to true. 034 * </p> 035 * <p> By default the check will check the following operators: 036 * {@link TokenTypes#SEMI SEMI}, 037 * {@link TokenTypes#POST_DEC POST_DEC}, 038 * {@link TokenTypes#POST_INC POST_INC}. 039 * {@link TokenTypes#DOT DOT} is also an acceptable token in a configuration 040 * of this check. 041 * </p> 042 * 043 * <p> 044 * An example of how to configure the check is: 045 * </p> 046 * <pre> 047 * <module name="NoWhitespaceBefore"/> 048 * </pre> 049 * <p> An example of how to configure the check to allow line breaks before 050 * a {@link TokenTypes#DOT DOT} token is: 051 * </p> 052 * <pre> 053 * <module name="NoWhitespaceBefore"> 054 * <property name="tokens" value="DOT"/> 055 * <property name="allowLineBreaks" value="true"/> 056 * </module> 057 * </pre> 058 * @author Rick Giles 059 * @author lkuehne 060 */ 061public class NoWhitespaceBeforeCheck 062 extends AbstractCheck { 063 064 /** 065 * A key is pointing to the warning message text in "messages.properties" 066 * file. 067 */ 068 public static final String MSG_KEY = "ws.preceded"; 069 070 /** Whether whitespace is allowed if the AST is at a linebreak. */ 071 private boolean allowLineBreaks; 072 073 @Override 074 public int[] getDefaultTokens() { 075 return new int[] { 076 TokenTypes.COMMA, 077 TokenTypes.SEMI, 078 TokenTypes.POST_INC, 079 TokenTypes.POST_DEC, 080 }; 081 } 082 083 @Override 084 public int[] getAcceptableTokens() { 085 return new int[] { 086 TokenTypes.COMMA, 087 TokenTypes.SEMI, 088 TokenTypes.POST_INC, 089 TokenTypes.POST_DEC, 090 TokenTypes.DOT, 091 TokenTypes.GENERIC_START, 092 TokenTypes.GENERIC_END, 093 }; 094 } 095 096 @Override 097 public int[] getRequiredTokens() { 098 return CommonUtils.EMPTY_INT_ARRAY; 099 } 100 101 @Override 102 public void visitToken(DetailAST ast) { 103 final String line = getLine(ast.getLineNo() - 1); 104 final int before = ast.getColumnNo() - 1; 105 106 if ((before < 0 || Character.isWhitespace(line.charAt(before))) 107 && !isInEmptyForInitializer(ast)) { 108 109 boolean flag = !allowLineBreaks; 110 // verify all characters before '.' are whitespace 111 for (int i = 0; !flag && i < before; i++) { 112 if (!Character.isWhitespace(line.charAt(i))) { 113 flag = true; 114 } 115 } 116 if (flag) { 117 log(ast.getLineNo(), before, MSG_KEY, ast.getText()); 118 } 119 } 120 } 121 122 /** 123 * Checks that semicolon is in empty for initializer. 124 * @param semicolonAst DetailAST of semicolon. 125 * @return true if semicolon is in empty for initializer. 126 */ 127 private static boolean isInEmptyForInitializer(DetailAST semicolonAst) { 128 boolean result = false; 129 if (semicolonAst.getType() == TokenTypes.SEMI) { 130 final DetailAST sibling = semicolonAst.getPreviousSibling(); 131 if (sibling != null 132 && sibling.getType() == TokenTypes.FOR_INIT 133 && sibling.getChildCount() == 0) { 134 result = true; 135 } 136 } 137 return result; 138 } 139 140 /** 141 * Control whether whitespace is flagged at line breaks. 142 * @param allowLineBreaks whether whitespace should be 143 * flagged at line breaks. 144 */ 145 public void setAllowLineBreaks(boolean allowLineBreaks) { 146 this.allowLineBreaks = allowLineBreaks; 147 } 148}