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.HashSet;
023import java.util.Set;
024
025import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
026import com.puppycrawl.tools.checkstyle.api.DetailAST;
027import com.puppycrawl.tools.checkstyle.api.FullIdent;
028import com.puppycrawl.tools.checkstyle.api.TokenTypes;
029import com.puppycrawl.tools.checkstyle.utils.CheckUtils;
030
031/**
032 * <p>Checks that if a class defines a covariant method equals,
033 * then it defines method equals(java.lang.Object).
034 * Inspired by findbugs,
035 * http://findbugs.sourceforge.net/bugDescriptions.html#EQ_SELF_NO_OBJECT
036 * </p>
037 * <p>
038 * An example of how to configure the check is:
039 * </p>
040 * <pre>
041 * &lt;module name="CovariantEquals"/&gt;
042 * </pre>
043 * @author Rick Giles
044 */
045public class CovariantEqualsCheck extends AbstractCheck {
046
047    /**
048     * A key is pointing to the warning message text in "messages.properties"
049     * file.
050     */
051    public static final String MSG_KEY = "covariant.equals";
052
053    /** Set of equals method definitions. */
054    private final Set<DetailAST> equalsMethods = new HashSet<>();
055
056    @Override
057    public int[] getDefaultTokens() {
058        return new int[] {TokenTypes.CLASS_DEF, TokenTypes.LITERAL_NEW, TokenTypes.ENUM_DEF, };
059    }
060
061    @Override
062    public int[] getRequiredTokens() {
063        return getDefaultTokens();
064    }
065
066    @Override
067    public int[] getAcceptableTokens() {
068        return new int[] {TokenTypes.CLASS_DEF, TokenTypes.LITERAL_NEW, TokenTypes.ENUM_DEF, };
069    }
070
071    @Override
072    public void visitToken(DetailAST ast) {
073        equalsMethods.clear();
074
075        // examine method definitions for equals methods
076        final DetailAST objBlock = ast.findFirstToken(TokenTypes.OBJBLOCK);
077        if (objBlock != null) {
078            DetailAST child = objBlock.getFirstChild();
079            boolean hasEqualsObject = false;
080            while (child != null) {
081                if (child.getType() == TokenTypes.METHOD_DEF
082                        && CheckUtils.isEqualsMethod(child)) {
083                    if (isFirstParameterObject(child)) {
084                        hasEqualsObject = true;
085                    }
086                    else {
087                        equalsMethods.add(child);
088                    }
089                }
090                child = child.getNextSibling();
091            }
092
093            // report equals method definitions
094            if (!hasEqualsObject) {
095                for (DetailAST equalsAST : equalsMethods) {
096                    final DetailAST nameNode = equalsAST
097                            .findFirstToken(TokenTypes.IDENT);
098                    log(nameNode.getLineNo(), nameNode.getColumnNo(),
099                            MSG_KEY);
100                }
101            }
102        }
103    }
104
105    /**
106     * Tests whether a method's first parameter is an Object.
107     * @param methodDefAst the method definition AST to test.
108     *     Precondition: ast is a TokenTypes.METHOD_DEF node.
109     * @return true if ast has first parameter of type Object.
110     */
111    private static boolean isFirstParameterObject(DetailAST methodDefAst) {
112        final DetailAST paramsNode = methodDefAst.findFirstToken(TokenTypes.PARAMETERS);
113
114        // parameter type "Object"?
115        final DetailAST paramNode =
116            paramsNode.findFirstToken(TokenTypes.PARAMETER_DEF);
117        final DetailAST typeNode = paramNode.findFirstToken(TokenTypes.TYPE);
118        final FullIdent fullIdent = FullIdent.createFullIdentBelow(typeNode);
119        final String name = fullIdent.getText();
120        return "Object".equals(name) || "java.lang.Object".equals(name);
121    }
122}