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 org.apache.commons.lang3.ArrayUtils; 023 024import com.puppycrawl.tools.checkstyle.api.DetailAST; 025import com.puppycrawl.tools.checkstyle.api.TokenTypes; 026import com.puppycrawl.tools.checkstyle.checks.AbstractOptionCheck; 027 028/** 029 * <p> 030 * Checks line wrapping with separators. 031 * The policy to verify is specified using the {@link WrapOption} class 032 * and defaults to {@link WrapOption#EOL}. 033 * </p> 034 * <p> By default the check will check the following separators: 035 * {@link TokenTypes#DOT DOT}, 036 * {@link TokenTypes#COMMA COMMA}, 037 * Other acceptable tokens are 038 * {@link TokenTypes#SEMI SEMI}, 039 * {@link TokenTypes#ELLIPSIS ELLIPSIS}, 040 * {@link TokenTypes#AT AT}, 041 * {@link TokenTypes#LPAREN LPAREN}, 042 * {@link TokenTypes#RPAREN RPAREN}, 043 * {@link TokenTypes#ARRAY_DECLARATOR ARRAY_DECLARATOR}, 044 * {@link TokenTypes#RBRACK RBRACK}, 045 * </p> 046 * <p> 047 * Code example for comma and dot at the new line: 048 * </p> 049 * <pre> 050 * s 051 * .isEmpty(); 052 * foo(i 053 * ,s); 054 * </pre> 055 * <p> 056 * An example of how to configure the check is: 057 * </p> 058 * <pre> 059 * <module name="SeparatorWrap"/> 060 * </pre> 061 * <p> 062 * Code example for comma and dot at the previous line: 063 * </p> 064 * <pre> 065 * s. 066 * isEmpty(); 067 * foo(i, 068 * s); 069 * </pre> 070 * <p> An example of how to configure the check for comma at the 071 * new line is: 072 * </p> 073 * <pre> 074 * <module name="SeparatorWrap"> 075 * <property name="tokens" value="COMMA"/> 076 * <property name="option" value="nl"/> 077 * </module> 078 * </pre> 079 * 080 * @author maxvetrenko 081 */ 082public class SeparatorWrapCheck 083 extends AbstractOptionCheck<WrapOption> { 084 085 /** 086 * A key is pointing to the warning message text in "messages.properties" 087 * file. 088 */ 089 public static final String LINE_PREVIOUS = "line.previous"; 090 091 /** 092 * A key is pointing to the warning message text in "messages.properties" 093 * file. 094 */ 095 public static final String LINE_NEW = "line.new"; 096 097 /** 098 * Sets the comma wrap option to end of the line. 099 */ 100 public SeparatorWrapCheck() { 101 super(WrapOption.EOL, WrapOption.class); 102 } 103 104 @Override 105 public int[] getDefaultTokens() { 106 return new int[] { 107 TokenTypes.DOT, 108 TokenTypes.COMMA, 109 }; 110 } 111 112 @Override 113 public int[] getAcceptableTokens() { 114 return new int[] { 115 TokenTypes.DOT, 116 TokenTypes.COMMA, 117 TokenTypes.SEMI, 118 TokenTypes.ELLIPSIS, 119 TokenTypes.AT, 120 TokenTypes.LPAREN, 121 TokenTypes.RPAREN, 122 TokenTypes.ARRAY_DECLARATOR, 123 TokenTypes.RBRACK, 124 }; 125 } 126 127 @Override 128 public int[] getRequiredTokens() { 129 return ArrayUtils.EMPTY_INT_ARRAY; 130 } 131 132 @Override 133 public void visitToken(DetailAST ast) { 134 final String text = ast.getText(); 135 final int colNo = ast.getColumnNo(); 136 final int lineNo = ast.getLineNo(); 137 final String currentLine = getLines()[lineNo - 1]; 138 final String substringAfterToken = 139 currentLine.substring(colNo + text.length()).trim(); 140 final String substringBeforeToken = 141 currentLine.substring(0, colNo).trim(); 142 final WrapOption wSp = getAbstractOption(); 143 144 if (wSp == WrapOption.EOL 145 && substringBeforeToken.isEmpty()) { 146 log(lineNo, colNo, LINE_PREVIOUS, text); 147 } 148 else if (wSp == WrapOption.NL 149 && substringAfterToken.isEmpty()) { 150 log(lineNo, colNo, LINE_NEW, text); 151 } 152 } 153}