From 87b0460ba999c22ff28e11995e3912d32a57d287 Mon Sep 17 00:00:00 2001 From: tsantalis Date: Fri, 27 Dec 2024 18:10:31 -0500 Subject: [PATCH] Enable detection of overlapping Extract Variable within composite https://github.com/spring-projects/spring-framework/commit/5dc6a16c0b7ad2431a01c33885c1bf21442ff318 --- .../decomposition/AbstractCodeMapping.java | 41 ++++++++++++++----- .../decomposition/UMLOperationBodyMapper.java | 10 +++++ 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/src/main/java/gr/uom/java/xmi/decomposition/AbstractCodeMapping.java b/src/main/java/gr/uom/java/xmi/decomposition/AbstractCodeMapping.java index cd35e9ad2e..e5961638ea 100644 --- a/src/main/java/gr/uom/java/xmi/decomposition/AbstractCodeMapping.java +++ b/src/main/java/gr/uom/java/xmi/decomposition/AbstractCodeMapping.java @@ -711,18 +711,37 @@ else if(stringConcatMatch(initializer, before)) { } if(variableName.equals(after) && statement.getPatternInstanceofExpressions().size() > 0) { for(LeafExpression expression2 : statement.getPatternInstanceofExpressions()) { - if(expression2.getString().endsWith(" " + variableName) && expression2.getString().contains(before)) { - ExtractVariableRefactoring ref = new ExtractVariableRefactoring(declaration, operation1, operation2, insideExtractedOrInlinedMethod); - List expressions1 = fragment1.findExpression(before); - if(expressions1.size() > 0) { - LeafMapping leafMapping = new LeafMapping(expressions1.get(0), expression2, operation1, operation2); - ref.addSubExpressionMapping(leafMapping); + String beforeWithoutCast = null; + if(before.startsWith("(") && before.contains(")")) { + beforeWithoutCast = before.substring(before.indexOf(")") + 1); + } + if(expression2.getString().endsWith(" " + variableName)) { + if(expression2.getString().contains(before)) { + ExtractVariableRefactoring ref = new ExtractVariableRefactoring(declaration, operation1, operation2, insideExtractedOrInlinedMethod); + List expressions1 = fragment1.findExpression(before); + for(LeafExpression expression1 : expressions1) { + LeafMapping leafMapping = new LeafMapping(expression1, expression2, operation1, operation2); + ref.addSubExpressionMapping(leafMapping); + } + processExtractVariableRefactoring(ref, refactorings); + //if(identical()) { + identicalWithInlinedVariable = true; + //} + return; + } + else if(beforeWithoutCast != null && expression2.getString().contains(beforeWithoutCast)) { + ExtractVariableRefactoring ref = new ExtractVariableRefactoring(declaration, operation1, operation2, insideExtractedOrInlinedMethod); + List expressions1 = fragment1.findExpression(beforeWithoutCast); + for(LeafExpression expression1 : expressions1) { + LeafMapping leafMapping = new LeafMapping(expression1, expression2, operation1, operation2); + ref.addSubExpressionMapping(leafMapping); + } + processExtractVariableRefactoring(ref, refactorings); + //if(identical()) { + identicalWithInlinedVariable = true; + //} + return; } - processExtractVariableRefactoring(ref, refactorings); - //if(identical()) { - identicalWithInlinedVariable = true; - //} - return; } } } diff --git a/src/main/java/gr/uom/java/xmi/decomposition/UMLOperationBodyMapper.java b/src/main/java/gr/uom/java/xmi/decomposition/UMLOperationBodyMapper.java index bf0254c49f..763a78538a 100644 --- a/src/main/java/gr/uom/java/xmi/decomposition/UMLOperationBodyMapper.java +++ b/src/main/java/gr/uom/java/xmi/decomposition/UMLOperationBodyMapper.java @@ -4730,6 +4730,11 @@ else if(replacements.size() == 1 && replacementInfo.getReplacements(ReplacementT } } } + for(AbstractExpression expression : statement2.getExpressions()) { + if(expression.getPatternInstanceofExpressions().size() > 0) { + mapping.temporaryVariableAssignment(expression, leaves2, classDiff, parentMapper != null, mappings); + } + } for(AbstractCodeFragment leaf : leaves1) { if(leaf.getLocationInfo().before(mapping.getFragment1().getLocationInfo())) { mapping.inlinedVariableAssignment(leaf, leaves2, classDiff, parentMapper != null); @@ -5080,6 +5085,11 @@ else if(replacements.size() == 1 && replacementInfo.getReplacements(ReplacementT } } } + for(AbstractExpression expression : statement2.getExpressions()) { + if(expression.getPatternInstanceofExpressions().size() > 0) { + mapping.temporaryVariableAssignment(expression, leaves2, classDiff, parentMapper != null, mappings); + } + } for(AbstractCodeFragment leaf : leaves1) { if(leaf.getLocationInfo().before(mapping.getFragment1().getLocationInfo())) { mapping.inlinedVariableAssignment(leaf, leaves2, classDiff, parentMapper != null);