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