Skip to content

Commit

Permalink
Improve optimize() for correctly detecting Rename Class in commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tsantalis committed Feb 2, 2025
1 parent 5bf8978 commit f138782
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
24 changes: 24 additions & 0 deletions src/main/java/gr/uom/java/xmi/diff/UMLModelDiff.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import gr.uom.java.xmi.decomposition.AbstractCodeMapping;
import gr.uom.java.xmi.decomposition.AbstractExpression;
import gr.uom.java.xmi.decomposition.AbstractStatement;
import gr.uom.java.xmi.decomposition.AnonymousClassDeclarationObject;
import gr.uom.java.xmi.decomposition.CompositeStatementObject;
import gr.uom.java.xmi.decomposition.CompositeStatementObjectMapping;
import gr.uom.java.xmi.decomposition.LeafExpression;
Expand Down Expand Up @@ -1134,6 +1135,7 @@ private TreeSet<UMLClassRenameDiff> findRenameMatchesForAddedClass(UMLClass adde
private TreeSet<UMLClassRenameDiff> optimize(TreeSet<UMLClassRenameDiff> diffSet) {
if(diffSet.size() > 1) {
TreeSet<UMLClassRenameDiff> identicalBodyDiffSet = new TreeSet<UMLClassRenameDiff>(new ClassRenameComparator());
TreeSet<UMLClassRenameDiff> identicalAnonymousDeclarationDiffSet = new TreeSet<UMLClassRenameDiff>(new ClassRenameComparator());
TreeSet<UMLClassRenameDiff> identicalStatementDiffSet = new TreeSet<UMLClassRenameDiff>(new ClassRenameComparator());
TreeSet<UMLClassRenameDiff> identicalSignatureDiffSet = new TreeSet<UMLClassRenameDiff>(new ClassRenameComparator());
TreeSet<UMLClassRenameDiff> identicalPackageDeclarationDocDiffSet = new TreeSet<UMLClassRenameDiff>(new ClassRenameComparator());
Expand All @@ -1157,6 +1159,7 @@ private TreeSet<UMLClassRenameDiff> optimize(TreeSet<UMLClassRenameDiff> diffSet
int identicalStatementSignatures = 0;
int totalStatements = 0;
int matchingStatements = 0;
int operationsWithIdenticalAnonymousDeclarations = 0;
if(operations1.size() == operations2.size()) {
for(int i=0; i<operations1.size(); i++) {
UMLOperation op1 = operations1.get(i);
Expand Down Expand Up @@ -1186,6 +1189,21 @@ else if(op1.getBody() != null && op2.getBody() != null) {
if(rep1.containsAll(rep2) || rep2.containsAll(rep1)) {
matchingStatements += Math.min(rep1.size(), rep2.size());
}
List<AnonymousClassDeclarationObject> anonymousList1 = op1.getBody().getAllAnonymousClassDeclarations();
List<AnonymousClassDeclarationObject> anonymousList2 = op2.getBody().getAllAnonymousClassDeclarations();
int identicalAnonymousDeclarations = 0;
if(anonymousList1.size() == anonymousList2.size() && anonymousList1.size() > 0) {
for(int j=0; j<anonymousList1.size(); j++) {
AnonymousClassDeclarationObject anonymous1 = anonymousList1.get(j);
AnonymousClassDeclarationObject anonymous2 = anonymousList2.get(j);
if(anonymous1.toString().equals(anonymous2.toString())) {
identicalAnonymousDeclarations++;
}
}
}
if(identicalAnonymousDeclarations == anonymousList1.size() && anonymousList1.size() > 0) {
operationsWithIdenticalAnonymousDeclarations++;
}
}
String actualSignature1 = op1.getActualSignature();
String actualSignature2 = op2.getActualSignature();
Expand Down Expand Up @@ -1222,6 +1240,9 @@ else if(op1.getBody() != null && op2.getBody() != null) {
if(identicalSignatures == operations1.size()) {
identicalSignatureDiffSet.add(diff);
}
if(operationsWithIdenticalAnonymousDeclarations == operations1.size()) {
identicalAnonymousDeclarationDiffSet.add(diff);
}
}
if(identicalBodyDiffSet.size() == 1) {
return identicalBodyDiffSet;
Expand All @@ -1235,6 +1256,9 @@ else if(identicalBodyDiffSet.size() < diffSet.size() && identicalStatementDiffSe
if(identicalPackageDeclarationDocDiffSet.size() == 1) {
return identicalPackageDeclarationDocDiffSet;
}
if(identicalAnonymousDeclarationDiffSet.size() == 1) {
return identicalAnonymousDeclarationDiffSet;
}
Map.Entry<Integer, TreeSet<UMLClassRenameDiff>> entry = matchingStatementMap.lastEntry();
if(entry != null && entry.getKey() > 0 && entry.getValue().size() == 1) {
return entry.getValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ public void testAllRefactorings() throws Exception {
.or(Refactorings.SplitMethod.getValue());
TestBuilder test = new TestBuilder(detector, REPOS, types);
RefactoringPopulator.feedTSERefactoringInstances(test);
test.assertExpectationsWithGitHubAPI(2965, 275, 412);
test.assertExpectationsWithGitHubAPI(2971, 274, 406);
}
}
10 changes: 8 additions & 2 deletions src/test/resources/oracle/tse-dataset/junit4.json
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@
{
"type": "RENAME_CLASS",
"description": "Rename Class\torg.junit.rules.ErrorCollectorTest.UsesErrorCollectorCheckThat renamed to org.junit.rules.ErrorCollectorTest.CheckTwoMatchersThatFail",
"validation": true
"validation": false
},
{
"type": "RENAME_CLASS",
Expand Down Expand Up @@ -695,7 +695,13 @@
"validation": true
}
],
"baseline": []
"baseline": [
{
"type": "RENAME_CLASS",
"description": "Rename Class\torg.junit.rules.ErrorCollectorTest.UsesErrorCollectorCheckThat renamed to org.junit.rules.ErrorCollectorTest.CheckMatcherThatFailsWithoutProvidedReason",
"validation": true
}
]
},
{
"repository": "https://github.com/junit-team/junit4.git",
Expand Down

0 comments on commit f138782

Please sign in to comment.