-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(spoon): Add EqualsHashcode badsmell (#899)
- Loading branch information
1 parent
f6de608
commit 83ae327
Showing
5 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...in/java/io/github/martinwitt/spoon_analyzer/badsmells/equals_hashcode/EqualsHashcode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package io.github.martinwitt.spoon_analyzer.badsmells.equals_hashcode; | ||
|
||
import io.github.martinwitt.spoon_analyzer.BadSmell; | ||
import io.github.martinwitt.spoon_analyzer.BadSmellVisitor; | ||
import spoon.reflect.declaration.CtType; | ||
|
||
/** | ||
* This badsmell means the class does not override equals() and hashcode() methods together. | ||
* Overriding only one of them is not enough and can lead to unexpected behavior. | ||
*/ | ||
public class EqualsHashcode implements BadSmell { | ||
|
||
private CtType<?> affectedType; | ||
|
||
public EqualsHashcode(CtType<?> affectedType) { | ||
this.affectedType = affectedType; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "EqualsHashcode"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "This class does not override equals() and hashcode() methods together."; | ||
} | ||
|
||
@Override | ||
public CtType<?> getAffectedType() { | ||
return affectedType; | ||
} | ||
|
||
@Override | ||
public <T> T accept(BadSmellVisitor<T> visitor) { | ||
// TODO Auto-generated method stub | ||
throw new UnsupportedOperationException("Unimplemented method 'accept'"); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...io/github/martinwitt/spoon_analyzer/badsmells/equals_hashcode/EqualsHashcodeAnalyzer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package io.github.martinwitt.spoon_analyzer.badsmells.equals_hashcode; | ||
|
||
import io.github.martinwitt.spoon_analyzer.BadSmell; | ||
import io.github.martinwitt.spoon_analyzer.LocalAnalyzer; | ||
import java.util.List; | ||
import spoon.reflect.declaration.CtMethod; | ||
import spoon.reflect.declaration.CtType; | ||
|
||
public class EqualsHashcodeAnalyzer implements LocalAnalyzer { | ||
|
||
@Override | ||
public List<BadSmell> analyze(CtType<?> clazz) { | ||
|
||
boolean hasEquals = false; | ||
boolean hasHashcode = false; | ||
for (CtMethod<?> methods : clazz.getMethods()) { | ||
if (isEqualsMethod(methods)) { | ||
hasEquals = true; | ||
} | ||
if (isHashcodeMethod(methods)) { | ||
hasHashcode = true; | ||
} | ||
} | ||
// if only one of them is present, it is a badsmell, otherwise not | ||
if ((!hasEquals && hasHashcode) || (hasEquals && !hasHashcode)) { | ||
return List.of(new EqualsHashcode(clazz)); | ||
} | ||
return List.of(); | ||
} | ||
|
||
private boolean isEqualsMethod(CtMethod<?> method) { | ||
return method.getSimpleName().equals("equals"); | ||
} | ||
|
||
private boolean isHashcodeMethod(CtMethod<?> method) { | ||
return method.getSimpleName().equals("hashCode"); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...ithub/martinwitt/spoon_analyzer/badsmells/equals_hashcode/EqualsHashcodeAnalyzerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package io.github.martinwitt.spoon_analyzer.badsmells.equals_hashcode; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import spoon.Launcher; | ||
import spoon.reflect.declaration.CtType; | ||
import spoon.support.compiler.VirtualFile; | ||
|
||
public class EqualsHashcodeAnalyzerTest { | ||
|
||
@Test | ||
void noEqualsNoHashcode() { | ||
String code = "public class A { }"; | ||
|
||
Launcher launcher = new Launcher(); | ||
launcher.addInputResource(new VirtualFile(code)); | ||
var model = launcher.buildModel(); | ||
EqualsHashcodeAnalyzer analyzer = new EqualsHashcodeAnalyzer(); | ||
CtType<?> simpleClass = model.getAllTypes().stream().findFirst().get(); | ||
var result = analyzer.analyze(simpleClass); | ||
assertEquals(0, result.size()); | ||
} | ||
|
||
@Test | ||
void UpperClassEqualsNoHashcode() { | ||
String code = | ||
""" | ||
class A { | ||
@Override | ||
public boolean equals(Object obj) { | ||
return super.equals(obj); | ||
} | ||
} | ||
class B extends A { | ||
} | ||
"""; | ||
|
||
Launcher launcher = new Launcher(); | ||
launcher.addInputResource(new VirtualFile(code)); | ||
var model = launcher.buildModel(); | ||
EqualsHashcodeAnalyzer analyzer = new EqualsHashcodeAnalyzer(); | ||
CtType<?> simpleClass = model.getAllTypes().stream().findFirst().get(); | ||
var result = analyzer.analyze(simpleClass); | ||
|
||
assertEquals(1, result.size()); | ||
} | ||
} |