From 852b145ea2066f9eba5494e988181727b5fe105f Mon Sep 17 00:00:00 2001 From: tsantalis Date: Sun, 5 Jan 2025 19:26:45 -0500 Subject: [PATCH] Run MappingOptimizer for Extract And Move Method refactorings https://github.com/pmd/pmd/commit/5e92b5907e82c553f3d5a1ef05f96305dc15c8a5 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 --- .../uom/java/xmi/diff/MappingOptimizer.java | 2 +- .../gr/uom/java/xmi/diff/UMLModelDiff.java | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/main/java/gr/uom/java/xmi/diff/MappingOptimizer.java b/src/main/java/gr/uom/java/xmi/diff/MappingOptimizer.java index 523c35f69..f72982a2e 100644 --- a/src/main/java/gr/uom/java/xmi/diff/MappingOptimizer.java +++ b/src/main/java/gr/uom/java/xmi/diff/MappingOptimizer.java @@ -35,7 +35,7 @@ public MappingOptimizer(UMLAbstractClassDiff classDiff) { this.classDiff = classDiff; } - public void optimizeDuplicateMappingsForMoveCode(List moveCodeMappers, List refactorings) { + public void optimizeDuplicateMappingsForMoveCode(List moveCodeMappers, Collection refactorings) { if(moveCodeMappers.size() > 1) { Map> oneToManyMappings = new HashMap<>(); Map> oneToManyMappers = new HashMap<>(); diff --git a/src/main/java/gr/uom/java/xmi/diff/UMLModelDiff.java b/src/main/java/gr/uom/java/xmi/diff/UMLModelDiff.java index d6e38bbef..64cab0430 100644 --- a/src/main/java/gr/uom/java/xmi/diff/UMLModelDiff.java +++ b/src/main/java/gr/uom/java/xmi/diff/UMLModelDiff.java @@ -4492,6 +4492,24 @@ else if(!newMappingSamePackage && oldMappingSamePackage) { return skip; } + private Set findMappersWithTheSameFragment1(Set mappings) { + Set mappers = new LinkedHashSet(); + 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 addedOperationInvocations, UMLOperationBodyMapper operationBodyMapper) throws RefactoringMinerTimedOutException { @@ -4503,6 +4521,11 @@ private void createExtractAndMoveMethodRefactoring(UMLOperation addedOperation, mapper.addChildMapper(operationBodyMapper); MappingOptimizer optimizer = new MappingOptimizer(mapper.getClassDiff()); optimizer.optimizeDuplicateMappingsForExtract(mapper, refactorings); + Set mappers = findMappersWithTheSameFragment1(operationBodyMapper.getMappings()); + if(mappers.size() > 0) { + mappers.add(operationBodyMapper); + optimizer.optimizeDuplicateMappingsForMoveCode(new ArrayList<>(mappers), refactorings); + } } private UMLOperationBodyMapper createMapperForExtractAndMove(UMLOperation addedOperation,