001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2020 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 * <p>
029 * Checks that string literals are not used with <code>==</code> or <code>&#33;=</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="http://www.thejavageek.com/2013/07/27/string-comparison-with-equals-and-assignment-operator/">
034 * in this article</a>.
035 * </p>
036 * <p>
037 * Rationale: Novice Java programmers often use code like:
038 * </p>
039 * <pre>
040 * if (x == "something")
041 * </pre>
042 * <p>
043 * when they mean
044 * </p>
045 * <pre>
046 * if ("something".equals(x))
047 * </pre>
048 * <p>
049 * To configure the check:
050 * </p>
051 * <pre>
052 * &lt;module name=&quot;StringLiteralEquality&quot;/&gt;
053 * </pre>
054 * <p>
055 * Examples of violations:
056 * </p>
057 * <pre>
058 * String status = "pending";
059 *
060 * if (status == "done") {} // violation
061 *
062 * while (status != "done") {} // violation
063 *
064 * boolean flag = (status == "done"); // violation
065 *
066 * boolean flag = (status.equals("done")); // OK
067 *
068 * String name = "X";
069 *
070 * if (name == getName()) {}
071 * // OK, limitation that check cannot tell runtime type returned from method call
072 * </pre>
073 * <p>
074 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
075 * </p>
076 * <p>
077 * Violation Message Keys:
078 * </p>
079 * <ul>
080 * <li>
081 * {@code string.literal.equality}
082 * </li>
083 * </ul>
084 *
085 * @since 3.2
086 * @noinspection HtmlTagCanBeJavadocTag
087 */
088@StatelessCheck
089public class StringLiteralEqualityCheck extends AbstractCheck {
090
091    /**
092     * A key is pointing to the warning message text in "messages.properties"
093     * file.
094     */
095    public static final String MSG_KEY = "string.literal.equality";
096
097    @Override
098    public int[] getDefaultTokens() {
099        return getRequiredTokens();
100    }
101
102    @Override
103    public int[] getAcceptableTokens() {
104        return getRequiredTokens();
105    }
106
107    @Override
108    public int[] getRequiredTokens() {
109        return new int[] {TokenTypes.EQUAL, TokenTypes.NOT_EQUAL};
110    }
111
112    @Override
113    public void visitToken(DetailAST ast) {
114        // no need to check for nulls here, == and != always have two children
115        final DetailAST firstChild = ast.getFirstChild();
116        final DetailAST secondChild = firstChild.getNextSibling();
117
118        if (firstChild.getType() == TokenTypes.STRING_LITERAL
119                || secondChild.getType() == TokenTypes.STRING_LITERAL) {
120            log(ast, MSG_KEY, ast.getText());
121        }
122    }
123
124}