001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2022 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="https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java/"> 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 * <p> 074 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 075 * </p> 076 * <p> 077 * Violation Message Keys: 078 * </p> 079 * <ul> 080 * <li> 081 * {@code string.literal.equality} 082 * </li> 083 * </ul> 084 * 085 * @since 3.2 086 * @noinspection HtmlTagCanBeJavadocTag 087 * @noinspectionreason HtmlTagCanBeJavadocTag - encoded symbols were not decoded 088 * when replaced with Javadoc tag 089 */ 090@StatelessCheck 091public class StringLiteralEqualityCheck extends AbstractCheck { 092 093 /** 094 * A key is pointing to the warning message text in "messages.properties" 095 * file. 096 */ 097 public static final String MSG_KEY = "string.literal.equality"; 098 099 @Override 100 public int[] getDefaultTokens() { 101 return getRequiredTokens(); 102 } 103 104 @Override 105 public int[] getAcceptableTokens() { 106 return getRequiredTokens(); 107 } 108 109 @Override 110 public int[] getRequiredTokens() { 111 return new int[] {TokenTypes.EQUAL, TokenTypes.NOT_EQUAL}; 112 } 113 114 @Override 115 public void visitToken(DetailAST ast) { 116 final boolean hasStringLiteralChild = 117 ast.findFirstToken(TokenTypes.STRING_LITERAL) != null 118 || ast.findFirstToken(TokenTypes.TEXT_BLOCK_LITERAL_BEGIN) != null 119 || areStringsConcatenated(ast); 120 121 if (hasStringLiteralChild) { 122 log(ast, MSG_KEY, ast.getText()); 123 } 124 } 125 126 /** 127 * Checks whether string literal or text block literals are concatenated. 128 * 129 * @param ast ast 130 * @return {@code true} if string literal or text block literals are concatenated 131 */ 132 private static boolean areStringsConcatenated(DetailAST ast) { 133 DetailAST plusAst = ast.findFirstToken(TokenTypes.PLUS); 134 boolean result = false; 135 while (plusAst != null) { 136 if (plusAst.findFirstToken(TokenTypes.STRING_LITERAL) == null 137 && plusAst.findFirstToken(TokenTypes.TEXT_BLOCK_LITERAL_BEGIN) == null) { 138 plusAst = plusAst.findFirstToken(TokenTypes.PLUS); 139 } 140 else { 141 result = true; 142 break; 143 } 144 } 145 return result; 146 } 147 148}