From 7e10cbd0320ba0cafd4b617755790d076d7cd810 Mon Sep 17 00:00:00 2001 From: Pouryafard75 Date: Tue, 17 Dec 2024 15:07:00 -0500 Subject: [PATCH] ASTDiff: Bug fix for inter-files (https://github.com/tsantalis/RefactoringMiner/issues/828) --- .../astDiff/matchers/ProjectASTDiffer.java | 2 +- .../UnifiedModelDiffRefactoringsMatcher.java | 25 ++++++++++++------- .../astDiff/utils/Helpers.java | 18 +++++++++---- 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/refactoringminer/astDiff/matchers/ProjectASTDiffer.java b/src/main/java/org/refactoringminer/astDiff/matchers/ProjectASTDiffer.java index 5ed478b9bb..e04af16fd7 100644 --- a/src/main/java/org/refactoringminer/astDiff/matchers/ProjectASTDiffer.java +++ b/src/main/java/org/refactoringminer/astDiff/matchers/ProjectASTDiffer.java @@ -160,7 +160,7 @@ private void computeMovedDiffsEditScripts() { private void makeASTDiff(List umlClassBaseDiffList, boolean mergeFlag){ for (UMLAbstractClassDiff classDiff : umlClassBaseDiffList) { - Collection appends = findAppends(projectASTDiff.getDiffSet(), classDiff.getOriginalClass().getSourceFile(), classDiff.getNextClass().getSourceFile()); + Collection 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()) { diff --git a/src/main/java/org/refactoringminer/astDiff/matchers/wrappers/UnifiedModelDiffRefactoringsMatcher.java b/src/main/java/org/refactoringminer/astDiff/matchers/wrappers/UnifiedModelDiffRefactoringsMatcher.java index 2acf56a81c..b334bfd707 100644 --- a/src/main/java/org/refactoringminer/astDiff/matchers/wrappers/UnifiedModelDiffRefactoringsMatcher.java +++ b/src/main/java/org/refactoringminer/astDiff/matchers/wrappers/UnifiedModelDiffRefactoringsMatcher.java @@ -122,21 +122,28 @@ else if (refactoring.getRefactoringType().equals(RefactoringType.MOVE_AND_INLINE } private void findDiffsAndApplyMatcher(String srcPath, String dstPath, OptimizationAwareMatcher matcher) { - Collection diffs = findAppends(diffSet, srcPath, dstPath); + Collection 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 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 appends = findAppends(newlyGeneratedDiffsOptimizationMap.keySet(), srcPath, dstPath, true); + if (appends.isEmpty()) { + Pair 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(); diff --git a/src/main/java/org/refactoringminer/astDiff/utils/Helpers.java b/src/main/java/org/refactoringminer/astDiff/utils/Helpers.java index 1f65353758..e923eace11 100644 --- a/src/main/java/org/refactoringminer/astDiff/utils/Helpers.java +++ b/src/main/java/org/refactoringminer/astDiff/utils/Helpers.java @@ -44,13 +44,21 @@ public static Pair findPairOfType(Tree srcOperationNode, Tree dstOpe return null; } - public static Collection findAppends(Collection diffs, String originalSourceFile, String nextSourceFile) { + public static Collection findAppends(Collection diffs, String originalSourceFile, String nextSourceFile, boolean both) { Collection 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; }