001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2014 Oliver Burn 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//////////////////////////////////////////////////////////////////////////////// 019package com.puppycrawl.tools.checkstyle.checks.coding; 020 021import com.google.common.collect.Sets; 022import com.puppycrawl.tools.checkstyle.api.Check; 023import com.puppycrawl.tools.checkstyle.api.DetailAST; 024import com.puppycrawl.tools.checkstyle.api.FullIdent; 025import com.puppycrawl.tools.checkstyle.api.TokenTypes; 026import com.puppycrawl.tools.checkstyle.checks.CheckUtils; 027import java.util.Set; 028 029/** 030 * <p>Checks that if a class defines a covariant method equals, 031 * then it defines method equals(java.lang.Object). 032 * Inspired by findbugs, 033 * http://findbugs.sourceforge.net/bugDescriptions.html#EQ_SELF_NO_OBJECT 034 * </p> 035 * <p> 036 * An example of how to configure the check is: 037 * </p> 038 * <pre> 039 * <module name="CovariantEquals"/> 040 * </pre> 041 * @author Rick Giles 042 * @version 1.0 043 */ 044public class CovariantEqualsCheck extends Check 045{ 046 /** Set of equals method definitions */ 047 private final Set<DetailAST> equalsMethods = Sets.newHashSet(); 048 049 @Override 050 public int[] getDefaultTokens() 051 { 052 return new int[] {TokenTypes.CLASS_DEF, TokenTypes.LITERAL_NEW, }; 053 } 054 055 @Override 056 public int[] getRequiredTokens() 057 { 058 return getDefaultTokens(); 059 } 060 061 @Override 062 public void visitToken(DetailAST ast) 063 { 064 equalsMethods.clear(); 065 boolean hasEqualsObject = false; 066 067 // examine method definitions for equals methods 068 final DetailAST objBlock = ast.findFirstToken(TokenTypes.OBJBLOCK); 069 if (objBlock != null) { 070 DetailAST child = objBlock.getFirstChild(); 071 while (child != null) { 072 if (child.getType() == TokenTypes.METHOD_DEF 073 && CheckUtils.isEqualsMethod(child)) 074 { 075 if (hasObjectParameter(child)) { 076 hasEqualsObject = true; 077 } 078 else { 079 equalsMethods.add(child); 080 } 081 } 082 child = child.getNextSibling(); 083 } 084 085 // report equals method definitions 086 if (!hasEqualsObject) { 087 for (DetailAST equalsAST : equalsMethods) { 088 final DetailAST nameNode = equalsAST 089 .findFirstToken(TokenTypes.IDENT); 090 log(nameNode.getLineNo(), nameNode.getColumnNo(), 091 "covariant.equals"); 092 } 093 } 094 } 095 } 096 097 /** 098 * Tests whether a method definition AST has exactly one 099 * parameter of type Object. 100 * @param ast the method definition AST to test. 101 * Precondition: ast is a TokenTypes.METHOD_DEF node. 102 * @return true if ast has exactly one parameter of type Object. 103 */ 104 private boolean hasObjectParameter(DetailAST ast) 105 { 106 // one parameter? 107 final DetailAST paramsNode = ast.findFirstToken(TokenTypes.PARAMETERS); 108 if (paramsNode.getChildCount() != 1) { 109 return false; 110 } 111 112 // parameter type "Object"? 113 final DetailAST paramNode = 114 paramsNode.findFirstToken(TokenTypes.PARAMETER_DEF); 115 final DetailAST typeNode = paramNode.findFirstToken(TokenTypes.TYPE); 116 final FullIdent fullIdent = FullIdent.createFullIdentBelow(typeNode); 117 final String name = fullIdent.getText(); 118 return ("Object".equals(name) || "java.lang.Object".equals(name)); 119 } 120}