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