From 40591ada20771784292381fd01f29a9589b22a6c Mon Sep 17 00:00:00 2001 From: Gavin King Date: Mon, 30 Sep 2024 18:11:20 +0200 Subject: [PATCH] add a fairly lame implementation of equals()/hashCode() checking --- gradle/java-module.gradle | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/gradle/java-module.gradle b/gradle/java-module.gradle index 44f48563f1..3cb648efaf 100644 --- a/gradle/java-module.gradle +++ b/gradle/java-module.gradle @@ -452,12 +452,15 @@ task enforceRules { def illegalImport = ~/^import (sun|java.awt|org.slf4j)/ def missingNewline = ~/^\s*}\s*(else|catch|finally)/ def lowerEll = ~/\b\d+l\b/ + def equals = ~/boolean +equals\((@?\w+ )*Object \w+\)/ + def hashCode = ~/int +hashCode\(\)/ def errors = 0 def tree = fileTree("src/main/java/") tree.include "**/*.java" tree.each { file -> def lineNum = 0 def shortName = file.path.substring(rootDir.path.length()) + def equalsMinusHashcode = 0 file.eachLine { line -> lineNum++ if (line =~ illegalImport) { @@ -472,6 +475,22 @@ task enforceRules { errors++ logger.error("Lowercase long literal in ${shortName}\n${lineNum}: ${line}") } + if (!line.startsWith("//")) { //ignore commented-out code + if (line =~ equals) { + equalsMinusHashcode ++ + } + if (line =~ hashCode) { + equalsMinusHashcode -- + } + } + } + if (equalsMinusHashcode>0) { + errors++ + logger.error("Equals with missing hash code in ${shortName}") + } + if (equalsMinusHashcode<0) { + errors++ + logger.error("Hash code with missing equals in ${shortName}") } } if ( errors>0 ) {