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 java.util.HashMap;
023import java.util.Map;
024
025import com.puppycrawl.tools.checkstyle.FileStatefulCheck;
026import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
027import com.puppycrawl.tools.checkstyle.api.DetailAST;
028import com.puppycrawl.tools.checkstyle.api.FullIdent;
029import com.puppycrawl.tools.checkstyle.api.TokenTypes;
030import com.puppycrawl.tools.checkstyle.utils.CheckUtil;
031
032/**
033 * <p>
034 * Checks that classes that either override {@code equals()} or {@code hashCode()} also
035 * overrides the other.
036 * This check only verifies that the method declarations match {@code Object.equals(Object)} and
037 * {@code Object.hashCode()} exactly to be considered an override. This check does not verify
038 * invalid method names, parameters other than {@code Object}, or anything else.
039 * </p>
040 * <p>
041 * Rationale: The contract of {@code equals()} and {@code hashCode()} requires that
042 * equal objects have the same hashCode. Therefore, whenever you override
043 * {@code equals()} you must override {@code hashCode()} to ensure that your class can
044 * be used in hash-based collections.
045 * </p>
046 * <p>
047 * To configure the check:
048 * </p>
049 * <pre>
050 * &lt;module name=&quot;EqualsHashCode&quot;/&gt;
051 * </pre>
052 * <p>Example:</p>
053 * <pre>
054 * public static class Example1 {
055 *     public int hashCode() {
056 *         // code
057 *     }
058 *     public boolean equals(String o) { // violation, overloaded implementation of 'equals'
059 *         // code
060 *     }
061 * }
062 * public static class Example2 {
063 *     public boolean equals(Object o) { // violation, no 'hashCode'
064 *         // code
065 *     }
066 *     public boolean equals(String o) {
067 *         // code
068 *     }
069 * }
070 * public static class Example3 {
071 *     public int hashCode() {
072 *         // code
073 *     }
074 *     public boolean equals(Object o) { // OK
075 *         // code
076 *     }
077 *     public boolean equals(String o) {
078 *         // code
079 *     }
080 * }
081 * public static class Example4 {
082 *     public int hashCode() {
083 *         // code
084 *     }
085 *     public boolean equals(java.lang.Object o) { // OK
086 *         // code
087 *    }
088 * }
089 * public static class Example5 {
090 *     public static int hashCode(int i) {
091 *         // code
092 *     }
093 *     public boolean equals(Object o) { // violation, overloaded implementation of 'hashCode'
094 *         // code
095 *     }
096 * }
097 * public static class Example6 {
098 *     public int hashCode() { // violation, overloaded implementation of 'equals'
099 *         // code
100 *     }
101 *     public static boolean equals(Object o, Object o2) {
102 *         // code
103 *     }
104 * }
105 * </pre>
106 *
107 * @since 3.0
108 */
109@FileStatefulCheck
110public class EqualsHashCodeCheck
111        extends AbstractCheck {
112
113    // implementation note: we have to use the following members to
114    // keep track of definitions in different inner classes
115
116    /**
117     * A key is pointing to the warning message text in "messages.properties"
118     * file.
119     */
120    public static final String MSG_KEY_HASHCODE = "equals.noHashCode";
121
122    /**
123     * A key is pointing to the warning message text in "messages.properties"
124     * file.
125     */
126    public static final String MSG_KEY_EQUALS = "equals.noEquals";
127
128    /** Maps OBJ_BLOCK to the method definition of equals(). */
129    private final Map<DetailAST, DetailAST> objBlockWithEquals = new HashMap<>();
130
131    /** Maps OBJ_BLOCKs to the method definition of hashCode(). */
132    private final Map<DetailAST, DetailAST> objBlockWithHashCode = new HashMap<>();
133
134    @Override
135    public int[] getDefaultTokens() {
136        return getRequiredTokens();
137    }
138
139    @Override
140    public int[] getAcceptableTokens() {
141        return getRequiredTokens();
142    }
143
144    @Override
145    public int[] getRequiredTokens() {
146        return new int[] {TokenTypes.METHOD_DEF};
147    }
148
149    @Override
150    public void beginTree(DetailAST rootAST) {
151        objBlockWithEquals.clear();
152        objBlockWithHashCode.clear();
153    }
154
155    @Override
156    public void visitToken(DetailAST ast) {
157        if (isEqualsMethod(ast)) {
158            objBlockWithEquals.put(ast.getParent(), ast);
159        }
160        else if (isHashCodeMethod(ast)) {
161            objBlockWithHashCode.put(ast.getParent(), ast);
162        }
163    }
164
165    /**
166     * Determines if an AST is a valid Equals method implementation.
167     *
168     * @param ast the AST to check
169     * @return true if the {code ast} is a Equals method.
170     */
171    private static boolean isEqualsMethod(DetailAST ast) {
172        final DetailAST modifiers = ast.getFirstChild();
173        final DetailAST parameters = ast.findFirstToken(TokenTypes.PARAMETERS);
174
175        return CheckUtil.isEqualsMethod(ast)
176                && isObjectParam(parameters.getFirstChild())
177                && (ast.findFirstToken(TokenTypes.SLIST) != null
178                        || modifiers.findFirstToken(TokenTypes.LITERAL_NATIVE) != null);
179    }
180
181    /**
182     * Determines if an AST is a valid HashCode method implementation.
183     *
184     * @param ast the AST to check
185     * @return true if the {code ast} is a HashCode method.
186     */
187    private static boolean isHashCodeMethod(DetailAST ast) {
188        final DetailAST modifiers = ast.getFirstChild();
189        final DetailAST methodName = ast.findFirstToken(TokenTypes.IDENT);
190        final DetailAST parameters = ast.findFirstToken(TokenTypes.PARAMETERS);
191
192        return "hashCode".equals(methodName.getText())
193                && parameters.getFirstChild() == null
194                && (ast.findFirstToken(TokenTypes.SLIST) != null
195                        || modifiers.findFirstToken(TokenTypes.LITERAL_NATIVE) != null);
196    }
197
198    /**
199     * Determines if an AST is a formal param of type Object.
200     *
201     * @param paramNode the AST to check
202     * @return true if firstChild is a parameter of an Object type.
203     */
204    private static boolean isObjectParam(DetailAST paramNode) {
205        final DetailAST typeNode = paramNode.findFirstToken(TokenTypes.TYPE);
206        final FullIdent fullIdent = FullIdent.createFullIdentBelow(typeNode);
207        final String name = fullIdent.getText();
208        return "Object".equals(name) || "java.lang.Object".equals(name);
209    }
210
211    @Override
212    public void finishTree(DetailAST rootAST) {
213        objBlockWithEquals
214            .entrySet().stream().filter(detailASTDetailASTEntry -> {
215                return objBlockWithHashCode.remove(detailASTDetailASTEntry.getKey()) == null;
216            }).forEach(detailASTDetailASTEntry -> {
217                final DetailAST equalsAST = detailASTDetailASTEntry.getValue();
218                log(equalsAST, MSG_KEY_HASHCODE);
219            });
220        objBlockWithHashCode.forEach((key, equalsAST) -> log(equalsAST, MSG_KEY_EQUALS));
221    }
222
223}