001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2024 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 * <div> 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 * </div> 036 * 037 * <p> 038 * Rationale: Novice Java programmers often use code like: 039 * </p> 040 * <pre> 041 * if (x == "something") 042 * </pre> 043 * 044 * <p> 045 * when they mean 046 * </p> 047 * <pre> 048 * if ("something".equals(x)) 049 * </pre> 050 * 051 * <p> 052 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 053 * </p> 054 * 055 * <p> 056 * Violation Message Keys: 057 * </p> 058 * <ul> 059 * <li> 060 * {@code string.literal.equality} 061 * </li> 062 * </ul> 063 * 064 * @since 3.2 065 * @noinspection HtmlTagCanBeJavadocTag 066 * @noinspectionreason HtmlTagCanBeJavadocTag - encoded symbols were not decoded 067 * when replaced with Javadoc tag 068 */ 069@StatelessCheck 070public class StringLiteralEqualityCheck extends AbstractCheck { 071 072 /** 073 * A key is pointing to the warning message text in "messages.properties" 074 * file. 075 */ 076 public static final String MSG_KEY = "string.literal.equality"; 077 078 @Override 079 public int[] getDefaultTokens() { 080 return getRequiredTokens(); 081 } 082 083 @Override 084 public int[] getAcceptableTokens() { 085 return getRequiredTokens(); 086 } 087 088 @Override 089 public int[] getRequiredTokens() { 090 return new int[] {TokenTypes.EQUAL, TokenTypes.NOT_EQUAL}; 091 } 092 093 @Override 094 public void visitToken(DetailAST ast) { 095 final boolean hasStringLiteralChild = hasStringLiteralChild(ast); 096 097 if (hasStringLiteralChild) { 098 log(ast, MSG_KEY, ast.getText()); 099 } 100 } 101 102 /** 103 * Checks whether string literal or text block literals are concatenated. 104 * 105 * @param ast ast 106 * @return {@code true} if string literal or text block literals are concatenated 107 */ 108 private static boolean hasStringLiteralChild(DetailAST ast) { 109 DetailAST currentAst = ast; 110 boolean result = false; 111 while (currentAst != null) { 112 if (currentAst.findFirstToken(TokenTypes.STRING_LITERAL) == null 113 && currentAst.findFirstToken(TokenTypes.TEXT_BLOCK_LITERAL_BEGIN) == null) { 114 currentAst = currentAst.findFirstToken(TokenTypes.PLUS); 115 } 116 else { 117 result = true; 118 break; 119 } 120 } 121 return result; 122 } 123 124}