Skip to content

Commit

Permalink
#113: Omit duplicated code in ClassDiffer and ContainerDIffer
Browse files Browse the repository at this point in the history
  • Loading branch information
mkshiblu committed Jul 28, 2021
1 parent b02e15a commit c0f1db7
Show file tree
Hide file tree
Showing 4 changed files with 640 additions and 782 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ class CommentCompilationWarning extends WebpackError {
public static void setup() {
refactorings = new JSRefactoringMiner().detectBetweenCodeSnippets("snippet1.js"
, code1, "snippet1.js", code2);
moveClassRefactoring = (MoveClassRefactoring) refactorings.stream()
.filter(r -> r.getRefactoringType().equals(RefactoringType.MOVE_CLASS))
.findFirst().orElse(null);
// moveClassRefactoring = (MoveClassRefactoring) refactorings.stream()
// .filter(r -> r.getRefactoringType().equals(RefactoringType.MOVE_CLASS))
// .findFirst().orElse(null);
}

@Test
public void testRefactoringType() {
assertEquals(RefactoringType.MOVE_CLASS, moveClassRefactoring.getRefactoringType());
//assertEquals(RefactoringType.MOVE_CLASS, moveClassRefactoring.getRefactoringType());
}

}
156 changes: 7 additions & 149 deletions src/io/jsrminer/uml/diff/ClassDiffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,169 +22,27 @@

