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   * <p>
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   * </p>
36   * <p>
37   * Rationale: Novice Java programmers often use code like:
38   * </p>
39   * <pre>
40   * if (x == "something")
41   * </pre>
42   * <p>
43   * when they mean
44   * </p>
45   * <pre>
46   * if ("something".equals(x))
47   * </pre>
48   * <p>
49   * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
50   * </p>
51   * <p>
52   * Violation Message Keys:
53   * </p>
54   * <ul>
55   * <li>
56   * {@code string.literal.equality}
57   * </li>
58   * </ul>
59   *
60   * @since 3.2
61   * @noinspection HtmlTagCanBeJavadocTag
62   * @noinspectionreason HtmlTagCanBeJavadocTag - encoded symbols were not decoded
63   *      when replaced with Javadoc tag
64   */
65  @StatelessCheck
66  public class StringLiteralEqualityCheck extends AbstractCheck {
67  
68      /**
69       * A key is pointing to the warning message text in "messages.properties"
70       * file.
71       */
72      public static final String MSG_KEY = "string.literal.equality";
73  
74      @Override
75      public int[] getDefaultTokens() {
76          return getRequiredTokens();
77      }
78  
79      @Override
80      public int[] getAcceptableTokens() {
81          return getRequiredTokens();
82      }
83  
84      @Override
85      public int[] getRequiredTokens() {
86          return new int[] {TokenTypes.EQUAL, TokenTypes.NOT_EQUAL};
87      }
88  
89      @Override
90      public void visitToken(DetailAST ast) {
91          final boolean hasStringLiteralChild = hasStringLiteralChild(ast);
92  
93          if (hasStringLiteralChild) {
94              log(ast, MSG_KEY, ast.getText());
95          }
96      }
97  
98      /**
99       * Checks whether string literal or text block literals are concatenated.
100      *
101      * @param ast ast
102      * @return {@code true} if string literal or text block literals are concatenated
103      */
104     private static boolean hasStringLiteralChild(DetailAST ast) {
105         DetailAST currentAst = ast;
106         boolean result = false;
107         while (currentAst != null) {
108             if (currentAst.findFirstToken(TokenTypes.STRING_LITERAL) == null
109                     && currentAst.findFirstToken(TokenTypes.TEXT_BLOCK_LITERAL_BEGIN) == null) {
110                 currentAst = currentAst.findFirstToken(TokenTypes.PLUS);
111             }
112             else {
113                 result = true;
114                 break;
115             }
116         }
117         return result;
118     }
119 
120 }