001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2018 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 com.puppycrawl.tools.checkstyle.StatelessCheck; 025import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 026import com.puppycrawl.tools.checkstyle.api.DetailAST; 027import com.puppycrawl.tools.checkstyle.api.TokenTypes; 028import com.puppycrawl.tools.checkstyle.utils.CommonUtil; 029 030/** 031 * <p> 032 * Checks line wrapping with separators. 033 * The policy to verify is specified using the {@link WrapOption} class 034 * and defaults to {@link WrapOption#EOL}. 035 * </p> 036 * <p> By default the check will check the following separators: 037 * {@link TokenTypes#DOT DOT}, 038 * {@link TokenTypes#COMMA COMMA}, 039 * Other acceptable tokens are 040 * {@link TokenTypes#SEMI SEMI}, 041 * {@link TokenTypes#ELLIPSIS ELLIPSIS}, 042 * {@link TokenTypes#AT AT}, 043 * {@link TokenTypes#LPAREN LPAREN}, 044 * {@link TokenTypes#RPAREN RPAREN}, 045 * {@link TokenTypes#ARRAY_DECLARATOR ARRAY_DECLARATOR}, 046 * {@link TokenTypes#RBRACK RBRACK}, 047 * </p> 048 * <p> 049 * Code example for comma and dot at the new line: 050 * </p> 051 * <pre> 052 * s 053 * .isEmpty(); 054 * foo(i 055 * ,s); 056 * </pre> 057 * <p> 058 * An example of how to configure the check is: 059 * </p> 060 * <pre> 061 * <module name="SeparatorWrap"/> 062 * </pre> 063 * <p> 064 * Code example for comma and dot at the previous line: 065 * </p> 066 * <pre> 067 * s. 068 * isEmpty(); 069 * foo(i, 070 * s); 071 * </pre> 072 * <p> An example of how to configure the check for comma at the 073 * new line is: 074 * </p> 075 * <pre> 076 * <module name="SeparatorWrap"> 077 * <property name="tokens" value="COMMA"/> 078 * <property name="option" value="nl"/> 079 * </module> 080 * </pre> 081 * 082 */ 083@StatelessCheck 084public class SeparatorWrapCheck 085 extends AbstractCheck { 086 087 /** 088 * A key is pointing to the warning message text in "messages.properties" 089 * file. 090 */ 091 public static final String MSG_LINE_PREVIOUS = "line.previous"; 092 093 /** 094 * A key is pointing to the warning message text in "messages.properties" 095 * file. 096 */ 097 public static final String MSG_LINE_NEW = "line.new"; 098 099 /** The policy to enforce. */ 100 private WrapOption option = WrapOption.EOL; 101 102 /** 103 * Set the option to enforce. 104 * @param optionStr string to decode option from 105 * @throws IllegalArgumentException if unable to decode 106 */ 107 public void setOption(String optionStr) { 108 try { 109 option = WrapOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH)); 110 } 111 catch (IllegalArgumentException iae) { 112 throw new IllegalArgumentException("unable to parse " + optionStr, iae); 113 } 114 } 115 116 @Override 117 public int[] getDefaultTokens() { 118 return new int[] { 119 TokenTypes.DOT, 120 TokenTypes.COMMA, 121 }; 122 } 123 124 @Override 125 public int[] getAcceptableTokens() { 126 return new int[] { 127 TokenTypes.DOT, 128 TokenTypes.COMMA, 129 TokenTypes.SEMI, 130 TokenTypes.ELLIPSIS, 131 TokenTypes.AT, 132 TokenTypes.LPAREN, 133 TokenTypes.RPAREN, 134 TokenTypes.ARRAY_DECLARATOR, 135 TokenTypes.RBRACK, 136 TokenTypes.METHOD_REF, 137 }; 138 } 139 140 @Override 141 public int[] getRequiredTokens() { 142 return CommonUtil.EMPTY_INT_ARRAY; 143 } 144 145 @Override 146 public void visitToken(DetailAST ast) { 147 final String text = ast.getText(); 148 final int colNo = ast.getColumnNo(); 149 final int lineNo = ast.getLineNo(); 150 final String currentLine = getLines()[lineNo - 1]; 151 final String substringAfterToken = 152 currentLine.substring(colNo + text.length()).trim(); 153 final String substringBeforeToken = 154 currentLine.substring(0, colNo).trim(); 155 156 if (option == WrapOption.EOL 157 && substringBeforeToken.isEmpty()) { 158 log(ast, MSG_LINE_PREVIOUS, text); 159 } 160 else if (option == WrapOption.NL 161 && substringAfterToken.isEmpty()) { 162 log(ast, MSG_LINE_NEW, text); 163 } 164 } 165 166}