Skip to content

Commit

Permalink
Promote Extract and Move Method in the same package
Browse files Browse the repository at this point in the history
  • Loading branch information
tsantalis committed Jan 5, 2025
1 parent 0d4f8c2 commit 7580830
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion src/main/java/gr/uom/java/xmi/diff/UMLModelDiff.java
Original file line number Diff line number Diff line change
Expand Up @@ -4392,7 +4392,7 @@ else if(childFieldDeclarationMap != null) {
!conflictingExpression(addedOperationInvocation, addedOperation, variableDeclarationMap, childFieldDeclarationMap)) {
UMLOperationBodyMapper operationBodyMapper = createMapperForExtractAndMove(addedOperation,
mapper, className, addedOperationInvocation, Optional.empty());
if(extractAndMoveMatchCondition(operationBodyMapper, mapper, addedOperationInvocation)) {
if(extractAndMoveMatchCondition(operationBodyMapper, mapper, addedOperationInvocation) && !skipRefactoring(operationBodyMapper.getMappings())) {
if(className.equals(addedOperation.getClassName())) {
//extract inside moved or renamed class
createExtractAndMoveMethodRefactoring(addedOperation, mapper, addedOperationInvocations, operationBodyMapper);
Expand Down Expand Up @@ -4455,6 +4455,38 @@ else if(mapping instanceof CompositeStatementObjectMapping && !mapper.getNonMapp
}
}

private static boolean samePackage(AbstractCodeMapping mapping) {
String filePathBefore = mapping.getFragment1().getLocationInfo().getFilePath().substring(0, mapping.getFragment1().getLocationInfo().getFilePath().lastIndexOf("/"));
String filePathAfter = mapping.getFragment2().getLocationInfo().getFilePath().substring(0, mapping.getFragment2().getLocationInfo().getFilePath().lastIndexOf("/"));
return filePathBefore.equals(filePathAfter);
}

private boolean skipRefactoring(Set<AbstractCodeMapping> mappings) {
boolean skip = false;
Set<Refactoring> refactoringsToBeRemoved = new LinkedHashSet<Refactoring>();
for(Refactoring r : this.refactorings) {
if(r instanceof ExtractOperationRefactoring) {
ExtractOperationRefactoring extract = (ExtractOperationRefactoring)r;
for(AbstractCodeMapping newMapping : mappings) {
boolean newMappingSamePackage = samePackage(newMapping);
for(AbstractCodeMapping oldMapping : extract.getBodyMapper().getMappings()) {
if(newMapping.getFragment1().equals(oldMapping.getFragment1())) {
boolean oldMappingSamePackage = samePackage(oldMapping);
if(newMappingSamePackage && !oldMappingSamePackage) {
refactoringsToBeRemoved.add(r);
}
else if(!newMappingSamePackage && oldMappingSamePackage) {
skip = true;
}
}
}
}
}
}
this.refactorings.removeAll(refactoringsToBeRemoved);
return skip;
}

private void createExtractAndMoveMethodRefactoring(UMLOperation addedOperation, UMLOperationBodyMapper mapper,
List<AbstractCall> addedOperationInvocations, UMLOperationBodyMapper operationBodyMapper)
throws RefactoringMinerTimedOutException {
Expand Down

0 comments on commit 7580830

Please sign in to comment.