Skip to content

Commit

Permalink
Include enum constants when computing move/rename class candidates
Browse files Browse the repository at this point in the history
  • Loading branch information
tsantalis committed Jul 31, 2024
1 parent 4d37c55 commit d465813
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 3 deletions.
88 changes: 88 additions & 0 deletions src/main/java/gr/uom/java/xmi/UMLAbstractClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,10 @@ public UMLAttribute attributeWithTheSameNameIgnoringChangedType(UMLAttribute att
if(originalAttribute.equalsIgnoringChangedType(attribute))
return originalAttribute;
}
for(UMLAttribute originalAttribute : enumConstants) {
if(originalAttribute.equalsIgnoringChangedType(attribute))
return originalAttribute;
}
return null;
}

Expand All @@ -436,6 +440,10 @@ public UMLAttribute attributeWithTheSameSignature(UMLAttribute attribute) {
if(originalAttribute.equalsIgnoringChangedVisibility(attribute))
return originalAttribute;
}
for(UMLAttribute originalAttribute : enumConstants) {
if(originalAttribute.equalsIgnoringChangedVisibility(attribute))
return originalAttribute;
}
return null;
}

Expand All @@ -444,6 +452,10 @@ public boolean containsIdenticalAttributeIncludingAnnotation(UMLAttribute attrib
if(originalAttribute.identicalIncludingAnnotation(attribute))
return true;
}
for(UMLAttribute originalAttribute : enumConstants) {
if(originalAttribute.identicalIncludingAnnotation(attribute))
return true;
}
return false;
}

Expand All @@ -452,6 +464,10 @@ public boolean containsAttributeWithTheSameNameIgnoringChangedType(UMLAttribute
if(originalAttribute.equalsIgnoringChangedType(attribute))
return true;
}
for(UMLAttribute originalAttribute : enumConstants) {
if(originalAttribute.equalsIgnoringChangedType(attribute))
return true;
}
return false;
}

Expand All @@ -460,6 +476,10 @@ public boolean containsRenamedAttributeWithIdenticalTypeAndInitializer(UMLAttrib
if(originalAttribute.renamedWithIdenticalTypeAndInitializer(attribute))
return true;
}
for(UMLAttribute originalAttribute : enumConstants) {
if(originalAttribute.renamedWithIdenticalTypeAndInitializer(attribute))
return true;
}
return false;
}

Expand All @@ -468,6 +488,10 @@ public boolean containsAttributeWithTheSameName(UMLAttribute attribute) {
if(originalAttribute.getName().equals(attribute.getName()))
return true;
}
for(UMLAttribute originalAttribute : enumConstants) {
if(originalAttribute.getName().equals(attribute.getName()))
return true;
}
return false;
}

Expand All @@ -488,6 +512,20 @@ else if(attribute.getName().contains(pattern.getAfter()) && pattern.getBefore().
}
}
}
for(UMLAttribute originalAttribute : enumConstants) {
String originalAttributeName = originalAttribute.getName();
if(originalAttributeName.contains(pattern.getBefore()) && !pattern.getBefore().isEmpty()) {
String originalAttributeNameAfterReplacement = originalAttributeName.replace(pattern.getBefore(), pattern.getAfter());
if(originalAttributeNameAfterReplacement.equals(attribute.getName()))
return true;
}
else if(attribute.getName().contains(pattern.getAfter()) && pattern.getBefore().isEmpty()) {
String attributeNameAfterReplacement = attribute.getName().replace(pattern.getAfter(), "");
if(attributeNameAfterReplacement.equals(originalAttribute.getName())) {
return true;
}
}
}
return false;
}

Expand All @@ -496,6 +534,10 @@ public boolean containsAttributeWithName(String attributeName) {
if(originalAttribute.getName().equals(attributeName))
return true;
}
for(UMLAttribute originalAttribute : enumConstants) {
if(originalAttribute.getName().equals(attributeName))
return true;
}
return false;
}

