add a fairly lame implementation of equals()/hashCode() checking

This commit is contained in:
Gavin King 2024-09-30 18:11:20 +02:00 committed by Steve Ebersole
parent 054aeff78b
commit 40591ada20
1 changed files with 19 additions and 0 deletions

View File

@ -452,12 +452,15 @@ task enforceRules {
def illegalImport = ~/^import (sun|java.awt|org.slf4j)/ def illegalImport = ~/^import (sun|java.awt|org.slf4j)/
def missingNewline = ~/^\s*}\s*(else|catch|finally)/ def missingNewline = ~/^\s*}\s*(else|catch|finally)/
def lowerEll = ~/\b\d+l\b/ def lowerEll = ~/\b\d+l\b/
def equals = ~/boolean +equals\((@?\w+ )*Object \w+\)/
def hashCode = ~/int +hashCode\(\)/
def errors = 0 def errors = 0
def tree = fileTree("src/main/java/") def tree = fileTree("src/main/java/")
tree.include "**/*.java" tree.include "**/*.java"
tree.each { file -> tree.each { file ->
def lineNum = 0 def lineNum = 0
def shortName = file.path.substring(rootDir.path.length()) def shortName = file.path.substring(rootDir.path.length())
def equalsMinusHashcode = 0
file.eachLine { line -> file.eachLine { line ->
lineNum++ lineNum++
if (line =~ illegalImport) { if (line =~ illegalImport) {
@ -472,6 +475,22 @@ task enforceRules {
errors++ errors++
logger.error("Lowercase long literal in ${shortName}\n${lineNum}: ${line}") 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 ) { if ( errors>0 ) {