Skip to content

Commit

Permalink
Support new Move Code scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
tsantalis committed Oct 25, 2024
1 parent f1d41c1 commit dc6a601
Show file tree
Hide file tree
Showing 12 changed files with 1,290 additions and 4 deletions.
20 changes: 20 additions & 0 deletions src/main/java/gr/uom/java/xmi/diff/UMLClassBaseDiff.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public abstract class UMLClassBaseDiff extends UMLAbstractClassDiff implements C
private Map<MethodInvocationReplacement, UMLOperationBodyMapper> consistentMethodInvocationRenames;
private Set<UMLOperationBodyMapper> potentialCodeMoveBetweenSetUpTearDownMethods = new LinkedHashSet<>();
private Set<UMLOperationBodyMapper> movedMethodsInDifferentPositionWithinFile = new LinkedHashSet<>();
private Set<Pair<UMLOperationBodyMapper, UMLOperationBodyMapper>> calledBy = new LinkedHashSet<>();
private Optional<Pair<UMLType, UMLType>> implementedInterfaceBecomesSuperclass;
private Optional<Pair<UMLType, UMLType>> superclassBecomesImplementedInterface;
private Optional<UMLJavadocDiff> javadocDiff;
Expand Down Expand Up @@ -368,6 +369,19 @@ else if(removedOperation.isConstructor()) {
refactorings.add(ref);
}
}
for(Pair<UMLOperationBodyMapper, UMLOperationBodyMapper> pair : calledBy) {
UMLOperationBodyMapper called = pair.getLeft();
UMLOperationBodyMapper caller = pair.getRight();
if(!called.getContainer1().getParameterTypeList().equals(called.getContainer2().getParameterTypeList())) {
UMLOperationBodyMapper moveCodeMapper = new UMLOperationBodyMapper(caller, called, this);
if(moveCodeMapper.mappingsWithoutBlocks() > 1 || moveCodeMapper.exactMatches() > 0) {
MoveCodeRefactoring ref = new MoveCodeRefactoring(moveCodeMapper.getContainer1(), moveCodeMapper.getContainer2(), moveCodeMapper, Type.MOVE_BETWEEN_EXISTING);
if(!moveCodeMappers.contains(moveCodeMapper))
moveCodeMappers.add(moveCodeMapper);
refactorings.add(ref);
}
}
}
MappingOptimizer optimizer = new MappingOptimizer(this);
optimizer.optimizeDuplicateMappingsForMoveCode(moveCodeMappers, refactorings);
}
Expand Down Expand Up @@ -1181,6 +1195,12 @@ else if(removedOperation.hasTestAnnotation() && addedOperation.hasParameterizedT
RenameOperationRefactoring rename = new RenameOperationRefactoring(bestMapper, callReferences);
refactorings.add(rename);
}
for(UMLOperationBodyMapper mapper : operationBodyMapperList) {
if(containCallToOperation(bestMapper.getContainer1(), mapper.getContainer1()) && containCallToOperation(bestMapper.getContainer2(), mapper.getContainer2())) {
Pair<UMLOperationBodyMapper, UMLOperationBodyMapper> pair = Pair.of(bestMapper, mapper);
calledBy.add(pair);
}
}
this.addOperationBodyMapper(bestMapper);
consistentMethodInvocationRenames = findConsistentMethodInvocationRenames();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public void testAllRefactorings() throws Exception {
GitHistoryRefactoringMinerImpl detector = new GitHistoryRefactoringMinerImpl();
TestBuilder test = new TestBuilder(detector, REPOS, Refactorings.All.getValue());
RefactoringPopulator.feedRefactoringsInstances(Refactorings.All.getValue(), Systems.FSE.getValue(), test);
test.assertExpectationsWithGitHubAPI(12292, 20, 232);
test.assertExpectationsWithGitHubAPI(12295, 20, 232);
}
}
54 changes: 54 additions & 0 deletions src/test/java/org/refactoringminer/test/TestStatementMappings.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
import gr.uom.java.xmi.diff.ExtractOperationRefactoring;
import gr.uom.java.xmi.diff.InlineOperationRefactoring;
import gr.uom.java.xmi.diff.MergeOperationRefactoring;
import gr.uom.java.xmi.diff.MoveCodeRefactoring;
import gr.uom.java.xmi.diff.ParameterizeTestRefactoring;
import gr.uom.java.xmi.diff.RenameOperationRefactoring;
import gr.uom.java.xmi.diff.UMLAbstractClassDiff;
import gr.uom.java.xmi.diff.UMLAnonymousClassDiff;
import gr.uom.java.xmi.diff.UMLClassBaseDiff;
import gr.uom.java.xmi.diff.UMLClassDiff;
import gr.uom.java.xmi.diff.UMLEnumConstantDiff;
import gr.uom.java.xmi.diff.UMLModelDiff;
Expand Down Expand Up @@ -1133,6 +1135,58 @@ else if(ref instanceof InlineOperationRefactoring) {
Assertions.assertTrue(expected.size() == actual.size() && expected.containsAll(actual) && actual.containsAll(expected));
}

