1 //////////////////////////////////////////////////////////////////////////////// 2 // checkstyle: Checks Java source code for adherence to a set of rules. 3 // Copyright (C) 2001-2015 the original author or authors. 4 // 5 // This library is free software; you can redistribute it and/or 6 // modify it under the terms of the GNU Lesser General Public 7 // License as published by the Free Software Foundation; either 8 // version 2.1 of the License, or (at your option) any later version. 9 // 10 // This library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 // Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public 16 // License along with this library; if not, write to the Free Software 17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 //////////////////////////////////////////////////////////////////////////////// 19 package com.puppycrawl.tools.checkstyle.checks.whitespace; 20 21 import com.puppycrawl.tools.checkstyle.api.Check; 22 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 23 import com.puppycrawl.tools.checkstyle.api.DetailAST; 24 25 /** 26 * <p> 27 * Checks that a token is followed by whitespace, with the exception that it 28 * does not check for whitespace after the semicolon of an empty for iterator. 29 * Use Check {@link EmptyForIteratorPadCheck EmptyForIteratorPad} to validate 30 * empty for iterators. 31 * </p> 32 * <p> By default the check will check the following tokens: 33 * {@link TokenTypes#COMMA COMMA}, 34 * {@link TokenTypes#SEMI SEMI}, 35 * {@link TokenTypes#TYPECAST TYPECAST}. 36 * </p> 37 * <p> 38 * An example of how to configure the check is: 39 * </p> 40 * <pre> 41 * <module name="WhitespaceAfter"/> 42 * </pre> 43 * <p> An example of how to configure the check for whitespace only after 44 * {@link TokenTypes#COMMA COMMA} and {@link TokenTypes#SEMI SEMI} tokens is: 45 * </p> 46 * <pre> 47 * <module name="WhitespaceAfter"> 48 * <property name="tokens" value="COMMA, SEMI"/> 49 * </module> 50 * </pre> 51 * @author Oliver Burn 52 * @author Rick Giles 53 */ 54 public class WhitespaceAfterCheck 55 extends Check 56 { 57 58 /** 59 * A key is pointing to the warning message text in "messages.properties" 60 * file. 61 */ 62 public static final String WS_NOT_FOLLOWED = "ws.notFollowed"; 63 64 @Override 65 public int[] getDefaultTokens() 66 { 67 return new int[] { 68 TokenTypes.COMMA, 69 TokenTypes.SEMI, 70 TokenTypes.TYPECAST, 71 }; 72 } 73 74 @Override 75 public int[] getAcceptableTokens() 76 { 77 return new int[] { 78 TokenTypes.COMMA, 79 TokenTypes.SEMI, 80 TokenTypes.TYPECAST, 81 }; 82 } 83 84 @Override 85 public void visitToken(DetailAST ast) 86 { 87 final Object[] message; 88 final DetailAST targetAST; 89 if (ast.getType() == TokenTypes.TYPECAST) { 90 targetAST = ast.findFirstToken(TokenTypes.RPAREN); 91 // TODO: i18n 92 message = new Object[]{"cast"}; 93 } 94 else { 95 targetAST = ast; 96 message = new Object[]{ast.getText()}; 97 } 98 final String line = getLine(targetAST.getLineNo() - 1); 99 final int after = 100 targetAST.getColumnNo() + targetAST.getText().length(); 101 102 if (after < line.length()) { 103 104 final char charAfter = line.charAt(after); 105 if (targetAST.getType() == TokenTypes.SEMI 106 && (charAfter == ';' || charAfter == ')')) 107 { 108 return; 109 } 110 if (!Character.isWhitespace(charAfter)) { 111 //empty FOR_ITERATOR? 112 if (targetAST.getType() == TokenTypes.SEMI) { 113 final DetailAST sibling = 114 targetAST.getNextSibling(); 115 if (sibling != null 116 && sibling.getType() == TokenTypes.FOR_ITERATOR 117 && sibling.getChildCount() == 0) 118 { 119 return; 120 } 121 } 122 log(targetAST.getLineNo(), 123 targetAST.getColumnNo() + targetAST.getText().length(), 124 WS_NOT_FOLLOWED, 125 message); 126 } 127 } 128 } 129 }