1 //////////////////////////////////////////////////////////////////////////////// 2 // checkstyle: Checks Java source code for adherence to a set of rules. 3 // Copyright (C) 2001-2019 the original author or authors. 4 // 5 // This library is free software; you can redistribute it and/or 6 // modify it under the terms of the GNU Lesser General Public 7 // License as published by the Free Software Foundation; either 8 // version 2.1 of the License, or (at your option) any later version. 9 // 10 // This library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 // Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public 16 // License along with this library; if not, write to the Free Software 17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 //////////////////////////////////////////////////////////////////////////////// 19 20 package com.puppycrawl.tools.checkstyle.checks.whitespace; 21 22 import java.util.Locale; 23 24 import com.puppycrawl.tools.checkstyle.StatelessCheck; 25 import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 26 import com.puppycrawl.tools.checkstyle.api.DetailAST; 27 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 28 import com.puppycrawl.tools.checkstyle.utils.CommonUtil; 29 30 /** 31 * <p> 32 * Checks line wrapping for operators. 33 * The policy to verify is specified using the {@link WrapOption} class 34 * and defaults to {@link WrapOption#NL}. 35 * </p> 36 * <p> By default the check will check the following operators: 37 * {@link TokenTypes#BAND BAND}, 38 * {@link TokenTypes#BOR BOR}, 39 * {@link TokenTypes#BSR BSR}, 40 * {@link TokenTypes#BXOR BXOR}, 41 * {@link TokenTypes#COLON COLON}, 42 * {@link TokenTypes#DIV DIV}, 43 * {@link TokenTypes#EQUAL EQUAL}, 44 * {@link TokenTypes#GE GE}, 45 * {@link TokenTypes#GT GT}, 46 * {@link TokenTypes#LAND LAND}, 47 * {@link TokenTypes#LE LE}, 48 * {@link TokenTypes#LITERAL_INSTANCEOF LITERAL_INSTANCEOF}, 49 * {@link TokenTypes#LOR LOR}, 50 * {@link TokenTypes#LT LT}, 51 * {@link TokenTypes#MINUS MINUS}, 52 * {@link TokenTypes#MOD MOD}, 53 * {@link TokenTypes#NOT_EQUAL NOT_EQUAL}, 54 * {@link TokenTypes#PLUS PLUS}, 55 * {@link TokenTypes#QUESTION QUESTION}, 56 * {@link TokenTypes#SL SL}, 57 * {@link TokenTypes#SR SR}, 58 * {@link TokenTypes#STAR STAR}. 59 * Other acceptable tokens are 60 * {@link TokenTypes#ASSIGN ASSIGN}, 61 * {@link TokenTypes#BAND_ASSIGN BAND_ASSIGN}, 62 * {@link TokenTypes#BOR_ASSIGN BOR_ASSIGN}, 63 * {@link TokenTypes#BSR_ASSIGN BSR_ASSIGN}, 64 * {@link TokenTypes#BXOR_ASSIGN BXOR_ASSIGN}, 65 * {@link TokenTypes#DIV_ASSIGN DIV_ASSIGN}, 66 * {@link TokenTypes#MINUS_ASSIGN MINUS_ASSIGN}, 67 * {@link TokenTypes#MOD_ASSIGN MOD_ASSIGN}, 68 * {@link TokenTypes#PLUS_ASSIGN PLUS_ASSIGN}, 69 * {@link TokenTypes#SL_ASSIGN SL_ASSIGN}, 70 * {@link TokenTypes#SR_ASSIGN SR_ASSIGN}, 71 * {@link TokenTypes#STAR_ASSIGN STAR_ASSIGN}. 72 * {@link TokenTypes#METHOD_REF METHOD_REF}. 73 * </p> 74 * <p> 75 * An example of how to configure the check is: 76 * </p> 77 * <pre> 78 * <module name="OperatorWrap"/> 79 * </pre> 80 * <p> An example of how to configure the check for assignment operators at the 81 * end of a line is: 82 * </p> 83 * <pre> 84 * <module name="OperatorWrap"> 85 * <property name="tokens" 86 * value="ASSIGN,DIV_ASSIGN,PLUS_ASSIGN,MINUS_ASSIGN,STAR_ASSIGN,MOD_ASSIGN 87 * ,SR_ASSIGN,BSR_ASSIGN,SL_ASSIGN,BXOR_ASSIGN,BOR_ASSIGN,BAND_ASSIGN"/> 88 * <property name="option" value="eol"/> 89 * </module> 90 * </pre> 91 * 92 */ 93 @StatelessCheck 94 public class OperatorWrapCheck 95 extends AbstractCheck { 96 97 /** 98 * A key is pointing to the warning message text in "messages.properties" 99 * 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 IllegalArgumentException if unable to decode 116 */ 117 public void setOption(String optionStr) { 118 option = WrapOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH)); 119 } 120 121 @Override 122 public int[] getDefaultTokens() { 123 return new int[] { 124 TokenTypes.QUESTION, // '?' 125 TokenTypes.COLON, // ':' (not reported for a case) 126 TokenTypes.EQUAL, // "==" 127 TokenTypes.NOT_EQUAL, // "!=" 128 TokenTypes.DIV, // '/' 129 TokenTypes.PLUS, //' +' (unary plus is UNARY_PLUS) 130 TokenTypes.MINUS, // '-' (unary minus is UNARY_MINUS) 131 TokenTypes.STAR, // '*' 132 TokenTypes.MOD, // '%' 133 TokenTypes.SR, // ">>" 134 TokenTypes.BSR, // ">>>" 135 TokenTypes.GE, // ">=" 136 TokenTypes.GT, // ">" 137 TokenTypes.SL, // "<<" 138 TokenTypes.LE, // "<=" 139 TokenTypes.LT, // '<' 140 TokenTypes.BXOR, // '^' 141 TokenTypes.BOR, // '|' 142 TokenTypes.LOR, // "||" 143 TokenTypes.BAND, // '&' 144 TokenTypes.LAND, // "&&" 145 TokenTypes.TYPE_EXTENSION_AND, 146 TokenTypes.LITERAL_INSTANCEOF, 147 }; 148 } 149 150 @Override 151 public int[] getAcceptableTokens() { 152 return new int[] { 153 TokenTypes.QUESTION, // '?' 154 TokenTypes.COLON, // ':' (not reported for a case) 155 TokenTypes.EQUAL, // "==" 156 TokenTypes.NOT_EQUAL, // "!=" 157 TokenTypes.DIV, // '/' 158 TokenTypes.PLUS, //' +' (unary plus is UNARY_PLUS) 159 TokenTypes.MINUS, // '-' (unary minus is UNARY_MINUS) 160 TokenTypes.STAR, // '*' 161 TokenTypes.MOD, // '%' 162 TokenTypes.SR, // ">>" 163 TokenTypes.BSR, // ">>>" 164 TokenTypes.GE, // ">=" 165 TokenTypes.GT, // ">" 166 TokenTypes.SL, // "<<" 167 TokenTypes.LE, // "<=" 168 TokenTypes.LT, // '<' 169 TokenTypes.BXOR, // '^' 170 TokenTypes.BOR, // '|' 171 TokenTypes.LOR, // "||" 172 TokenTypes.BAND, // '&' 173 TokenTypes.LAND, // "&&" 174 TokenTypes.LITERAL_INSTANCEOF, 175 TokenTypes.TYPE_EXTENSION_AND, 176 TokenTypes.ASSIGN, // '=' 177 TokenTypes.DIV_ASSIGN, // "/=" 178 TokenTypes.PLUS_ASSIGN, // "+=" 179 TokenTypes.MINUS_ASSIGN, //"-=" 180 TokenTypes.STAR_ASSIGN, // "*=" 181 TokenTypes.MOD_ASSIGN, // "%=" 182 TokenTypes.SR_ASSIGN, // ">>=" 183 TokenTypes.BSR_ASSIGN, // ">>>=" 184 TokenTypes.SL_ASSIGN, // "<<=" 185 TokenTypes.BXOR_ASSIGN, // "^=" 186 TokenTypes.BOR_ASSIGN, // "|=" 187 TokenTypes.BAND_ASSIGN, // "&=" 188 TokenTypes.METHOD_REF, // "::" 189 }; 190 } 191 192 @Override 193 public int[] getRequiredTokens() { 194 return CommonUtil.EMPTY_INT_ARRAY; 195 } 196 197 @Override 198 public void visitToken(DetailAST ast) { 199 final DetailAST parent = ast.getParent(); 200 //we do not want to check colon for cases and defaults 201 if (parent.getType() != TokenTypes.LITERAL_DEFAULT 202 && parent.getType() != TokenTypes.LITERAL_CASE) { 203 final String text = ast.getText(); 204 final int colNo = ast.getColumnNo(); 205 final int lineNo = ast.getLineNo(); 206 final String currentLine = getLine(lineNo - 1); 207 208 // Check if rest of line is whitespace, and not just the operator 209 // by itself. This last bit is to handle the operator on a line by 210 // itself. 211 if (option == WrapOption.NL 212 && !text.equals(currentLine.trim()) 213 && CommonUtil.isBlank(currentLine.substring(colNo + text.length()))) { 214 log(ast, MSG_LINE_NEW, text); 215 } 216 else if (option == WrapOption.EOL 217 && CommonUtil.hasWhitespaceBefore(colNo - 1, currentLine)) { 218 log(ast, MSG_LINE_PREVIOUS, text); 219 } 220 } 221 } 222 223 }