@Test
public void testMoveCodeStatementMappings() throws Exception {
GitHistoryRefactoringMinerImpl miner = new GitHistoryRefactoringMinerImpl();
final List<String> actual = new ArrayList<>();
miner.detectAtCommitWithGitHubAPI("https://github.com/checkstyle/checkstyle.git", "1a2c318e22a0b2b22ccc76019217c0892fe2d59b", new File(REPOS), new RefactoringHandler() {
@Override
public void handleModelDiff(String commitId, List<Refactoring> refactorings, UMLModelDiff modelDiff) {
List<UMLOperationBodyMapper> parentMappers = new ArrayList<>();
for (Refactoring ref : refactorings) {
if(ref instanceof ExtractOperationRefactoring) {
ExtractOperationRefactoring ex = (ExtractOperationRefactoring)ref;
UMLOperationBodyMapper bodyMapper = ex.getBodyMapper();
if(!bodyMapper.isNested()) {
if(!parentMappers.contains(bodyMapper.getParentMapper())) {
parentMappers.add(bodyMapper.getParentMapper());
}
}
mapperInfo(bodyMapper, actual);
}
else if(ref instanceof InlineOperationRefactoring) {
InlineOperationRefactoring in = (InlineOperationRefactoring)ref;
UMLOperationBodyMapper bodyMapper = in.getBodyMapper();
if(!bodyMapper.isNested()) {
if(!parentMappers.contains(bodyMapper.getParentMapper())) {
parentMappers.add(bodyMapper.getParentMapper());
}
}
mapperInfo(bodyMapper, actual);
}
else if(ref instanceof MoveCodeRefactoring) {
MoveCodeRefactoring in = (MoveCodeRefactoring)ref;
UMLOperationBodyMapper bodyMapper = in.getBodyMapper();
mapperInfo(bodyMapper, actual);
}
}
for(UMLOperationBodyMapper parentMapper : parentMappers) {
mapperInfo(parentMapper, actual);
}
//add main mapper
UMLClassBaseDiff classDiff = modelDiff.getUMLClassDiff("com.puppycrawl.tools.checkstyle.Main");
for(UMLOperationBodyMapper mapper : classDiff.getOperationBodyMapperList()) {
if(mapper.getContainer1().getName().equals("main") && mapper.getContainer2().getName().equals("main")) {
mapperInfo(mapper, actual);
break;
}
}
}
});
List<String> expected = IOUtils.readLines(new FileReader(EXPECTED_PATH + "checkstyle-1a2c318e22a0b2b22ccc76019217c0892fe2d59b.txt"));
Assertions.assertTrue(expected.size() == actual.size() && expected.containsAll(actual) && actual.containsAll(expected));
}

