Skip to content

Commit

Permalink
Run MappingOptimizer for Extract And Move Method refactorings
Browse files Browse the repository at this point in the history
pmd/pmd@5e92b59
Eliminates refactoring
Extract And Move Method	public newCollector(collector FileCollector, reporter PmdReporter) : FileCollector extracted from private prepareNewSourceManager(config CPDConfiguration, dirPath Path, recurse boolean) : void in class net.sourceforge.pmd.cpd.GUI & moved to class net.sourceforge.pmd.lang.document.InternalApiBridge
  • Loading branch information
tsantalis committed Jan 6, 2025
1 parent 1a6a1bf commit 852b145
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/gr/uom/java/xmi/diff/MappingOptimizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public MappingOptimizer(UMLAbstractClassDiff classDiff) {
this.classDiff = classDiff;
}

public void optimizeDuplicateMappingsForMoveCode(List<UMLOperationBodyMapper> moveCodeMappers, List<Refactoring> refactorings) {
public void optimizeDuplicateMappingsForMoveCode(List<UMLOperationBodyMapper> moveCodeMappers, Collection<Refactoring> refactorings) {
if(moveCodeMappers.size() > 1) {
Map<AbstractCodeFragment, List<AbstractCodeMapping>> oneToManyMappings = new HashMap<>();
Map<AbstractCodeFragment, List<UMLOperationBodyMapper>> oneToManyMappers = new HashMap<>();
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/gr/uom/java/xmi/diff/UMLModelDiff.java
Original file line number Diff line number Diff line change
Expand Up @@ -4492,6 +4492,24 @@ else if(!newMappingSamePackage && oldMappingSamePackage) {
return skip;
}

private Set<UMLOperationBodyMapper> findMappersWithTheSameFragment1(Set<AbstractCodeMapping> mappings) {
Set<UMLOperationBodyMapper> mappers = new LinkedHashSet<UMLOperationBodyMapper>();
for(Refactoring r : this.refactorings) {
if(r instanceof ExtractOperationRefactoring) {
ExtractOperationRefactoring extract = (ExtractOperationRefactoring)r;
for(AbstractCodeMapping newMapping : mappings) {
for(AbstractCodeMapping oldMapping : extract.getBodyMapper().getMappings()) {
if(newMapping.getFragment1().equals(oldMapping.getFragment1()) && !newMapping.equals(oldMapping)) {
mappers.add(extract.getBodyMapper());
break;
}
}
}
}
}
return mappers;
}

private void createExtractAndMoveMethodRefactoring(UMLOperation addedOperation, UMLOperationBodyMapper mapper,
List<AbstractCall> addedOperationInvocations, UMLOperationBodyMapper operationBodyMapper)
throws RefactoringMinerTimedOutException {
Expand All @@ -4503,6 +4521,11 @@ private void createExtractAndMoveMethodRefactoring(UMLOperation addedOperation,
mapper.addChildMapper(operationBodyMapper);
MappingOptimizer optimizer = new MappingOptimizer(mapper.getClassDiff());
optimizer.optimizeDuplicateMappingsForExtract(mapper, refactorings);
Set<UMLOperationBodyMapper> mappers = findMappersWithTheSameFragment1(operationBodyMapper.getMappings());
if(mappers.size() > 0) {
mappers.add(operationBodyMapper);
optimizer.optimizeDuplicateMappingsForMoveCode(new ArrayList<>(mappers), refactorings);
}
}

private UMLOperationBodyMapper createMapperForExtractAndMove(UMLOperation addedOperation,
Expand Down

0 comments on commit 852b145

Please sign in to comment.