View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 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>==</code> or <code>&#33;=</code>.
30   * Since <code>==</code> will compare the object references, not the actual value of the strings,
31   * <code>String.equals()</code> 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   * @noinspection HtmlTagCanBeJavadocTag
53   * @noinspectionreason HtmlTagCanBeJavadocTag - encoded symbols were not decoded
54   *      when replaced with Javadoc tag
55   */
56  @StatelessCheck
57  public class StringLiteralEqualityCheck extends AbstractCheck {
58  
59      /**
60       * A key is pointing to the warning message text in "messages.properties"
61       * file.
62       */
63      public static final String MSG_KEY = "string.literal.equality";
64  
65      @Override
66      public int[] getDefaultTokens() {
67          return getRequiredTokens();
68      }
69  
70      @Override
71      public int[] getAcceptableTokens() {
72          return getRequiredTokens();
73      }
74  
75      @Override
76      public int[] getRequiredTokens() {
77          return new int[] {TokenTypes.EQUAL, TokenTypes.NOT_EQUAL};
78      }
79  
80      @Override
81      public void visitToken(DetailAST ast) {
82          final boolean hasStringLiteralChild = hasStringLiteralChild(ast);
83  
84          if (hasStringLiteralChild) {
85              log(ast, MSG_KEY, ast.getText());
86          }
87      }
88  
89      /**
90       * Checks whether string literal or text block literals are concatenated.
91       *
92       * @param ast ast
93       * @return {@code true} if string literal or text block literals are concatenated
94       */
95      private static boolean hasStringLiteralChild(DetailAST ast) {
96          DetailAST currentAst = ast;
97          boolean result = false;
98          while (currentAst != null) {
99              if (currentAst.findFirstToken(TokenTypes.STRING_LITERAL) == null
100                     && currentAst.findFirstToken(TokenTypes.TEXT_BLOCK_LITERAL_BEGIN) == null) {
101                 currentAst = currentAst.findFirstToken(TokenTypes.PLUS);
102             }
103             else {
104                 result = true;
105                 break;
106             }
107         }
108         return result;
109     }
110 
111 }