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