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