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 java.util.Locale; 023 024import org.apache.commons.beanutils.ConversionException; 025import org.apache.commons.lang3.ArrayUtils; 026import org.apache.commons.lang3.StringUtils; 027 028import com.puppycrawl.tools.checkstyle.api.Check; 029import com.puppycrawl.tools.checkstyle.api.DetailAST; 030import com.puppycrawl.tools.checkstyle.api.TokenTypes; 031import com.puppycrawl.tools.checkstyle.utils.CommonUtils; 032 033/** 034 * <p> 035 * Checks line wrapping for operators. 036 * The policy to verify is specified using the {@link WrapOption} class 037 * and defaults to {@link WrapOption#NL}. 038 * </p> 039 * <p> By default the check will check the following operators: 040 * {@link TokenTypes#BAND BAND}, 041 * {@link TokenTypes#BOR BOR}, 042 * {@link TokenTypes#BSR BSR}, 043 * {@link TokenTypes#BXOR BXOR}, 044 * {@link TokenTypes#COLON COLON}, 045 * {@link TokenTypes#DIV DIV}, 046 * {@link TokenTypes#EQUAL EQUAL}, 047 * {@link TokenTypes#GE GE}, 048 * {@link TokenTypes#GT GT}, 049 * {@link TokenTypes#LAND LAND}, 050 * {@link TokenTypes#LE LE}, 051 * {@link TokenTypes#LITERAL_INSTANCEOF LITERAL_INSTANCEOF}, 052 * {@link TokenTypes#LOR LOR}, 053 * {@link TokenTypes#LT LT}, 054 * {@link TokenTypes#MINUS MINUS}, 055 * {@link TokenTypes#MOD MOD}, 056 * {@link TokenTypes#NOT_EQUAL NOT_EQUAL}, 057 * {@link TokenTypes#PLUS PLUS}, 058 * {@link TokenTypes#QUESTION QUESTION}, 059 * {@link TokenTypes#SL SL}, 060 * {@link TokenTypes#SR SR}, 061 * {@link TokenTypes#STAR STAR}. 062 * Other acceptable tokens are 063 * {@link TokenTypes#ASSIGN ASSIGN}, 064 * {@link TokenTypes#BAND_ASSIGN BAND_ASSIGN}, 065 * {@link TokenTypes#BOR_ASSIGN BOR_ASSIGN}, 066 * {@link TokenTypes#BSR_ASSIGN BSR_ASSIGN}, 067 * {@link TokenTypes#BXOR_ASSIGN BXOR_ASSIGN}, 068 * {@link TokenTypes#DIV_ASSIGN DIV_ASSIGN}, 069 * {@link TokenTypes#MINUS_ASSIGN MINUS_ASSIGN}, 070 * {@link TokenTypes#MOD_ASSIGN MOD_ASSIGN}, 071 * {@link TokenTypes#PLUS_ASSIGN PLUS_ASSIGN}, 072 * {@link TokenTypes#SL_ASSIGN SL_ASSIGN}, 073 * {@link TokenTypes#SR_ASSIGN SR_ASSIGN}, 074 * {@link TokenTypes#STAR_ASSIGN STAR_ASSIGN}. 075 * </p> 076 * <p> 077 * An example of how to configure the check is: 078 * </p> 079 * <pre> 080 * <module name="OperatorWrap"/> 081 * </pre> 082 * <p> An example of how to configure the check for assignment operators at the 083 * end of a line is: 084 * </p> 085 * <pre> 086 * <module name="OperatorWrap"> 087 * <property name="tokens" 088 * value="ASSIGN,DIV_ASSIGN,PLUS_ASSIGN,MINUS_ASSIGN,STAR_ASSIGN,MOD_ASSIGN,SR_ASSIGN,BSR_ASSIGN,SL_ASSIGN,BXOR_ASSIGN,BOR_ASSIGN,BAND_ASSIGN"/> 089 * <property name="option" value="eol"/> 090 * </module> 091 * </pre> 092 * 093 * @author Rick Giles 094 */ 095public class OperatorWrapCheck 096 extends Check { 097 098 /** 099 * A key is pointing to the warning message text in "messages.properties" 100 * file. 101 */ 102 public static final String LINE_NEW = "line.new"; 103 104 /** 105 * A key is pointing to the warning message text in "messages.properties" 106 * file. 107 */ 108 public static final String LINE_PREVIOUS = "line.previous"; 109 110 /** The policy to enforce. */ 111 private WrapOption option = WrapOption.NL; 112 113 /** 114 * Set the option to enforce. 115 * @param optionStr string to decode option from 116 * @throws ConversionException if unable to decode 117 */ 118 public void setOption(String optionStr) { 119 try { 120 option = WrapOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH)); 121 } 122 catch (IllegalArgumentException iae) { 123 throw new ConversionException("unable to parse " + optionStr, iae); 124 } 125 } 126 127 @Override 128 public int[] getDefaultTokens() { 129 return new int[] { 130 TokenTypes.QUESTION, // '?' 131 TokenTypes.COLON, // ':' (not reported for a case) 132 TokenTypes.EQUAL, // "==" 133 TokenTypes.NOT_EQUAL, // "!=" 134 TokenTypes.DIV, // '/' 135 TokenTypes.PLUS, //' +' (unary plus is UNARY_PLUS) 136 TokenTypes.MINUS, // '-' (unary minus is UNARY_MINUS) 137 TokenTypes.STAR, // '*' 138 TokenTypes.MOD, // '%' 139 TokenTypes.SR, // ">>" 140 TokenTypes.BSR, // ">>>" 141 TokenTypes.GE, // ">=" 142 TokenTypes.GT, // ">" 143 TokenTypes.SL, // "<<" 144 TokenTypes.LE, // "<=" 145 TokenTypes.LT, // '<' 146 TokenTypes.BXOR, // '^' 147 TokenTypes.BOR, // '|' 148 TokenTypes.LOR, // "||" 149 TokenTypes.BAND, // '&' 150 TokenTypes.LAND, // "&&" 151 TokenTypes.TYPE_EXTENSION_AND, 152 TokenTypes.LITERAL_INSTANCEOF, 153 }; 154 } 155 156 @Override 157 public int[] getAcceptableTokens() { 158 return new int[] { 159 TokenTypes.QUESTION, // '?' 160 TokenTypes.COLON, // ':' (not reported for a case) 161 TokenTypes.EQUAL, // "==" 162 TokenTypes.NOT_EQUAL, // "!=" 163 TokenTypes.DIV, // '/' 164 TokenTypes.PLUS, //' +' (unary plus is UNARY_PLUS) 165 TokenTypes.MINUS, // '-' (unary minus is UNARY_MINUS) 166 TokenTypes.STAR, // '*' 167 TokenTypes.MOD, // '%' 168 TokenTypes.SR, // ">>" 169 TokenTypes.BSR, // ">>>" 170 TokenTypes.GE, // ">=" 171 TokenTypes.GT, // ">" 172 TokenTypes.SL, // "<<" 173 TokenTypes.LE, // "<=" 174 TokenTypes.LT, // '<' 175 TokenTypes.BXOR, // '^' 176 TokenTypes.BOR, // '|' 177 TokenTypes.LOR, // "||" 178 TokenTypes.BAND, // '&' 179 TokenTypes.LAND, // "&&" 180 TokenTypes.LITERAL_INSTANCEOF, 181 TokenTypes.TYPE_EXTENSION_AND, 182 TokenTypes.ASSIGN, // '=' 183 TokenTypes.DIV_ASSIGN, // "/=" 184 TokenTypes.PLUS_ASSIGN, // "+=" 185 TokenTypes.MINUS_ASSIGN, //"-=" 186 TokenTypes.STAR_ASSIGN, // "*=" 187 TokenTypes.MOD_ASSIGN, // "%=" 188 TokenTypes.SR_ASSIGN, // ">>=" 189 TokenTypes.BSR_ASSIGN, // ">>>=" 190 TokenTypes.SL_ASSIGN, // "<<=" 191 TokenTypes.BXOR_ASSIGN, // "^=" 192 TokenTypes.BOR_ASSIGN, // "|=" 193 TokenTypes.BAND_ASSIGN, // "&=" 194 195 }; 196 } 197 198 @Override 199 public int[] getRequiredTokens() { 200 return ArrayUtils.EMPTY_INT_ARRAY; 201 } 202 203 @Override 204 public void visitToken(DetailAST ast) { 205 if (ast.getType() == TokenTypes.COLON) { 206 final DetailAST parent = ast.getParent(); 207 if (parent.getType() == TokenTypes.LITERAL_DEFAULT 208 || parent.getType() == TokenTypes.LITERAL_CASE) { 209 //we do not want to check colon for cases and defaults 210 return; 211 } 212 } 213 214 final String text = ast.getText(); 215 final int colNo = ast.getColumnNo(); 216 final int lineNo = ast.getLineNo(); 217 final String currentLine = getLine(lineNo - 1); 218 219 // Check if rest of line is whitespace, and not just the operator 220 // by itself. This last bit is to handle the operator on a line by 221 // itself. 222 if (option == WrapOption.NL 223 && !text.equals(currentLine.trim()) 224 && StringUtils.isBlank(currentLine.substring(colNo + text.length()))) { 225 log(lineNo, colNo, LINE_NEW, text); 226 } 227 else if (option == WrapOption.EOL 228 && CommonUtils.hasWhitespaceBefore(colNo - 1, currentLine)) { 229 log(lineNo, colNo, LINE_PREVIOUS, text); 230 } 231 } 232}