Skip to content

Commit e8d8205

Browse files
pouryafard75tsantalis
authored andcommitted
ASTDiff: Fixes #967
1 parent 665852f commit e8d8205

File tree

6 files changed

+41
-21
lines changed

6 files changed

+41
-21
lines changed

src/main/java/org/refactoringminer/astDiff/matchers/wrappers/BodyMapperMatcher.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ else if (abstractCodeMapping instanceof CompositeStatementObjectMapping)
6363
new JavaDocMatcher(optimizationData, bodyMapper.getContainer1().getJavadoc(), bodyMapper.getContainer2().getJavadoc(), bodyMapper.getJavadocDiff()).match(srcTree,dstTree,mappingStore);
6464
new CommentMatcher(optimizationData, bodyMapper.getCommentListDiff()).match(srcTree,dstTree,mappingStore);
6565

66-
// new RefactoringMatcher(optimizationData, new ArrayList<>(bodyMapper.getRefactoringsAfterPostProcessing())).
67-
// matchAndUpdateOptimizationStore(srcTree, dstTree, mappingStore);
6866
}
6967

7068
private void processCompositeMapping(Tree srcTree, Tree dstTree, AbstractCodeMapping abstractCodeMapping, ExtendedMultiMappingStore mappingStore) {

src/main/java/org/refactoringminer/astDiff/matchers/wrappers/FieldDeclarationByAttrDiffMatcher.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ private void processFieldDeclarationByAttrDiff(Tree srcTree, Tree dstTree, UMLAt
2828
if (umlAttributeDiff.getInitializerMapper().isPresent()) {
2929
UMLOperationBodyMapper umlOperationBodyMapper = umlAttributeDiff.getInitializerMapper().get();
3030
new MethodMatcher(optimizationData, umlOperationBodyMapper).match(srcTree, dstTree, mappingStore);
31+
//TODO: if its a moved process its refactoring
3132
}
3233
}
3334
}

src/main/java/org/refactoringminer/astDiff/matchers/wrappers/MethodMatcher.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121
public class MethodMatcher extends BodyMapperMatcher{
2222

2323
private static final String THROWS_KEYWORD_LABEL = "throws";
24+
private boolean refactoringProcessor = false;
25+
26+
public MethodMatcher(UMLOperationBodyMapper bodyMapper, boolean isPartOfExtractMethod, boolean refactoringProcessor) {
27+
super(bodyMapper, isPartOfExtractMethod);
28+
this.refactoringProcessor = refactoringProcessor;
29+
}
2430

2531
public MethodMatcher(UMLOperationBodyMapper bodyMapper, boolean isPartOfExtractMethod) {
2632
super(bodyMapper, isPartOfExtractMethod);
@@ -78,6 +84,10 @@ private void processMethod(Tree srcTree, Tree dstTree, UMLOperationBodyMapper um
7884
new BodyMapperMatcher(optimizationData, umlOperationBodyMapper, isPartOfExtractedMethod).match(srcOperationNode, dstOperationNode, mappingStore);
7985
processOperationDiff(srcOperationNode, dstOperationNode, umlOperationBodyMapper, mappingStore);
8086
processMethodParameters(srcOperationNode, dstOperationNode, umlOperationBodyMapper.getMatchedVariables(), mappingStore);
87+
if (refactoringProcessor){
88+
new RefactoringMatcher(optimizationData, new ArrayList<>(bodyMapper.getRefactoringsAfterPostProcessing())).
89+
matchAndUpdateOptimizationStore(srcTree, dstTree, mappingStore);
90+
}
8191
}
8292
}
8393

src/main/java/org/refactoringminer/astDiff/matchers/wrappers/RefactoringMatcher.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,10 @@ else if (refactoring instanceof RenameVariableRefactoring) {
223223
break;
224224
case RENAME_PARAMETER:
225225
eligible = !renameVariableRefactoring.isInsideExtractedOrInlinedMethod();
226-
// for (AbstractCodeMapping abstractCodeMapping : renameVariableRefactoring.getReferences()) {
227-
// if (abstractCodeMapping instanceof LeafMapping) {
228-
// findVariablesAndMatch(TreeUtilFunctions.findByLocationInfo(srcTree, abstractCodeMapping.getFragment1().getLocationInfo()), TreeUtilFunctions.findByLocationInfo(dstTree, abstractCodeMapping.getFragment2().getLocationInfo()), renameVariableRefactoring.getOriginalVariable().getVariableName(), renameVariableRefactoring.getRenamedVariable().getVariableName());
229-
// }
230-
// }
226+
if (eligible)
227+
for (AbstractCodeMapping abstractCodeMapping : renameVariableRefactoring.getReferences())
228+
if (abstractCodeMapping instanceof LeafMapping)
229+
findVariablesAndMatch(TreeUtilFunctions.findByLocationInfo(srcTree, abstractCodeMapping.getFragment1().getLocationInfo()), TreeUtilFunctions.findByLocationInfo(dstTree, abstractCodeMapping.getFragment2().getLocationInfo()), renameVariableRefactoring.getOriginalVariable().getVariableName(), renameVariableRefactoring.getRenamedVariable().getVariableName());
231230
break;
232231
case RENAME_VARIABLE:
233232
Set<AbstractCodeMapping> references = renameVariableRefactoring.getReferences();
@@ -389,16 +388,16 @@ private void findVariablesAndMatch(Tree srcStatement, Tree dstStatement, String
389388
}
390389
}
391390
else {
392-
// //find the ones with the same parent type and match em
393-
// for (Tree srcRef : srcRefs) {
394-
// for (Tree dstRef : dstRefs) {
395-
// if (srcRef.getParent() != null && dstRef.getParent() != null &&
396-
// srcRef.getParent().getType().name.equals(dstRef.getParent().getType().name)) {
397-
// optimizationData.getSubtreeMappings().addMapping(srcRef, dstRef);
398-
// break;
399-
// }
400-
// }
401-
// }
391+
//TODO: Begin with the one with fewer occurrences, find the match with the exact same parent type
392+
for (Tree srcRef : srcRefs) {
393+
for (Tree dstRef : dstRefs) {
394+
if (srcRef.getParent() != null && dstRef.getParent() != null &&
395+
srcRef.getParent().getType().name.equals(dstRef.getParent().getType().name)) {
396+
optimizationData.getSubtreeMappings().addMapping(srcRef, dstRef);
397+
break;
398+
}
399+
}
400+
}
402401

403402
}
404403
}

