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