001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2019 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.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>Checks that if a class defines a covariant method equals, 034 * then it defines method equals(java.lang.Object). 035 * Inspired by findbugs, 036 * http://findbugs.sourceforge.net/bugDescriptions.html#EQ_SELF_NO_OBJECT 037 * </p> 038 * <p> 039 * An example of how to configure the check is: 040 * </p> 041 * <pre> 042 * <module name="CovariantEquals"/> 043 * </pre> 044 */ 045@FileStatefulCheck 046public class CovariantEqualsCheck extends AbstractCheck { 047 048 /** 049 * A key is pointing to the warning message text in "messages.properties" 050 * file. 051 */ 052 public static final String MSG_KEY = "covariant.equals"; 053 054 /** Set of equals method definitions. */ 055 private final Set<DetailAST> equalsMethods = new HashSet<>(); 056 057 @Override 058 public int[] getDefaultTokens() { 059 return getRequiredTokens(); 060 } 061 062 @Override 063 public int[] getRequiredTokens() { 064 return new int[] {TokenTypes.CLASS_DEF, TokenTypes.LITERAL_NEW, TokenTypes.ENUM_DEF, }; 065 } 066 067 @Override 068 public int[] getAcceptableTokens() { 069 return getRequiredTokens(); 070 } 071 072 @Override 073 public void visitToken(DetailAST ast) { 074 equalsMethods.clear(); 075 076 // examine method definitions for equals methods 077 final DetailAST objBlock = ast.findFirstToken(TokenTypes.OBJBLOCK); 078 if (objBlock != null) { 079 DetailAST child = objBlock.getFirstChild(); 080 boolean hasEqualsObject = false; 081 while (child != null) { 082 if (child.getType() == TokenTypes.METHOD_DEF 083 && CheckUtil.isEqualsMethod(child)) { 084 if (isFirstParameterObject(child)) { 085 hasEqualsObject = true; 086 } 087 else { 088 equalsMethods.add(child); 089 } 090 } 091 child = child.getNextSibling(); 092 } 093 094 // report equals method definitions 095 if (!hasEqualsObject) { 096 for (DetailAST equalsAST : equalsMethods) { 097 final DetailAST nameNode = equalsAST 098 .findFirstToken(TokenTypes.IDENT); 099 log(nameNode, 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 123}