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.coding; 021 022import java.util.Collections; 023import java.util.HashSet; 024import java.util.Set; 025 026import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 027import com.puppycrawl.tools.checkstyle.api.DetailAST; 028import com.puppycrawl.tools.checkstyle.api.TokenTypes; 029 030/** 031 * Checks that any combination of String literals 032 * is on the left side of an equals() comparison. 033 * Also checks for String literals assigned to some field 034 * (such as <code>someString.equals(anotherString = "text")</code>). 035 * 036 * <p>Rationale: Calling the equals() method on String literals 037 * will avoid a potential NullPointerException. Also, it is 038 * pretty common to see null check right before equals comparisons 039 * which is not necessary in the below example. 040 * 041 * <p>For example: 042 * 043 * <pre> 044 * {@code 045 * String nullString = null; 046 * nullString.equals("My_Sweet_String"); 047 * } 048 * </pre> 049 * should be refactored to 050 * 051 * <pre> 052 * {@code 053 * String nullString = null; 054 * "My_Sweet_String".equals(nullString); 055 * } 056 * </pre> 057 * 058 * @author Travis Schneeberger 059 * @author Vladislav Lisetskiy 060 */ 061public class EqualsAvoidNullCheck extends AbstractCheck { 062 063 /** 064 * A key is pointing to the warning message text in "messages.properties" 065 * file. 066 */ 067 public static final String MSG_EQUALS_AVOID_NULL = "equals.avoid.null"; 068 069 /** 070 * A key is pointing to the warning message text in "messages.properties" 071 * file. 072 */ 073 public static final String MSG_EQUALS_IGNORE_CASE_AVOID_NULL = "equalsIgnoreCase.avoid.null"; 074 075 /** Method name for comparison. */ 076 private static final String EQUALS = "equals"; 077 078 /** Type name for comparison. */ 079 private static final String STRING = "String"; 080 081 /** Whether to process equalsIgnoreCase() invocations. */ 082 private boolean ignoreEqualsIgnoreCase; 083 084 /** Stack of sets of field names, one for each class of a set of nested classes. */ 085 private FieldFrame currentFrame; 086 087 @Override 088 public int[] getDefaultTokens() { 089 return getAcceptableTokens(); 090 } 091 092 @Override 093 public int[] getAcceptableTokens() { 094 return new int[] { 095 TokenTypes.METHOD_CALL, 096 TokenTypes.CLASS_DEF, 097 TokenTypes.METHOD_DEF, 098 TokenTypes.LITERAL_IF, 099 TokenTypes.LITERAL_FOR, 100 TokenTypes.LITERAL_WHILE, 101 TokenTypes.LITERAL_DO, 102 TokenTypes.LITERAL_CATCH, 103 TokenTypes.LITERAL_TRY, 104 TokenTypes.VARIABLE_DEF, 105 TokenTypes.PARAMETER_DEF, 106 TokenTypes.CTOR_DEF, 107 TokenTypes.SLIST, 108 TokenTypes.ENUM_DEF, 109 TokenTypes.ENUM_CONSTANT_DEF, 110 TokenTypes.LITERAL_NEW, 111 }; 112 } 113 114 @Override 115 public int[] getRequiredTokens() { 116 return getAcceptableTokens(); 117 } 118 119 /** 120 * Whether to ignore checking {@code String.equalsIgnoreCase(String)}. 121 * @param newValue whether to ignore checking 122 * {@code String.equalsIgnoreCase(String)}. 123 */ 124 public void setIgnoreEqualsIgnoreCase(boolean newValue) { 125 ignoreEqualsIgnoreCase = newValue; 126 } 127 128 @Override 129 public void beginTree(DetailAST rootAST) { 130 currentFrame = new FieldFrame(null); 131 } 132 133 @Override 134 public void visitToken(final DetailAST ast) { 135 switch (ast.getType()) { 136 case TokenTypes.VARIABLE_DEF: 137 case TokenTypes.PARAMETER_DEF: 138 currentFrame.addField(ast); 139 break; 140 case TokenTypes.METHOD_CALL: 141 processMethodCall(ast); 142 break; 143 case TokenTypes.SLIST: 144 processSlist(ast); 145 break; 146 case TokenTypes.LITERAL_NEW: 147 processLiteralNew(ast); 148 break; 149 default: 150 processFrame(ast); 151 } 152 } 153 154 @Override 155 public void leaveToken(DetailAST ast) { 156 final int astType = ast.getType(); 157 if (astType != TokenTypes.VARIABLE_DEF 158 && astType != TokenTypes.PARAMETER_DEF 159 && astType != TokenTypes.METHOD_CALL 160 && astType != TokenTypes.SLIST 161 && astType != TokenTypes.LITERAL_NEW 162 || astType == TokenTypes.LITERAL_NEW 163 && ast.branchContains(TokenTypes.LCURLY)) { 164 currentFrame = currentFrame.getParent(); 165 } 166 else if (astType == TokenTypes.SLIST) { 167 leaveSlist(ast); 168 } 169 } 170 171 @Override 172 public void finishTree(DetailAST ast) { 173 traverseFieldFrameTree(currentFrame); 174 } 175 176 /** 177 * Determine whether SLIST begins static or non-static block and add it as 178 * a frame in this case. 179 * @param ast SLIST ast. 180 */ 181 private void processSlist(DetailAST ast) { 182 final int parentType = ast.getParent().getType(); 183 if (parentType == TokenTypes.SLIST 184 || parentType == TokenTypes.STATIC_INIT 185 || parentType == TokenTypes.INSTANCE_INIT) { 186 final FieldFrame frame = new FieldFrame(currentFrame); 187 currentFrame.addChild(frame); 188 currentFrame = frame; 189 } 190 } 191 192 /** 193 * Determine whether SLIST begins static or non-static block. 194 * @param ast SLIST ast. 195 */ 196 private void leaveSlist(DetailAST ast) { 197 final int parentType = ast.getParent().getType(); 198 if (parentType == TokenTypes.SLIST 199 || parentType == TokenTypes.STATIC_INIT 200 || parentType == TokenTypes.INSTANCE_INIT) { 201 currentFrame = currentFrame.getParent(); 202 } 203 } 204 205 /** 206 * Process CLASS_DEF, METHOD_DEF, LITERAL_IF, LITERAL_FOR, LITERAL_WHILE, LITERAL_DO, 207 * LITERAL_CATCH, LITERAL_TRY, CTOR_DEF, ENUM_DEF, ENUM_CONSTANT_DEF. 208 * @param ast processed ast. 209 */ 210 private void processFrame(DetailAST ast) { 211 final FieldFrame frame = new FieldFrame(currentFrame); 212 final int astType = ast.getType(); 213 if (astType == TokenTypes.CLASS_DEF 214 || astType == TokenTypes.ENUM_DEF 215 || astType == TokenTypes.ENUM_CONSTANT_DEF) { 216 frame.setClassOrEnumOrEnumConstDef(true); 217 frame.setFrameName(ast.findFirstToken(TokenTypes.IDENT).getText()); 218 } 219 currentFrame.addChild(frame); 220 currentFrame = frame; 221 } 222 223 /** 224 * Add the method call to the current frame if it should be processed. 225 * @param methodCall METHOD_CALL ast. 226 */ 227 private void processMethodCall(DetailAST methodCall) { 228 final DetailAST dot = methodCall.getFirstChild(); 229 if (dot.getType() == TokenTypes.DOT) { 230 final String methodName = dot.getLastChild().getText(); 231 if (EQUALS.equals(methodName) 232 || !ignoreEqualsIgnoreCase && "equalsIgnoreCase".equals(methodName)) { 233 currentFrame.addMethodCall(methodCall); 234 } 235 } 236 } 237 238 /** 239 * Determine whether LITERAL_NEW is an anonymous class definition and add it as 240 * a frame in this case. 241 * @param ast LITERAL_NEW ast. 242 */ 243 private void processLiteralNew(DetailAST ast) { 244 if (ast.branchContains(TokenTypes.LCURLY)) { 245 final FieldFrame frame = new FieldFrame(currentFrame); 246 currentFrame.addChild(frame); 247 currentFrame = frame; 248 } 249 } 250 251 /** 252 * Traverse the tree of the field frames to check all equals method calls. 253 * @param frame to check method calls in. 254 */ 255 private void traverseFieldFrameTree(FieldFrame frame) { 256 for (FieldFrame child: frame.getChildren()) { 257 if (!child.getChildren().isEmpty()) { 258 traverseFieldFrameTree(child); 259 } 260 currentFrame = child; 261 child.getMethodCalls().forEach(this::checkMethodCall); 262 } 263 } 264 265 /** 266 * Check whether the method call should be violated. 267 * @param methodCall method call to check. 268 */ 269 private void checkMethodCall(DetailAST methodCall) { 270 DetailAST objCalledOn = methodCall.getFirstChild().getFirstChild(); 271 if (objCalledOn.getType() == TokenTypes.DOT) { 272 objCalledOn = objCalledOn.getLastChild(); 273 } 274 final DetailAST expr = methodCall.findFirstToken(TokenTypes.ELIST).getFirstChild(); 275 if (isObjectValid(objCalledOn) 276 && containsOneArgument(methodCall) 277 && containsAllSafeTokens(expr) 278 && isCalledOnStringFieldOrVariable(objCalledOn)) { 279 final String methodName = methodCall.getFirstChild().getLastChild().getText(); 280 if (EQUALS.equals(methodName)) { 281 log(methodCall.getLineNo(), methodCall.getColumnNo(), 282 MSG_EQUALS_AVOID_NULL); 283 } 284 else { 285 log(methodCall.getLineNo(), methodCall.getColumnNo(), 286 MSG_EQUALS_IGNORE_CASE_AVOID_NULL); 287 } 288 } 289 } 290 291 /** 292 * Check whether the object equals method is called on is not a String literal 293 * and not too complex. 294 * @param objCalledOn the object equals method is called on ast. 295 * @return true if the object is valid. 296 */ 297 private static boolean isObjectValid(DetailAST objCalledOn) { 298 boolean result = true; 299 final DetailAST previousSibling = objCalledOn.getPreviousSibling(); 300 if (previousSibling != null 301 && previousSibling.getType() == TokenTypes.DOT) { 302 result = false; 303 } 304 if (isStringLiteral(objCalledOn)) { 305 result = false; 306 } 307 return result; 308 } 309 310 /** 311 * Checks for calling equals on String literal and 312 * anon object which cannot be null. 313 * @param objCalledOn object AST 314 * @return if it is string literal 315 */ 316 private static boolean isStringLiteral(DetailAST objCalledOn) { 317 return objCalledOn.getType() == TokenTypes.STRING_LITERAL 318 || objCalledOn.getType() == TokenTypes.LITERAL_NEW; 319 } 320 321 /** 322 * Verify that method call has one argument. 323 * 324 * @param methodCall METHOD_CALL DetailAST 325 * @return true if method call has one argument. 326 */ 327 private static boolean containsOneArgument(DetailAST methodCall) { 328 final DetailAST elist = methodCall.findFirstToken(TokenTypes.ELIST); 329 return elist.getChildCount() == 1; 330 } 331 332 /** 333 * Looks for all "safe" Token combinations in the argument 334 * expression branch. 335 * @param expr the argument expression 336 * @return - true if any child matches the set of tokens, false if not 337 */ 338 private static boolean containsAllSafeTokens(final DetailAST expr) { 339 DetailAST arg = expr.getFirstChild(); 340 arg = skipVariableAssign(arg); 341 342 boolean argIsNotNull = false; 343 if (arg.getType() == TokenTypes.PLUS) { 344 DetailAST child = arg.getFirstChild(); 345 while (child != null 346 && !argIsNotNull) { 347 argIsNotNull = child.getType() == TokenTypes.STRING_LITERAL 348 || child.getType() == TokenTypes.IDENT; 349 child = child.getNextSibling(); 350 } 351 } 352 353 return argIsNotNull 354 || !arg.branchContains(TokenTypes.IDENT) 355 && !arg.branchContains(TokenTypes.LITERAL_NULL); 356 } 357 358 /** 359 * Skips over an inner assign portion of an argument expression. 360 * @param currentAST current token in the argument expression 361 * @return the next relevant token 362 */ 363 private static DetailAST skipVariableAssign(final DetailAST currentAST) { 364 if (currentAST.getType() == TokenTypes.ASSIGN 365 && currentAST.getFirstChild().getType() == TokenTypes.IDENT) { 366 return currentAST.getFirstChild().getNextSibling(); 367 } 368 return currentAST; 369 } 370 371 /** 372 * Determine, whether equals method is called on a field of String type. 373 * @param objCalledOn object ast. 374 * @return true if the object is of String type. 375 */ 376 private boolean isCalledOnStringFieldOrVariable(DetailAST objCalledOn) { 377 final boolean result; 378 final DetailAST previousSiblingAst = objCalledOn.getPreviousSibling(); 379 if (previousSiblingAst == null) { 380 result = isStringFieldOrVariable(objCalledOn); 381 } 382 else { 383 if (previousSiblingAst.getType() == TokenTypes.LITERAL_THIS) { 384 result = isStringFieldOrVariableFromThisInstance(objCalledOn); 385 } 386 else { 387 final String className = previousSiblingAst.getText(); 388 result = isStringFieldOrVariableFromClass(objCalledOn, className); 389 } 390 } 391 return result; 392 } 393 394 /** 395 * Whether the field or the variable is of String type. 396 * @param objCalledOn the field or the variable to check. 397 * @return true if the field or the variable is of String type. 398 */ 399 private boolean isStringFieldOrVariable(DetailAST objCalledOn) { 400 boolean result = false; 401 final String name = objCalledOn.getText(); 402 FieldFrame frame = currentFrame; 403 while (frame != null) { 404 final DetailAST field = frame.findField(name); 405 if (field != null 406 && (frame.isClassOrEnumOrEnumConstDef() 407 || checkLineNo(field, objCalledOn))) { 408 result = STRING.equals(getFieldType(field)); 409 break; 410 } 411 frame = frame.getParent(); 412 } 413 return result; 414 } 415 416 /** 417 * Whether the field or the variable from THIS instance is of String type. 418 * @param objCalledOn the field or the variable from THIS instance to check. 419 * @return true if the field or the variable from THIS instance is of String type. 420 */ 421 private boolean isStringFieldOrVariableFromThisInstance(DetailAST objCalledOn) { 422 boolean result = false; 423 final String name = objCalledOn.getText(); 424 final DetailAST field = getObjectFrame(currentFrame).findField(name); 425 if (field != null) { 426 result = STRING.equals(getFieldType(field)); 427 } 428 return result; 429 } 430 431 /** 432 * Whether the field or the variable from the specified class is of String type. 433 * @param objCalledOn the field or the variable from the specified class to check. 434 * @param className the name of the class to check in. 435 * @return true if the field or the variable from the specified class is of String type. 436 */ 437 private boolean isStringFieldOrVariableFromClass(DetailAST objCalledOn, 438 final String className) { 439 boolean result = false; 440 final String name = objCalledOn.getText(); 441 FieldFrame frame = getObjectFrame(currentFrame); 442 while (frame != null) { 443 if (className.equals(frame.getFrameName())) { 444 final DetailAST field = frame.findField(name); 445 if (field != null) { 446 result = STRING.equals(getFieldType(field)); 447 } 448 break; 449 } 450 frame = getObjectFrame(frame.getParent()); 451 } 452 return result; 453 } 454 455 /** 456 * Get the nearest parent frame which is CLASS_DEF, ENUM_DEF or ENUM_CONST_DEF. 457 * @param frame to start the search from. 458 * @return the nearest parent frame which is CLASS_DEF, ENUM_DEF or ENUM_CONST_DEF. 459 */ 460 private static FieldFrame getObjectFrame(FieldFrame frame) { 461 FieldFrame objectFrame = frame; 462 while (objectFrame != null && !objectFrame.isClassOrEnumOrEnumConstDef()) { 463 objectFrame = objectFrame.getParent(); 464 } 465 return objectFrame; 466 } 467 468 /** 469 * Check whether the field is declared before the method call in case of 470 * methods and initialization blocks. 471 * @param field field to check. 472 * @param objCalledOn object equals method called on. 473 * @return true if the field is declared before the method call. 474 */ 475 private static boolean checkLineNo(DetailAST field, DetailAST objCalledOn) { 476 boolean result = false; 477 if (field.getLineNo() < objCalledOn.getLineNo() 478 || field.getLineNo() == objCalledOn.getLineNo() 479 && field.getColumnNo() < objCalledOn.getColumnNo()) { 480 result = true; 481 } 482 return result; 483 } 484 485 /** 486 * Get field type. 487 * @param field to get the type from. 488 * @return type of the field. 489 */ 490 private static String getFieldType(DetailAST field) { 491 String fieldType = null; 492 final DetailAST identAst = field.findFirstToken(TokenTypes.TYPE) 493 .findFirstToken(TokenTypes.IDENT); 494 if (identAst != null) { 495 fieldType = identAst.getText(); 496 } 497 return fieldType; 498 } 499 500 /** 501 * Holds the names of fields of a type. 502 */ 503 private static class FieldFrame { 504 /** Parent frame. */ 505 private final FieldFrame parent; 506 507 /** Set of frame's children. */ 508 private final Set<FieldFrame> children = new HashSet<>(); 509 510 /** Set of fields. */ 511 private final Set<DetailAST> fields = new HashSet<>(); 512 513 /** Set of equals calls. */ 514 private final Set<DetailAST> methodCalls = new HashSet<>(); 515 516 /** Name of the class, enum or enum constant declaration. */ 517 private String frameName; 518 519 /** Whether the frame is CLASS_DEF, ENUM_DEF or ENUM_CONST_DEF. */ 520 private boolean classOrEnumOrEnumConstDef; 521 522 /** 523 * Creates new frame. 524 * @param parent parent frame. 525 */ 526 FieldFrame(FieldFrame parent) { 527 this.parent = parent; 528 } 529 530 /** 531 * Set the frame name. 532 * @param frameName value to set. 533 */ 534 public void setFrameName(String frameName) { 535 this.frameName = frameName; 536 } 537 538 /** 539 * Getter for the frame name. 540 * @return frame name. 541 */ 542 public String getFrameName() { 543 return frameName; 544 } 545 546 /** 547 * Getter for the parent frame. 548 * @return parent frame. 549 */ 550 public FieldFrame getParent() { 551 return parent; 552 } 553 554 /** 555 * Getter for frame's children. 556 * @return children of this frame. 557 */ 558 public Set<FieldFrame> getChildren() { 559 return Collections.unmodifiableSet(children); 560 } 561 562 /** 563 * Add child frame to this frame. 564 * @param child frame to add. 565 */ 566 public void addChild(FieldFrame child) { 567 children.add(child); 568 } 569 570 /** 571 * Add field to this FieldFrame. 572 * @param field the ast of the field. 573 */ 574 public void addField(DetailAST field) { 575 fields.add(field); 576 } 577 578 /** 579 * Sets isClassOrEnum. 580 * @param value value to set. 581 */ 582 public void setClassOrEnumOrEnumConstDef(boolean value) { 583 classOrEnumOrEnumConstDef = value; 584 } 585 586 /** 587 * Getter for classOrEnumOrEnumConstDef. 588 * @return classOrEnumOrEnumConstDef. 589 */ 590 public boolean isClassOrEnumOrEnumConstDef() { 591 return classOrEnumOrEnumConstDef; 592 } 593 594 /** 595 * Add method call to this frame. 596 * @param methodCall METHOD_CALL ast. 597 */ 598 public void addMethodCall(DetailAST methodCall) { 599 methodCalls.add(methodCall); 600 } 601 602 /** 603 * Determines whether this FieldFrame contains the field. 604 * @param name name of the field to check. 605 * @return true if this FieldFrame contains instance field field. 606 */ 607 public DetailAST findField(String name) { 608 for (DetailAST field: fields) { 609 if (getFieldName(field).equals(name)) { 610 return field; 611 } 612 } 613 return null; 614 } 615 616 /** 617 * Getter for frame's method calls. 618 * @return method calls of this frame. 619 */ 620 public Set<DetailAST> getMethodCalls() { 621 return Collections.unmodifiableSet(methodCalls); 622 } 623 624 /** 625 * Get the name of the field. 626 * @param field to get the name from. 627 * @return name of the field. 628 */ 629 private static String getFieldName(DetailAST field) { 630 return field.findFirstToken(TokenTypes.IDENT).getText(); 631 } 632 } 633}