001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2019 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.CommonUtil; 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 */ 093@StatelessCheck 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 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}