1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2026 the original author or authors.
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ///////////////////////////////////////////////////////////////////////////////////////////////
19
20 package com.puppycrawl.tools.checkstyle.checks.coding;
21
22 import com.puppycrawl.tools.checkstyle.StatelessCheck;
23 import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
24 import com.puppycrawl.tools.checkstyle.api.DetailAST;
25 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
26
27 /**
28 * <div>
29 * Checks that string literals are not used with {@code ==} or {@code !=}.
30 * Since {@code ==} will compare the object references, not the actual value of the strings,
31 * {@code String.equals()} should be used.
32 * More information can be found
33 * <a href="https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java/">
34 * in this article</a>.
35 * </div>
36 *
37 * <p>
38 * Rationale: Novice Java programmers often use code like:
39 * </p>
40 * <div class="wrapper"><pre class="prettyprint"><code class="language-java">
41 * if (x == "something")
42 * </code></pre></div>
43 *
44 * <p>
45 * when they mean
46 * </p>
47 * <div class="wrapper"><pre class="prettyprint"><code class="language-java">
48 * if ("something".equals(x))
49 * </code></pre></div>
50 *
51 * @since 3.2
52 */
53 @StatelessCheck
54 public class StringLiteralEqualityCheck extends AbstractCheck {
55
56 /**
57 * A key is pointing to the warning message text in "messages.properties"
58 * file.
59 */
60 public static final String MSG_KEY = "string.literal.equality";
61
62 @Override
63 public int[] getDefaultTokens() {
64 return getRequiredTokens();
65 }
66
67 @Override
68 public int[] getAcceptableTokens() {
69 return getRequiredTokens();
70 }
71
72 @Override
73 public int[] getRequiredTokens() {
74 return new int[] {TokenTypes.EQUAL, TokenTypes.NOT_EQUAL};
75 }
76
77 @Override
78 public void visitToken(DetailAST ast) {
79 final boolean hasStringLiteralChild = hasStringLiteralChild(ast);
80
81 if (hasStringLiteralChild) {
82 log(ast, MSG_KEY, ast.getText());
83 }
84 }
85
86 /**
87 * Checks whether string literal or text block literals are concatenated.
88 *
89 * @param ast ast
90 * @return {@code true} if string literal or text block literals are concatenated
91 */
92 private static boolean hasStringLiteralChild(DetailAST ast) {
93 DetailAST currentAst = ast;
94 boolean result = false;
95 while (currentAst != null) {
96 if (currentAst.findFirstToken(TokenTypes.STRING_LITERAL) == null
97 && currentAst.findFirstToken(TokenTypes.TEXT_BLOCK_LITERAL_BEGIN) == null) {
98 currentAst = currentAst.findFirstToken(TokenTypes.PLUS);
99 }
100 else {
101 result = true;
102 break;
103 }
104 }
105 return result;
106 }
107
108 }