Skip to content

Commit 4ade705

Browse files
committed
Support Move Code refactoring between different files
eclipse-jgit/jgit@faefa90 This commit has code moved between the setUp and tearDown methods of two test files. org.eclipse.jgit.junit.ssh/src/org/eclipse/jgit/junit/ssh/SshTestHarness.java org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/LocalDiskRepositoryTestCase.java
1 parent 4df3f64 commit 4ade705

File tree

5 files changed

+88
-7
lines changed

5 files changed

+88
-7
lines changed

src/main/java/gr/uom/java/xmi/decomposition/UMLOperationBodyMapper.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4309,6 +4309,14 @@ public boolean involvesTestMethods() {
43094309
return container1.hasTestAnnotation() && container2.hasTestAnnotation();
43104310
}
43114311

4312+
public boolean involvesSetUpMethods() {
4313+
return container1.hasSetUpAnnotation() && container2.hasSetUpAnnotation();
4314+
}
4315+
4316+
public boolean involvesTearDownMethods() {
4317+
return container1.hasTearDownAnnotation() && container2.hasTearDownAnnotation();
4318+
}
4319+
43124320
private void processInnerNodes(List<CompositeStatementObject> innerNodes1, List<CompositeStatementObject> innerNodes2,
43134321
List<AbstractCodeFragment> leaves1, List<AbstractCodeFragment> leaves2,
43144322
Map<String, String> parameterToArgumentMap, boolean containsCallToExtractedMethod) throws RefactoringMinerTimedOutException {

src/main/java/gr/uom/java/xmi/diff/MoveCodeRefactoring.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ public class MoveCodeRefactoring implements Refactoring {
1919
public enum Type {
2020
MOVE_FROM_REMOVED,
2121
MOVE_TO_ADDED,
22-
MOVE_BETWEEN_EXISTING;
22+
MOVE_BETWEEN_EXISTING,
23+
MOVE_BETWEEN_FILES;
2324
}
2425
private VariableDeclarationContainer sourceContainer;
2526
private VariableDeclarationContainer targetContainer;

src/main/java/gr/uom/java/xmi/diff/UMLModelDiff.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1826,6 +1826,13 @@ private boolean instanceAttributeMovedAlongWithMethod(UMLAttribute addedAttribut
18261826
return true;
18271827
}
18281828
}
1829+
else if(r instanceof MoveCodeRefactoring) {
1830+
MoveCodeRefactoring move = (MoveCodeRefactoring)r;
1831+
if(move.getSourceContainer().getClassName().equals(removedAttribute.getClassName()) &&
1832+
move.getTargetContainer().getClassName().equals(addedAttribute.getClassName())) {
1833+
return true;
1834+
}
1835+
}
18291836
else if(r instanceof ExtractOperationRefactoring) {
18301837
ExtractOperationRefactoring extract = (ExtractOperationRefactoring)r;
18311838
if(extract.getSourceOperationBeforeExtraction().getClassName().equals(removedAttribute.getClassName()) &&
@@ -3275,6 +3282,7 @@ else if(!diffs2.isEmpty()) {
32753282
if(allOperationsInAddedClasses.size() <= MAXIMUM_NUMBER_OF_COMPARED_METHODS) {
32763283
checkForExtractedAndMovedOperations(getOperationBodyMappersInCommonClasses(), allOperationsInAddedClasses);
32773284
}
3285+
checkForMovedCodeBetweenTestFixtures();
32783286
List<MoveAttributeRefactoring> moveAttributeRefactorings = new ArrayList<MoveAttributeRefactoring>();
32793287
moveAttributeRefactorings.addAll(checkForAttributeMovesBetweenCommonClasses(renameMap, refactorings));
32803288
moveAttributeRefactorings.addAll(checkForAttributeMovesIncludingAddedClasses(renameMap, refactorings));
@@ -5507,6 +5515,56 @@ else if(r instanceof MoveOperationRefactoring) {
55075515
}
55085516
}
55095517

5518+
private void checkForMovedCodeBetweenTestFixtures() throws RefactoringMinerTimedOutException {
5519+
List<UMLOperationBodyMapper> mappersWithUnmatchedStatementsInSetUpT1 = new ArrayList<UMLOperationBodyMapper>();
5520+
List<UMLOperationBodyMapper> mappersWithUnmatchedStatementsInSetUpT2 = new ArrayList<UMLOperationBodyMapper>();
5521+
List<UMLOperationBodyMapper> mappersWithUnmatchedStatementsInTearDownT1 = new ArrayList<UMLOperationBodyMapper>();
5522+
List<UMLOperationBodyMapper> mappersWithUnmatchedStatementsInTearDownT2 = new ArrayList<UMLOperationBodyMapper>();
5523+
for(UMLClassDiff classDiff : commonClassDiffList) {
5524+
for(UMLOperationBodyMapper mapper : classDiff.getOperationBodyMapperList()) {
5525+
if(mapper.involvesSetUpMethods() || mapper.getContainer1().getName().equals("setUp")) {
5526+
if(mapper.getNonMappedLeavesT1().size() > 0 || mapper.getNonMappedInnerNodesT1().size() > 0) {
5527+
mappersWithUnmatchedStatementsInSetUpT1.add(mapper);
5528+
}
5529+
else if(mapper.getNonMappedLeavesT2().size() > 0 || mapper.getNonMappedInnerNodesT2().size() > 0) {
5530+
mappersWithUnmatchedStatementsInSetUpT2.add(mapper);
5531+
}
5532+
}
5533+
else if(mapper.involvesTearDownMethods() || mapper.getContainer1().getName().equals("tearDown")) {
5534+
if(mapper.getNonMappedLeavesT1().size() > 0 || mapper.getNonMappedInnerNodesT1().size() > 0) {
5535+
mappersWithUnmatchedStatementsInTearDownT1.add(mapper);
5536+
}
5537+
else if(mapper.getNonMappedLeavesT2().size() > 0 || mapper.getNonMappedInnerNodesT2().size() > 0) {
5538+
mappersWithUnmatchedStatementsInTearDownT2.add(mapper);
5539+
}
5540+
}
5541+
}
5542+
}
5543+
List<UMLOperationBodyMapper> moveCodeMappers = new ArrayList<>();
5544+
for(UMLOperationBodyMapper mapperT1 : mappersWithUnmatchedStatementsInSetUpT1) {
5545+
for(UMLOperationBodyMapper mapperT2 : mappersWithUnmatchedStatementsInSetUpT2) {
5546+
UMLOperationBodyMapper moveCodeMapper = new UMLOperationBodyMapper(mapperT1, mapperT2, mapperT1.getClassDiff());
5547+
if(moveCodeMapper.getMappings().size() > 0) {
5548+
MoveCodeRefactoring ref = new MoveCodeRefactoring(moveCodeMapper.getContainer1(), moveCodeMapper.getContainer2(), moveCodeMapper, Type.MOVE_BETWEEN_FILES);
5549+
if(!moveCodeMappers.contains(moveCodeMapper))
5550+
moveCodeMappers.add(moveCodeMapper);
5551+
refactorings.add(ref);
5552+
}
5553+
}
5554+
}
5555+
for(UMLOperationBodyMapper mapperT1 : mappersWithUnmatchedStatementsInTearDownT1) {
5556+
for(UMLOperationBodyMapper mapperT2 : mappersWithUnmatchedStatementsInTearDownT2) {
5557+
UMLOperationBodyMapper moveCodeMapper = new UMLOperationBodyMapper(mapperT1, mapperT2, mapperT1.getClassDiff());
5558+
if(moveCodeMapper.getMappings().size() > 0) {
5559+
MoveCodeRefactoring ref = new MoveCodeRefactoring(moveCodeMapper.getContainer1(), moveCodeMapper.getContainer2(), moveCodeMapper, Type.MOVE_BETWEEN_FILES);
5560+
if(!moveCodeMappers.contains(moveCodeMapper))
5561+
moveCodeMappers.add(moveCodeMapper);
5562+
refactorings.add(ref);
5563+
}
5564+
}
5565+
}
5566+
}
5567+
55105568
private Map<UMLTypeParameter, UMLType> typeParameterToTypeArgumentMap(String subclass, String superclass) {
55115569
Map<UMLTypeParameter, UMLType> typeParameterToTypeArgumentMap = new LinkedHashMap<UMLTypeParameter, UMLType>();
55125570
UMLClassBaseDiff subclassDiff = getUMLClassDiff(subclass);

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import gr.uom.java.xmi.VariableDeclarationContainer;
88
import gr.uom.java.xmi.decomposition.*;
99
import gr.uom.java.xmi.diff.*;
10+
import gr.uom.java.xmi.diff.MoveCodeRefactoring.Type;
11+
1012
import org.apache.commons.lang3.tuple.ImmutablePair;
1113
import org.refactoringminer.api.Refactoring;
1214
import org.refactoringminer.api.RefactoringType;
@@ -125,8 +127,10 @@ private void processRefactorings(Tree srcTree, Tree dstTree, List<Refactoring> r
125127
processArgumentMappings(srcTree, dstTree, refactoringList, refactoring, inlineOperationRefactoring.getArgumentMappings());
126128
} else if (refactoring instanceof MoveCodeRefactoring) {
127129
MoveCodeRefactoring moveCodeRefactoring = (MoveCodeRefactoring) refactoring;
128-
UMLOperationBodyMapper bodyMapper = moveCodeRefactoring.getBodyMapper();
129-
new BodyMapperMatcher(optimizationData, bodyMapper, false).match(srcTree,dstTree,mappingStore);
130+
if (!moveCodeRefactoring.getMoveType().equals(Type.MOVE_BETWEEN_FILES)) {
131+
UMLOperationBodyMapper bodyMapper = moveCodeRefactoring.getBodyMapper();
132+
new BodyMapperMatcher(optimizationData, bodyMapper, false).match(srcTree,dstTree,mappingStore);
133+
}
130134
} else if (refactoring instanceof ParameterizeTestRefactoring) {
131135
ParameterizeTestRefactoring parameterizeTestRefactoring = (ParameterizeTestRefactoring) refactoring;
132136
UMLOperationBodyMapper bodyMapper = parameterizeTestRefactoring.getBodyMapper();

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import gr.uom.java.xmi.UMLClass;
88
import gr.uom.java.xmi.UMLComment;
99
import gr.uom.java.xmi.diff.*;
10+
import gr.uom.java.xmi.diff.MoveCodeRefactoring.Type;
11+
1012
import org.refactoringminer.api.Refactoring;
1113
import org.refactoringminer.api.RefactoringType;
1214
import org.refactoringminer.astDiff.models.ASTDiff;
@@ -108,16 +110,24 @@ else if (refactoring instanceof MoveAttributeRefactoring)
108110
else if (refactoring.getRefactoringType().equals(RefactoringType.EXTRACT_AND_MOVE_OPERATION))
109111
{
110112
ExtractOperationRefactoring extractOperationRefactoring = (ExtractOperationRefactoring) refactoring;
111-
String srcPath = extractOperationRefactoring.getBodyMapper().getOperation1().getLocationInfo().getFilePath();
112-
String dstPath = extractOperationRefactoring.getBodyMapper().getOperation2().getLocationInfo().getFilePath();
113+
String srcPath = extractOperationRefactoring.getBodyMapper().getContainer1().getLocationInfo().getFilePath();
114+
String dstPath = extractOperationRefactoring.getBodyMapper().getContainer2().getLocationInfo().getFilePath();
113115
findDiffsAndApplyMatcher(srcPath, dstPath, new BodyMapperMatcher(extractOperationRefactoring.getBodyMapper(), true));
114116
}
115117
else if (refactoring.getRefactoringType().equals(RefactoringType.MOVE_AND_INLINE_OPERATION)) {
116118
InlineOperationRefactoring inlineOperationRefactoring = (InlineOperationRefactoring) refactoring;
117-
String srcPath = inlineOperationRefactoring.getBodyMapper().getOperation1().getLocationInfo().getFilePath();
118-
String dstPath = inlineOperationRefactoring.getBodyMapper().getOperation2().getLocationInfo().getFilePath();
119+
String srcPath = inlineOperationRefactoring.getBodyMapper().getContainer1().getLocationInfo().getFilePath();
120+
String dstPath = inlineOperationRefactoring.getBodyMapper().getContainer2().getLocationInfo().getFilePath();
119121
findDiffsAndApplyMatcher(srcPath, dstPath, new BodyMapperMatcher(inlineOperationRefactoring.getBodyMapper(), true));
120122
}
123+
else if (refactoring.getRefactoringType().equals(RefactoringType.MOVE_CODE)) {
124+
MoveCodeRefactoring moveCodeRefactoring = (MoveCodeRefactoring) refactoring;
125+
if (moveCodeRefactoring.getMoveType().equals(Type.MOVE_BETWEEN_FILES)) {
126+
String srcPath = moveCodeRefactoring.getBodyMapper().getContainer1().getLocationInfo().getFilePath();
127+
String dstPath = moveCodeRefactoring.getBodyMapper().getContainer2().getLocationInfo().getFilePath();
128+
findDiffsAndApplyMatcher(srcPath, dstPath, new BodyMapperMatcher(moveCodeRefactoring.getBodyMapper(), true));
129+
}
130+
}
121131
}
122132
}
123133

0 commit comments

Comments
 (0)