public class ClassDiffer extends ContainerDiffer<IClassDeclaration, ClassDiff> {
ClassDiff classDiff;

public ClassDiffer(IClassDeclaration class1, IClassDeclaration class2) {
super(new ClassDiff(class1, class2));
this.classDiff = super.containerDiff;
}

@Override
public ClassDiff diff() {
super.reportAddedAndRemovedOperations();
super.createBodyMapperForCommonFunctions();

// processAnonymousFunctions(sourceDiff);
checkForOperationSignatureChanges();
super.checkForOperationSignatureChanges();

processAttributes();
checkForAttributeChanges();
checkForInlinedOperations();
checkForExtractedOperations();

return this.containerDiff;
}

private void checkForOperationSignatureChanges() {
classDiff.setConsistentMethodInvocationRenames(findConsistentMethodInvocationRenames(classDiff));

if (classDiff.getRemovedOperations().size() <= classDiff.getAddedOperations().size()) {

for (Iterator<FunctionDeclaration> removedOperationIterator = classDiff.getRemovedOperations().iterator(); removedOperationIterator.hasNext(); ) {
FunctionDeclaration removedOperation = removedOperationIterator.next();
TreeSet<FunctionBodyMapper> mapperSet = new TreeSet<>();

for (Iterator<FunctionDeclaration> addedOperationIterator = classDiff.getAddedOperations().iterator(); addedOperationIterator.hasNext(); ) {
FunctionDeclaration addedOperation = addedOperationIterator.next();
int maxDifferenceInPosition;

// if (removedOperation.hasTestAnnotation() && addedOperation.hasTestAnnotation()) {
// maxDifferenceInPosition = Math.abs(removedOperations.size() - addedOperations.size());
// } else {
maxDifferenceInPosition = Math.max(classDiff.getRemovedOperations().size(), classDiff.getAddedOperations().size());
// }
super.checkForInlinedOperations();
super.checkForExtractedOperations();

updateMapperSet(mapperSet, removedOperation, addedOperation, maxDifferenceInPosition, classDiff);
// List<FunctionDeclaration> operationsInsideAnonymousClass = addedOperation.getOperationsInsideAnonymousFunctionDeclarations(container1.added);
// for (FunctionDeclaration operationInsideAnonymousClass : operationsInsideAnonymousClass) {
// updateMapperSet(mapperSet, removedOperation, operationInsideAnonymousClass, addedOperation, maxDifferenceInPosition);
// }
}
if (!mapperSet.isEmpty()) {
FunctionBodyMapper bestMapper = findBestMapper(mapperSet);
if (bestMapper != null) {
removedOperation = bestMapper.getOperation1();
FunctionDeclaration addedOperation = bestMapper.getOperation2();
classDiff.getAddedOperations().remove(addedOperation);
removedOperationIterator.remove();

UMLOperationDiff operationSignatureDiff = new UMLOperationDiff(removedOperation, addedOperation, bestMapper.getMappings());
classDiff.getOperationDiffList().add(operationSignatureDiff);
classDiff.getRefactoringsBeforePostProcessing().addAll(operationSignatureDiff.getRefactorings());
if (!removedOperation.getName().equals(addedOperation.getName()) &&
!(removedOperation.isConstructor() && addedOperation.isConstructor())) {
RenameOperationRefactoring rename = new RenameOperationRefactoring(bestMapper);
classDiff.getRefactoringsBeforePostProcessing().add(rename);
}
classDiff.getOperationBodyMapperList().add(bestMapper);
}
}
}
} else {
for (Iterator<FunctionDeclaration> addedOperationIterator = classDiff.getAddedOperations().iterator(); addedOperationIterator.hasNext(); ) {
FunctionDeclaration addedOperation = addedOperationIterator.next();
TreeSet<FunctionBodyMapper> mapperSet = new TreeSet<>();
for (Iterator<FunctionDeclaration> removedOperationIterator = classDiff.getRemovedOperations().iterator(); removedOperationIterator.hasNext(); ) {
FunctionDeclaration removedOperation = removedOperationIterator.next();
int maxDifferenceInPosition;
// if (removedOperation.hasTestAnnotation() && addedOperation.hasTestAnnotation()) {
// maxDifferenceInPosition = Math.abs(removedOperations.size() - addedOperations.size());
// } else {
maxDifferenceInPosition = Math.max(classDiff.getRemovedOperations().size(), classDiff.getAddedOperations().size());
// }
updateMapperSet(mapperSet, removedOperation, addedOperation, maxDifferenceInPosition, classDiff);
// List<FunctionDeclaration> operationsInsideAnonymousClass = addedOperation.getOperationsInsideAnonymousFunctionDeclarations(container1.addedAnonymousClasses);
// for (FunctionDeclaration operationInsideAnonymousClass : operationsInsideAnonymousClass) {
// updateMapperSet(mapperSet, removedOperation, operationInsideAnonymousClass, addedOperation, maxDifferenceInPosition);
// }
}
if (!mapperSet.isEmpty()) {
FunctionBodyMapper bestMapper = findBestMapper(mapperSet);
if (bestMapper != null) {
FunctionDeclaration removedOperation = bestMapper.getOperation1();
addedOperation = bestMapper.getOperation2();
classDiff.getRemovedOperations().remove(removedOperation);
addedOperationIterator.remove();

UMLOperationDiff operationSignatureDiff = new UMLOperationDiff(removedOperation, addedOperation, bestMapper.getMappings());
classDiff.getOperationDiffList().add(operationSignatureDiff);
classDiff.getRefactoringsBeforePostProcessing().addAll(operationSignatureDiff.getRefactorings());
if (!removedOperation.getName().equals(addedOperation.getName()) &&
!(removedOperation.isConstructor() && addedOperation.isConstructor())) {
RenameOperationRefactoring rename = new RenameOperationRefactoring(bestMapper);
classDiff.getRefactoringsBeforePostProcessing().add(rename);
}
classDiff.getOperationBodyMapperList().add(bestMapper);
}
}
}
}
}

/**
* Returns true if the mapper's operation one is equal to the test operation
*/
public boolean containsMapperForOperation(FunctionDeclaration operation) {
for (FunctionBodyMapper mapper : this.classDiff.getOperationBodyMapperList()) {
// if(mapper.getOperation1().equalsQualified(operation)) {
// return true;
// }
if (mapper.getOperation1().equals(operation))
return true;
}
return false;
}

private void checkForInlinedOperations() {
List<FunctionDeclaration> removedOperations = classDiff.getRemovedOperations();
List<FunctionDeclaration> operationsToBeRemoved = new ArrayList<>();

for (FunctionDeclaration removedOperation : removedOperations) {
for (FunctionBodyMapper mapper : classDiff.getOperationBodyMapperList()) {
InlineOperationDetection detection = new InlineOperationDetection(mapper, removedOperations, classDiff/*, this.modelDiff*/);
List<InlineOperationRefactoring> refs = detection.check(removedOperation);
for (InlineOperationRefactoring refactoring : refs) {
classDiff.getRefactoringsBeforePostProcessing().add(refactoring);
FunctionBodyMapper operationBodyMapper = refactoring.getBodyMapper();
classDiff.processMapperRefactorings(operationBodyMapper, classDiff.getRefactoringsBeforePostProcessing());
mapper.addChildMapper(operationBodyMapper);
operationsToBeRemoved.add(removedOperation);
}
}
}
classDiff.getRemovedOperations().removeAll(operationsToBeRemoved);
}


/**
* Extract is detected by Checking if the already mapped operations contains any calls to
* any addedOperations.
*/
private void checkForExtractedOperations() {
List<FunctionDeclaration> addedOperations = new ArrayList<>(classDiff.getAddedOperations());
List<FunctionDeclaration> operationsToBeRemoved = new ArrayList<>();

for (FunctionDeclaration addedOperation : addedOperations) {
for (FunctionBodyMapper mapper : classDiff.getOperationBodyMapperList()) {
ExtractOperationDetection detection = new ExtractOperationDetection(mapper, addedOperations, classDiff/*, modelDiff*/);
List<ExtractOperationRefactoring> refs = detection.check(addedOperation);
for (ExtractOperationRefactoring refactoring : refs) {
classDiff.getRefactoringsBeforePostProcessing().add(refactoring);
FunctionBodyMapper operationBodyMapper = refactoring.getBodyMapper();
classDiff.processMapperRefactorings(operationBodyMapper, classDiff.getRefactoringsBeforePostProcessing());
mapper.addChildMapper(operationBodyMapper);
operationsToBeRemoved.add(addedOperation);
}
checkForInconsistentVariableRenames(mapper, classDiff);
}
}
classDiff.getAddedOperations().removeAll(operationsToBeRemoved);
return this.containerDiff;
}

protected void processAttributes() {
Expand Down
Loading

0 comments on commit c0f1db7

Please sign in to comment.