src/main/java/org/refactoringminer/astDiff/matchers/wrappers/UnifiedModelDiffRefactoringsMatcher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ private void processModeldiffRefactorings() {
8585
// if (isExtractedMethodRef(moveOperationRefactoring.getBodyMapper().getOperation2())) continue;
8686
String srcPath = moveOperationRefactoring.getOriginalOperation().getLocationInfo().getFilePath();
8787
String dstPath = moveOperationRefactoring.getMovedOperation().getLocationInfo().getFilePath();
88-
findDiffsAndApplyMatcher(srcPath, dstPath, new MethodMatcher(moveOperationRefactoring.getBodyMapper(), true));
88+
findDiffsAndApplyMatcher(srcPath, dstPath, new MethodMatcher(moveOperationRefactoring.getBodyMapper(), true, true));
8989
if (moveOperationRefactoring.getOriginalOperation().getJavadoc() == null && moveOperationRefactoring.getMovedOperation().getJavadoc() != null) {
9090
UMLClass originalClass = modelDiff.getRemovedClass(moveOperationRefactoring.getOriginalOperation().getClassName());
9191
if (originalClass != null && originalClass.getJavadoc() != null) {

src/main/java/org/refactoringminer/astDiff/models/ExtendedMultiMappingStore.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import com.github.gumtreediff.tree.FakeTree;
99
import com.github.gumtreediff.tree.Tree;
1010
import com.github.gumtreediff.utils.Pair;
11+
import org.refactoringminer.astDiff.utils.Constants;
12+
import org.refactoringminer.astDiff.utils.TreeUtilFunctions;
1113

1214
/**
1315
* @author Pourya Alikhani Fard [email protected]
@@ -236,18 +238,28 @@ public void replaceWithOptimizedMappings(ExtendedMultiMappingStore optimizationM
236238
if (this.getDsts(srcMapped) != null)
237239
{
238240
Set<Tree> dstForSrcList = new LinkedHashSet<>(this.getDsts(srcMapped));
239-
for (Tree dstForSrc : dstForSrcList)
240-
removeMapping(srcMapped,dstForSrc);
241+
for (Tree dstForSrc : dstForSrcList) {
242+
if (sameFile(dstMapped, dstForSrc))
243+
removeMapping(srcMapped, dstForSrc);
244+
}
241245
}
242246
if (this.getSrcs(dstMapped) != null)
243247
{
244248
Set<Tree> srcForDstList = new LinkedHashSet<>(this.getSrcs(dstMapped));
245249
for (Tree srcForDst : srcForDstList)
246-
removeMapping(srcForDst,dstMapped);
250+
if (sameFile(srcMapped, srcForDst))
251+
removeMapping(srcForDst,dstMapped);
247252
}
248253
}
249254
for (Mapping optimizationMapping : optimizationMappings) {
250255
this.addMapping(optimizationMapping.first,optimizationMapping.second);
251256
}
252257
}
258+
259+
private boolean sameFile(Tree t1, Tree t2) {
260+
// find the most parent of both, and compare
261+
Tree t1_p = TreeUtilFunctions.getParentUntilType(t1, Constants.COMPILATION_UNIT);
262+
Tree t2_p = TreeUtilFunctions.getParentUntilType(t2, Constants.COMPILATION_UNIT);
263+
return Objects.equals(t1_p, t2_p);
264+
}
253265
}

0 commit comments

Comments
 (0)