Skip to content

Commit 40591ad

Browse files
gavinkingsebersole
authored andcommitted
add a fairly lame implementation of equals()/hashCode() checking
1 parent 054aeff commit 40591ad

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

gradle/java-module.gradle

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,12 +452,15 @@ task enforceRules {
452452
def illegalImport = ~/^import (sun|java.awt|org.slf4j)/
453453
def missingNewline = ~/^\s*}\s*(else|catch|finally)/
454454
def lowerEll = ~/\b\d+l\b/
455+
def equals = ~/boolean +equals\((@?\w+ )*Object \w+\)/
456+
def hashCode = ~/int +hashCode\(\)/
455457
def errors = 0
456458
def tree = fileTree("src/main/java/")
457459
tree.include "**/*.java"
458460
tree.each { file ->
459461
def lineNum = 0
460462
def shortName = file.path.substring(rootDir.path.length())
463+
def equalsMinusHashcode = 0
461464
file.eachLine { line ->
462465
lineNum++
463466
if (line =~ illegalImport) {
@@ -472,6 +475,22 @@ task enforceRules {
472475
errors++
473476
logger.error("Lowercase long literal in ${shortName}\n${lineNum}: ${line}")
474477
}
478+
if (!line.startsWith("//")) { //ignore commented-out code
479+
if (line =~ equals) {
480+
equalsMinusHashcode ++
481+
}
482+
if (line =~ hashCode) {
483+
equalsMinusHashcode --
484+
}
485+
}
486+
}
487+
if (equalsMinusHashcode>0) {
488+
errors++
489+
logger.error("Equals with missing hash code in ${shortName}")
490+
}
491+
if (equalsMinusHashcode<0) {
492+
errors++
493+
logger.error("Hash code with missing equals in ${shortName}")
475494
}
476495
}
477496
if ( errors>0 ) {

0 commit comments

Comments
 (0)