Expand Down Expand Up @@ -532,6 +574,18 @@ public MatchResult hasAttributesAndOperationsWithCommonNames(UMLAbstractClass um
commonAttributes.add(attribute);
}
}
for(UMLAttribute attribute : enumConstants) {
totalAttributes++;
if(umlClass.containsAttributeWithTheSameName(attribute)) {
commonAttributes.add(attribute);
}
}
for(UMLAttribute attribute : umlClass.enumConstants) {
totalAttributes++;
if(this.containsAttributeWithTheSameName(attribute)) {
commonAttributes.add(attribute);
}
}
if(this.isTestClass() && umlClass.isTestClass()) {
if(commonOperations.size() > Math.floor(totalOperations/2.0) || commonOperations.containsAll(this.operations)) {
return new MatchResult(commonOperations.size(), commonAttributes.size(), totalOperations, totalAttributes, true);
Expand Down Expand Up @@ -680,6 +734,28 @@ else if(operation.isConstructor() && !commonPrefix.isEmpty() && !commonSuffix.is
}
}
}
for(UMLAttribute attribute : enumConstants) {
totalAttributes++;
if(umlClass.containsAttributeWithTheSameNameIgnoringChangedType(attribute) ||
umlClass.containsRenamedAttributeWithIdenticalTypeAndInitializer(attribute) ||
(pattern != null && umlClass.containsAttributeWithTheSameRenamePattern(attribute, pattern.reverse()))) {
commonAttributes.add(attribute);
if(umlClass.containsIdenticalAttributeIncludingAnnotation(attribute)) {
identicalAttributes.add(attribute);
}
}
}
for(UMLAttribute attribute : umlClass.enumConstants) {
totalAttributes++;
if(this.containsAttributeWithTheSameNameIgnoringChangedType(attribute) ||
this.containsRenamedAttributeWithIdenticalTypeAndInitializer(attribute) ||
(pattern != null && this.containsAttributeWithTheSameRenamePattern(attribute, pattern))) {
commonAttributes.add(attribute);
if(this.containsIdenticalAttributeIncludingAnnotation(attribute)) {
identicalAttributes.add(attribute);
}
}
}
if(this.isTestClass() && umlClass.isTestClass()) {
if(commonOperations.size() > Math.floor(totalOperations/2.0) || commonOperations.containsAll(this.operations)) {
return new MatchResult(commonOperations.size(), commonAttributes.size(), totalOperations, totalAttributes, true);
Expand Down Expand Up @@ -790,6 +866,18 @@ public MatchResult hasSameAttributesAndOperations(UMLAbstractClass umlClass) {
commonAttributes.add(attribute);
}
}
for(UMLAttribute attribute : enumConstants) {
totalAttributes++;
if(umlClass.containsAttributeWithTheSameNameIgnoringChangedType(attribute)) {
commonAttributes.add(attribute);
}
}
for(UMLAttribute attribute : umlClass.enumConstants) {
totalAttributes++;
if(this.containsAttributeWithTheSameNameIgnoringChangedType(attribute)) {
commonAttributes.add(attribute);
}
}
UMLAnnotationListDiff annotationListDiff = new UMLAnnotationListDiff(this.getAnnotations(), umlClass.getAnnotations());
boolean identicalAnnotations = this.getAnnotations().size() > 0 && umlClass.getAnnotations().size() > 0 && annotationListDiff.isEmpty();
if(totalOperations == 0 && totalAttributes == 0 && (this.getEnumConstants().size() == 0 || umlClass.getEnumConstants().size() == 0) && !this.getNonQualifiedName().equals(umlClass.getNonQualifiedName()) && !identicalAnnotations && !identicalComments(umlClass)) {
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/gr/uom/java/xmi/UMLAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,10 @@ public List<UMLAnnotation> getAnnotations() {
public boolean identicalIncludingAnnotation(UMLAttribute attribute) {
AbstractExpression thisInitializer = this.getVariableDeclaration().getInitializer();
AbstractExpression otherInitializer = attribute.getVariableDeclaration().getInitializer();
boolean equal = this.name.equals(attribute.name) && this.type.equals(attribute.type) && this.type.equalsQualified(attribute.type) &&
//ignore type if it is enum constant
boolean equalType = this instanceof UMLEnumConstant && attribute instanceof UMLEnumConstant ? true : this.type.equals(attribute.type);
boolean equalQualifiedType = this instanceof UMLEnumConstant && attribute instanceof UMLEnumConstant ? true : this.type.equalsQualified(attribute.type);
boolean equal = this.name.equals(attribute.name) && equalType && equalQualifiedType &&
this.visibility.equals(attribute.visibility) && this.getAnnotations().equals(attribute.getAnnotations());
if(thisInitializer != null && otherInitializer != null) {
return equal && thisInitializer.getExpression().equals(otherInitializer.getExpression());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public void testAllRefactorings() throws Exception {
GitHistoryRefactoringMinerImpl detector = new GitHistoryRefactoringMinerImpl();
TestBuilder test = new TestBuilder(detector, REPOS, Refactorings.All.getValue());
RefactoringPopulator.feedRefactoringsInstances(Refactorings.All.getValue(), Systems.FSE.getValue(), test);
test.assertExpectationsWithGitHubAPI(12264, 20, 236);
test.assertExpectationsWithGitHubAPI(12265, 20, 236);
}
}
7 changes: 7 additions & 0 deletions src/test/resources/oracle/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -52527,6 +52527,13 @@
"validation": "TP",
"detectionTools": "RefactoringMiner",
"validators": null
}, {
"type": "Move Class",
"description": "Move Class com.facebook.scrumptious.picker.GraphObjectAdapter.SectionAndItem.Type moved to com.example.scrumptious.picker.GraphObjectAdapter.SectionAndItem.Type",
"comment": null,
"validation": "TP",
"detectionTools": "RefactoringMiner",
"validators": null
}],
"refDiffExecutionTime": 2914
}, {
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/oracle/expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ b2b4085348de32f10903970dded99fdf0376a43c, 59, 0, 1
c753d2e41ba667f9b5a31451a16ecbaecdc65d80, 190, 0, 0
01cddc5afb590b4d36cb784637a8ea8aa31d3561, 5, 0, 0
8bdc57ba8975d851fe91edc908761aacea624766, 4, 0, 0
e813a0be86c87366157a0201e6c61662cadee586, 43, 0, 0
e813a0be86c87366157a0201e6c61662cadee586, 44, 0, 0
4983f83d1bedb7b737fc56d409c1c06b04e34e4e, 7, 0, 0
9a581e07cb6381d70f3fd9bb2055e810e2a682a9, 3, 0, 3
cb49e436b9d7ee55f2531ebc2ef1863f5c9ba9fe, 1, 0, 0
Expand Down

0 comments on commit d465813

Please sign in to comment.