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.CommonUtils; 029 030/** 031 * <p> 032 * Checks line wrapping for operators. 033 * The policy to verify is specified using the {@link WrapOption} class 034 * and defaults to {@link WrapOption#NL}. 035 * </p> 036 * <p> By default the check will check the following operators: 037 * {@link TokenTypes#BAND BAND}, 038 * {@link TokenTypes#BOR BOR}, 039 * {@link TokenTypes#BSR BSR}, 040 * {@link TokenTypes#BXOR BXOR}, 041 * {@link TokenTypes#COLON COLON}, 042 * {@link TokenTypes#DIV DIV}, 043 * {@link TokenTypes#EQUAL EQUAL}, 044 * {@link TokenTypes#GE GE}, 045 * {@link TokenTypes#GT GT}, 046 * {@link TokenTypes#LAND LAND}, 047 * {@link TokenTypes#LE LE}, 048 * {@link TokenTypes#LITERAL_INSTANCEOF LITERAL_INSTANCEOF}, 049 * {@link TokenTypes#LOR LOR}, 050 * {@link TokenTypes#LT LT}, 051 * {@link TokenTypes#MINUS MINUS}, 052 * {@link TokenTypes#MOD MOD}, 053 * {@link TokenTypes#NOT_EQUAL NOT_EQUAL}, 054 * {@link TokenTypes#PLUS PLUS}, 055 * {@link TokenTypes#QUESTION QUESTION}, 056 * {@link TokenTypes#SL SL}, 057 * {@link TokenTypes#SR SR}, 058 * {@link TokenTypes#STAR STAR}. 059 * Other acceptable tokens are 060 * {@link TokenTypes#ASSIGN ASSIGN}, 061 * {@link TokenTypes#BAND_ASSIGN BAND_ASSIGN}, 062 * {@link TokenTypes#BOR_ASSIGN BOR_ASSIGN}, 063 * {@link TokenTypes#BSR_ASSIGN BSR_ASSIGN}, 064 * {@link TokenTypes#BXOR_ASSIGN BXOR_ASSIGN}, 065 * {@link TokenTypes#DIV_ASSIGN DIV_ASSIGN}, 066 * {@link TokenTypes#MINUS_ASSIGN MINUS_ASSIGN}, 067 * {@link TokenTypes#MOD_ASSIGN MOD_ASSIGN}, 068 * {@link TokenTypes#PLUS_ASSIGN PLUS_ASSIGN}, 069 * {@link TokenTypes#SL_ASSIGN SL_ASSIGN}, 070 * {@link TokenTypes#SR_ASSIGN SR_ASSIGN}, 071 * {@link TokenTypes#STAR_ASSIGN STAR_ASSIGN}. 072 * {@link TokenTypes#METHOD_REF METHOD_REF}. 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 */ 094@StatelessCheck 095public class OperatorWrapCheck 096 extends AbstractCheck { 097 098 /** 099 * A key is pointing to the warning message text in "messages.properties" 100 * file. 101 */ 102 public static final String MSG_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 MSG_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 IllegalArgumentException 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 IllegalArgumentException("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 TokenTypes.METHOD_REF, // "::" 195 }; 196 } 197 198 @Override 199 public int[] getRequiredTokens() { 200 return CommonUtils.EMPTY_INT_ARRAY; 201 } 202 203 @Override 204 public void visitToken(DetailAST ast) { 205 final DetailAST parent = ast.getParent(); 206 //we do not want to check colon for cases and defaults 207 if (ast.getType() != TokenTypes.COLON 208 || parent.getType() != TokenTypes.LITERAL_DEFAULT 209 && parent.getType() != TokenTypes.LITERAL_CASE) { 210 final String text = ast.getText(); 211 final int colNo = ast.getColumnNo(); 212 final int lineNo = ast.getLineNo(); 213 final String currentLine = getLine(lineNo - 1); 214 215 // Check if rest of line is whitespace, and not just the operator 216 // by itself. This last bit is to handle the operator on a line by 217 // itself. 218 if (option == WrapOption.NL 219 && !text.equals(currentLine.trim()) 220 && CommonUtils.isBlank(currentLine.substring(colNo + text.length()))) { 221 log(lineNo, colNo, MSG_LINE_NEW, text); 222 } 223 else if (option == WrapOption.EOL 224 && CommonUtils.hasWhitespaceBefore(colNo - 1, currentLine)) { 225 log(lineNo, colNo, MSG_LINE_PREVIOUS, text); 226 } 227 } 228 } 229 230}