001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2020 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 com.puppycrawl.tools.checkstyle.StatelessCheck; 023import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 024import com.puppycrawl.tools.checkstyle.api.DetailAST; 025import com.puppycrawl.tools.checkstyle.api.TokenTypes; 026 027/** 028 * <p> 029 * Checks that string literals are not used with <code>==</code> or <code>!=</code>. 030 * Since <code>==</code> will compare the object references, not the actual value of the strings, 031 * <code>String.equals()</code> should be used. 032 * More information can be found 033 * <a href="http://www.thejavageek.com/2013/07/27/string-comparison-with-equals-and-assignment-operator/"> 034 * in this article</a>. 035 * </p> 036 * <p> 037 * Rationale: Novice Java programmers often use code like: 038 * </p> 039 * <pre> 040 * if (x == "something") 041 * </pre> 042 * <p> 043 * when they mean 044 * </p> 045 * <pre> 046 * if ("something".equals(x)) 047 * </pre> 048 * <p> 049 * To configure the check: 050 * </p> 051 * <pre> 052 * <module name="StringLiteralEquality"/> 053 * </pre> 054 * <p> 055 * Examples of violations: 056 * </p> 057 * <pre> 058 * String status = "pending"; 059 * 060 * if (status == "done") {} // violation 061 * 062 * while (status != "done") {} // violation 063 * 064 * boolean flag = (status == "done"); // violation 065 * 066 * boolean flag = (status.equals("done")); // OK 067 * 068 * String name = "X"; 069 * 070 * if (name == getName()) {} 071 * // OK, limitation that check cannot tell runtime type returned from method call 072 * </pre> 073 * 074 * @since 3.2 075 * @noinspection HtmlTagCanBeJavadocTag 076 */ 077@StatelessCheck 078public class StringLiteralEqualityCheck extends AbstractCheck { 079 080 /** 081 * A key is pointing to the warning message text in "messages.properties" 082 * file. 083 */ 084 public static final String MSG_KEY = "string.literal.equality"; 085 086 @Override 087 public int[] getDefaultTokens() { 088 return getRequiredTokens(); 089 } 090 091 @Override 092 public int[] getAcceptableTokens() { 093 return getRequiredTokens(); 094 } 095 096 @Override 097 public int[] getRequiredTokens() { 098 return new int[] {TokenTypes.EQUAL, TokenTypes.NOT_EQUAL}; 099 } 100 101 @Override 102 public void visitToken(DetailAST ast) { 103 // no need to check for nulls here, == and != always have two children 104 final DetailAST firstChild = ast.getFirstChild(); 105 final DetailAST secondChild = firstChild.getNextSibling(); 106 107 if (firstChild.getType() == TokenTypes.STRING_LITERAL 108 || secondChild.getType() == TokenTypes.STRING_LITERAL) { 109 log(ast, MSG_KEY, ast.getText()); 110 } 111 } 112 113}