@Test
public void testLogGuardStatementMappings() throws Exception {
GitHistoryRefactoringMinerImpl miner = new GitHistoryRefactoringMinerImpl();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
import gr.uom.java.xmi.diff.ExtractOperationRefactoring;
import gr.uom.java.xmi.diff.InlineOperationRefactoring;
import gr.uom.java.xmi.diff.MergeOperationRefactoring;
import gr.uom.java.xmi.diff.MoveCodeRefactoring;
import gr.uom.java.xmi.diff.ParameterizeTestRefactoring;
import gr.uom.java.xmi.diff.RenameOperationRefactoring;
import gr.uom.java.xmi.diff.UMLAbstractClassDiff;
import gr.uom.java.xmi.diff.UMLAnonymousClassDiff;
import gr.uom.java.xmi.diff.UMLClassBaseDiff;
import gr.uom.java.xmi.diff.UMLClassDiff;
import gr.uom.java.xmi.diff.UMLEnumConstantDiff;
import gr.uom.java.xmi.diff.UMLModelDiff;
Expand Down Expand Up @@ -2378,6 +2380,58 @@ else if(ref instanceof InlineOperationRefactoring) {
Assert.assertTrue(expected.size() == actual.size() && expected.containsAll(actual) && actual.containsAll(expected));
}

@Test
public void testMoveCodeStatementMappings() throws Exception {
GitHistoryRefactoringMinerImpl miner = new GitHistoryRefactoringMinerImpl();
final List<String> actual = new ArrayList<>();
miner.detectAtCommitWithGitHubAPI("https://github.com/checkstyle/checkstyle.git", "1a2c318e22a0b2b22ccc76019217c0892fe2d59b", new File(REPOS), new RefactoringHandler() {
@Override
public void handleModelDiff(String commitId, List<Refactoring> refactorings, UMLModelDiff modelDiff) {
List<UMLOperationBodyMapper> parentMappers = new ArrayList<>();
for (Refactoring ref : refactorings) {
if(ref instanceof ExtractOperationRefactoring) {
ExtractOperationRefactoring ex = (ExtractOperationRefactoring)ref;
UMLOperationBodyMapper bodyMapper = ex.getBodyMapper();
if(!bodyMapper.isNested()) {
if(!parentMappers.contains(bodyMapper.getParentMapper())) {
parentMappers.add(bodyMapper.getParentMapper());
}
}
mapperInfo(bodyMapper, actual);
}
else if(ref instanceof InlineOperationRefactoring) {
InlineOperationRefactoring in = (InlineOperationRefactoring)ref;
UMLOperationBodyMapper bodyMapper = in.getBodyMapper();
if(!bodyMapper.isNested()) {
if(!parentMappers.contains(bodyMapper.getParentMapper())) {
parentMappers.add(bodyMapper.getParentMapper());
}
}
mapperInfo(bodyMapper, actual);
}
else if(ref instanceof MoveCodeRefactoring) {
MoveCodeRefactoring in = (MoveCodeRefactoring)ref;
UMLOperationBodyMapper bodyMapper = in.getBodyMapper();
mapperInfo(bodyMapper, actual);
}
}
for(UMLOperationBodyMapper parentMapper : parentMappers) {
mapperInfo(parentMapper, actual);
}
//add main mapper
UMLClassBaseDiff classDiff = modelDiff.getUMLClassDiff("com.puppycrawl.tools.checkstyle.Main");
for(UMLOperationBodyMapper mapper : classDiff.getOperationBodyMapperList()) {
if(mapper.getContainer1().getName().equals("main") && mapper.getContainer2().getName().equals("main")) {
mapperInfo(mapper, actual);
break;
}
}
}
});
List<String> expected = IOUtils.readLines(new FileReader(EXPECTED_PATH + "checkstyle-1a2c318e22a0b2b22ccc76019217c0892fe2d59b.txt"));
Assert.assertTrue(expected.size() == actual.size() && expected.containsAll(actual) && actual.containsAll(expected));
}

@Test
public void testLogGuardStatementMappings() throws Exception {
GitHistoryRefactoringMinerImpl miner = new GitHistoryRefactoringMinerImpl();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
private createChecker(config Configuration, auditListener AuditListener) : Checker -> private runCheckstyle(cliOptions CliOptions) : int
line range:184-184==line range:231-231
line range:186-186==line range:235-235
line range:187-187==line range:236-236
line range:188-188==line range:237-237
line range:189-189==line range:238-238
private loadConfig(format String, props Properties) : Configuration -> private runCheckstyle(cliOptions CliOptions) : int
line range:261-261==line range:223-224
private processCommandLine(line CommandLine) : void -> public main(args String...) : void
line range:117-118==line range:76-77
line range:124-124==line range:94-94
line range:116-120==line range:75-97
line range:116-120==line range:75-78
line range:123-126==line range:93-95
private processCommandLine(line CommandLine) : void -> private createListener(format String, outputLocation String) : AuditListener
line range:137-137==line range:290-290
line range:138-138==line range:291-291
line range:143-143==line range:294-294
line range:151-151==line range:297-297
line range:152-152==line range:298-298
line range:142-142==line range:293-293
line range:150-153==line range:296-299
private processCommandLine(line CommandLine) : void -> private runCheckstyle(cliOptions CliOptions) : int
line range:164-164==line range:245-245
line range:129-132==line range:217-220
line range:134-134==line range:223-224
line range:156-156==line range:227-227
line range:163-163==line range:230-230
line range:163-163==line range:241-241
line range:119-119==line range:248-248
public main(args String...) : void -> public main(args String...) : void
line range:63-63==line range:68-68
line range:66-66==line range:101-101
line range:69-69==line range:103-103
line range:73-73==line range:114-114
line range:81-81==line range:117-117
line range:64-82==line range:70-118
line range:76-82==line range:111-118
line range:68-71==line range:99-105
line range:72-75==line range:106-110
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"parentCommitId":"d6272b70eb5ad61fd8ab34091e970c50b1f67386","currentCommitId":"1a2c318e22a0b2b22ccc76019217c0892fe2d59b","filesBefore":["src/main/java/com/puppycrawl/tools/checkstyle/Main.java"],"filesCurrent":["src/main/java/com/puppycrawl/tools/checkstyle/Main.java","src/test/java/com/puppycrawl/tools/checkstyle/MainTest.java","src/test/resources/com/puppycrawl/tools/checkstyle/InputMain.java"],"renamedFilesHint":{},"repositoryDirectoriesBefore":["src/main/java/com","src/main","src/main/java/com/puppycrawl","src/main/java/com/puppycrawl/tools","src","src/main/java","src/main/java/com/puppycrawl/tools/checkstyle"],"repositoryDirectoriesCurrent":["src/main/java/com","src/main","src/main/java/com/puppycrawl/tools","src/test/java/com/puppycrawl/tools/checkstyle","src","src/test/java/com","src/main/java","src/main/java/com/puppycrawl/tools/checkstyle","src/test/java/com/puppycrawl","src/test/resources/com/puppycrawl/tools/checkstyle","src/test/java","src/test/resources/com","src/main/java/com/puppycrawl","src/test","src/test/resources","src/test/resources/com/puppycrawl","src/test/java/com/puppycrawl/tools","src/test/resources/com/puppycrawl/tools"],"commitTime":0,"authoredTime":0,"commitAuthorName":null}
Loading

0 comments on commit dc6a601

Please sign in to comment.