View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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   * <pre>
41   * if (x == "something")
42   * </pre>
43   *
44   * <p>
45   * when they mean
46   * </p>
47   * <pre>
48   * if ("something".equals(x))
49   * </pre>
50   *
51   * <p>
52   * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
53   * </p>
54   *
55   * <p>
56   * Violation Message Keys:
57   * </p>
58   * <ul>
59   * <li>
60   * {@code string.literal.equality}
61   * </li>
62   * </ul>
63   *
64   * @since 3.2
65   * @noinspection HtmlTagCanBeJavadocTag
66   * @noinspectionreason HtmlTagCanBeJavadocTag - encoded symbols were not decoded
67   *      when replaced with Javadoc tag
68   */
69  @StatelessCheck
70  public class StringLiteralEqualityCheck extends AbstractCheck {
71  
72      /**
73       * A key is pointing to the warning message text in "messages.properties"
74       * file.
75       */
76      public static final String MSG_KEY = "string.literal.equality";
77  
78      @Override
79      public int[] getDefaultTokens() {
80          return getRequiredTokens();
81      }
82  
83      @Override
84      public int[] getAcceptableTokens() {
85          return getRequiredTokens();
86      }
87  
88      @Override
89      public int[] getRequiredTokens() {
90          return new int[] {TokenTypes.EQUAL, TokenTypes.NOT_EQUAL};
91      }
92  
93      @Override
94      public void visitToken(DetailAST ast) {
95          final boolean hasStringLiteralChild = hasStringLiteralChild(ast);
96  
97          if (hasStringLiteralChild) {
98              log(ast, MSG_KEY, ast.getText());
99          }
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 }