001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2017 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 antlr.collections.AST;
026import com.puppycrawl.tools.checkstyle.FileStatefulCheck;
027import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
028import com.puppycrawl.tools.checkstyle.api.DetailAST;
029import com.puppycrawl.tools.checkstyle.api.FullIdent;
030import com.puppycrawl.tools.checkstyle.api.TokenTypes;
031import com.puppycrawl.tools.checkstyle.utils.CheckUtils;
032
033/**
034 * <p>
035 * Checks that classes that either override {@code equals()} or {@code hashCode()} also
036 * overrides the other.
037 * This checks only verifies that the method declarations match {@link Object#equals(Object)} and
038 * {@link Object#hashCode()} exactly to be considered an override. This check does not verify
039 * invalid method names, parameters other than {@code Object}, or anything else.
040 * </p>
041 * <p>
042 * Rationale: The contract of equals() and hashCode() requires that
043 * equal objects have the same hashCode. Hence, whenever you override
044 * equals() you must override hashCode() to ensure that your class can
045 * be used in collections that are hash based.
046 * </p>
047 * <p>
048 * An example of how to configure the check is:
049 * </p>
050 * <pre>
051 * &lt;module name="EqualsHashCode"/&gt;
052 * </pre>
053 * @author lkuehne
054 */
055@FileStatefulCheck
056public class EqualsHashCodeCheck
057        extends AbstractCheck {
058    // implementation note: we have to use the following members to
059    // keep track of definitions in different inner classes
060
061    /**
062     * A key is pointing to the warning message text in "messages.properties"
063     * file.
064     */
065    public static final String MSG_KEY_HASHCODE = "equals.noHashCode";
066
067    /**
068     * A key is pointing to the warning message text in "messages.properties"
069     * file.
070     */
071    public static final String MSG_KEY_EQUALS = "equals.noEquals";
072
073    /** Maps OBJ_BLOCK to the method definition of equals(). */
074    private final Map<DetailAST, DetailAST> objBlockWithEquals = new HashMap<>();
075
076    /** Maps OBJ_BLOCKs to the method definition of hashCode(). */
077    private final Map<DetailAST, DetailAST> objBlockWithHashCode = new HashMap<>();
078
079    @Override
080    public int[] getDefaultTokens() {
081        return getRequiredTokens();
082    }
083
084    @Override
085    public int[] getAcceptableTokens() {
086        return getRequiredTokens();
087    }
088
089    @Override
090    public int[] getRequiredTokens() {
091        return new int[] {TokenTypes.METHOD_DEF};
092    }
093
094    @Override
095    public void beginTree(DetailAST rootAST) {
096        objBlockWithEquals.clear();
097        objBlockWithHashCode.clear();
098    }
099
100    @Override
101    public void visitToken(DetailAST ast) {
102        if (isEqualsMethod(ast)) {
103            objBlockWithEquals.put(ast.getParent(), ast);
104        }
105        else if (isHashCodeMethod(ast)) {
106            objBlockWithHashCode.put(ast.getParent(), ast);
107        }
108    }
109
110    /**
111     * Determines if an AST is a valid Equals method implementation.
112     *
113     * @param ast the AST to check
114     * @return true if the {code ast} is a Equals method.
115     */
116    private static boolean isEqualsMethod(DetailAST ast) {
117        final DetailAST modifiers = ast.getFirstChild();
118        final DetailAST parameters = ast.findFirstToken(TokenTypes.PARAMETERS);
119
120        return CheckUtils.isEqualsMethod(ast)
121                && modifiers.findFirstToken(TokenTypes.LITERAL_PUBLIC) != null
122                && isObjectParam(parameters.getFirstChild())
123                && (ast.findFirstToken(TokenTypes.SLIST) != null
124                        || modifiers.findFirstToken(TokenTypes.LITERAL_NATIVE) != null);
125    }
126
127    /**
128     * Determines if an AST is a valid HashCode method implementation.
129     *
130     * @param ast the AST to check
131     * @return true if the {code ast} is a HashCode method.
132     */
133    private static boolean isHashCodeMethod(DetailAST ast) {
134        final DetailAST modifiers = ast.getFirstChild();
135        final AST type = ast.findFirstToken(TokenTypes.TYPE);
136        final AST methodName = ast.findFirstToken(TokenTypes.IDENT);
137        final DetailAST parameters = ast.findFirstToken(TokenTypes.PARAMETERS);
138
139        return type.getFirstChild().getType() == TokenTypes.LITERAL_INT
140                && "hashCode".equals(methodName.getText())
141                && modifiers.findFirstToken(TokenTypes.LITERAL_PUBLIC) != null
142                && modifiers.findFirstToken(TokenTypes.LITERAL_STATIC) == null
143                && parameters.getFirstChild() == null
144                && (ast.findFirstToken(TokenTypes.SLIST) != null
145                        || modifiers.findFirstToken(TokenTypes.LITERAL_NATIVE) != null);
146    }
147
148    /**
149     * Determines if an AST is a formal param of type Object.
150     * @param paramNode the AST to check
151     * @return true if firstChild is a parameter of an Object type.
152     */
153    private static boolean isObjectParam(DetailAST paramNode) {
154        final DetailAST typeNode = paramNode.findFirstToken(TokenTypes.TYPE);
155        final FullIdent fullIdent = FullIdent.createFullIdentBelow(typeNode);
156        final String name = fullIdent.getText();
157        return "Object".equals(name) || "java.lang.Object".equals(name);
158    }
159
160    @Override
161    public void finishTree(DetailAST rootAST) {
162        objBlockWithEquals
163            .entrySet().stream().filter(detailASTDetailASTEntry -> {
164                return objBlockWithHashCode.remove(detailASTDetailASTEntry.getKey()) == null;
165            }).forEach(detailASTDetailASTEntry -> {
166                final DetailAST equalsAST = detailASTDetailASTEntry.getValue();
167                log(equalsAST.getLineNo(), equalsAST.getColumnNo(), MSG_KEY_HASHCODE);
168            });
169        objBlockWithHashCode.forEach((key, equalsAST) -> {
170            log(equalsAST.getLineNo(), equalsAST.getColumnNo(), MSG_KEY_EQUALS);
171        });
172    }
173}