Skip to content

Commit

Permalink
ASTDiff: Bug fix for inter-files
Browse files Browse the repository at this point in the history
  • Loading branch information
pouryafard75 committed Dec 17, 2024
1 parent 7ef9e17 commit 7e10cbd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private void computeMovedDiffsEditScripts() {

private void makeASTDiff(List<? extends UMLAbstractClassDiff> umlClassBaseDiffList, boolean mergeFlag){
for (UMLAbstractClassDiff classDiff : umlClassBaseDiffList) {
Collection<ASTDiff> appends = findAppends(projectASTDiff.getDiffSet(), classDiff.getOriginalClass().getSourceFile(), classDiff.getNextClass().getSourceFile());
Collection<ASTDiff> appends = findAppends(projectASTDiff.getDiffSet(), classDiff.getOriginalClass().getSourceFile(), classDiff.getNextClass().getSourceFile(), false);
boolean decision = (!appends.isEmpty()) || mergeFlag;
ASTDiff classASTDiff = process(classDiff, findTreeContexts(modelDiff, classDiff), decision, appends);
if (!appends.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,21 +122,28 @@ else if (refactoring.getRefactoringType().equals(RefactoringType.MOVE_AND_INLINE
}

private void findDiffsAndApplyMatcher(String srcPath, String dstPath, OptimizationAwareMatcher matcher) {
Collection<ASTDiff> diffs = findAppends(diffSet, srcPath, dstPath);
Collection<ASTDiff> diffs = findAppends(diffSet, srcPath, dstPath, false);
if (diffs.isEmpty()) {
//This means that the there is no astDiff associated with the refactoring,
//It could be a case of move from a deleted file to an added file which neither of them have an equivalent file in the other side.
//In this case, we should create a new ASTDiff and apply the matcher on it.
Pair<TreeContext, TreeContext> treeContextPair = findTreeContexts(modelDiff, srcPath, dstPath);
Tree srcTree = treeContextPair.first.getRoot();
Tree dstTree = treeContextPair.second.getRoot();
ExtendedMultiMappingStore mappingStore = new ExtendedMultiMappingStore(srcTree, dstTree);
ASTDiff diff = new ASTDiff(srcPath, dstPath,
Collection<ASTDiff> appends = findAppends(newlyGeneratedDiffsOptimizationMap.keySet(), srcPath, dstPath, true);
if (appends.isEmpty()) {
Pair<TreeContext, TreeContext> treeContextPair = findTreeContexts(modelDiff, srcPath, dstPath);
Tree srcTree = treeContextPair.first.getRoot();
Tree dstTree = treeContextPair.second.getRoot();
ExtendedMultiMappingStore mappingStore = new ExtendedMultiMappingStore(srcTree, dstTree);
ASTDiff diff = new ASTDiff(srcPath, dstPath,
treeContextPair.first, treeContextPair.second,
mappingStore);
newlyGeneratedDiffsOptimizationMap.putIfAbsent(diff, new OptimizationData(new ArrayList<>(), new ExtendedMultiMappingStore(srcTree,dstTree)));
newlyGeneratedDiffsOptimizationMap.get(diff);
setOptimizationAndMatch(matcher, diff, newlyGeneratedDiffsOptimizationMap.get(diff), srcTree, dstTree);
newlyGeneratedDiffsOptimizationMap.putIfAbsent(diff, new OptimizationData(new ArrayList<>(), new ExtendedMultiMappingStore(srcTree,dstTree)));
setOptimizationAndMatch(matcher, diff, newlyGeneratedDiffsOptimizationMap.get(diff), srcTree, dstTree);
}
else {
for (ASTDiff append : appends) {
setOptimizationAndMatch(matcher, append, newlyGeneratedDiffsOptimizationMap.get(append), append.src.getRoot(), append.dst.getRoot());
}
}
}
for (ASTDiff diff : diffs) {
Tree srcTotalTree = modelDiff.getParentModel().getTreeContextMap().get(srcPath).getRoot();
Expand Down
18 changes: 13 additions & 5 deletions src/main/java/org/refactoringminer/astDiff/utils/Helpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,21 @@ public static Pair<Tree, Tree> findPairOfType(Tree srcOperationNode, Tree dstOpe
return null;
}

public static Collection<ASTDiff> findAppends(Collection<ASTDiff> diffs, String originalSourceFile, String nextSourceFile) {
public static Collection<ASTDiff> findAppends(Collection<ASTDiff> diffs, String originalSourceFile, String nextSourceFile, boolean both) {
Collection<ASTDiff> appends = new LinkedHashSet<>();
for (ASTDiff existing : diffs) {
if (existing.getSrcPath().equals(originalSourceFile))
appends.add(existing);
else if (existing.getDstPath().equals(nextSourceFile))
appends.add(existing);
if (both) {
if (existing.getSrcPath().equals(originalSourceFile) && existing.getDstPath().equals(nextSourceFile)) {
appends.add(existing);
break;
}
}
else {
if (existing.getSrcPath().equals(originalSourceFile))
appends.add(existing);
else if (existing.getDstPath().equals(nextSourceFile))
appends.add(existing);
}
}
return appends;
}
Expand Down

0 comments on commit 7e10cbd

Please sign in to comment.