From 6941fa016cb951ad9083a012ebb9242df8ac5d8f Mon Sep 17 00:00:00 2001 From: tsantalis Date: Sun, 18 Feb 2024 09:01:37 -0500 Subject: [PATCH] Support matching of field declaration initializer moved to constructor SpawnBalancer https://github.com/addthis/hydra/commit/7fea4c9d5ee97d4a61ad985cadc9c5c0ab2db780#diff-9f268c6081f4e659aa57c3b5d2c2ced5d7d893e84a5b2a81ecf12b28279c9030 --- .../decomposition/AbstractCodeFragment.java | 7 +++++++ .../decomposition/LambdaExpressionObject.java | 13 ++++++++++++ .../decomposition/UMLOperationBodyMapper.java | 21 +++++++++++++++++++ .../uom/java/xmi/diff/UMLAttributeDiff.java | 18 ++++++++++++++++ 4 files changed, 59 insertions(+) diff --git a/src/main/java/gr/uom/java/xmi/decomposition/AbstractCodeFragment.java b/src/main/java/gr/uom/java/xmi/decomposition/AbstractCodeFragment.java index 6235040e3f..ae81f6552c 100644 --- a/src/main/java/gr/uom/java/xmi/decomposition/AbstractCodeFragment.java +++ b/src/main/java/gr/uom/java/xmi/decomposition/AbstractCodeFragment.java @@ -185,6 +185,13 @@ public List findExpression(String s) { locations.add(expression.getLocationInfo()); } } + for(LambdaExpressionObject expression : getLambdas()) { + if(expression.getString().equals(s)) { + if(!locations.contains(expression.getLocationInfo())) + matchingExpressions.add(expression.asLeafExpression()); + locations.add(expression.getLocationInfo()); + } + } for(LeafExpression expression : getArguments()) { if(expression.getString().equals(s)) { if(!locations.contains(expression.getLocationInfo())) diff --git a/src/main/java/gr/uom/java/xmi/decomposition/LambdaExpressionObject.java b/src/main/java/gr/uom/java/xmi/decomposition/LambdaExpressionObject.java index d64c6a8c97..9a92011a34 100644 --- a/src/main/java/gr/uom/java/xmi/decomposition/LambdaExpressionObject.java +++ b/src/main/java/gr/uom/java/xmi/decomposition/LambdaExpressionObject.java @@ -40,9 +40,11 @@ public class LambdaExpressionObject implements VariableDeclarationContainer, Loc private List umlParameters = new ArrayList(); private boolean hasParentheses = false; private VariableDeclarationContainer owner; + private String asString; public LambdaExpressionObject(CompilationUnit cu, String filePath, LambdaExpression lambda, VariableDeclarationContainer owner) { this.owner = owner; + this.asString = lambda.toString(); this.locationInfo = new LocationInfo(cu, filePath, lambda, CodeElementType.LAMBDA_EXPRESSION); this.hasParentheses = lambda.hasParentheses(); List params = lambda.parameters(); @@ -79,18 +81,21 @@ else if(lambda.getBody() instanceof Expression) { public LambdaExpressionObject(CompilationUnit cu, String filePath, ExpressionMethodReference reference, VariableDeclarationContainer owner) { this.owner = owner; + this.asString = reference.toString(); this.locationInfo = new LocationInfo(cu, filePath, reference, CodeElementType.LAMBDA_EXPRESSION); this.expression = new AbstractExpression(cu, filePath, reference, CodeElementType.LAMBDA_EXPRESSION_BODY, this); } public LambdaExpressionObject(CompilationUnit cu, String filePath, SuperMethodReference reference, VariableDeclarationContainer owner) { this.owner = owner; + this.asString = reference.toString(); this.locationInfo = new LocationInfo(cu, filePath, reference, CodeElementType.LAMBDA_EXPRESSION); this.expression = new AbstractExpression(cu, filePath, reference, CodeElementType.LAMBDA_EXPRESSION_BODY, this); } public LambdaExpressionObject(CompilationUnit cu, String filePath, TypeMethodReference reference, VariableDeclarationContainer owner) { this.owner = owner; + this.asString = reference.toString(); this.locationInfo = new LocationInfo(cu, filePath, reference, CodeElementType.LAMBDA_EXPRESSION); this.expression = new AbstractExpression(cu, filePath, reference, CodeElementType.LAMBDA_EXPRESSION_BODY, this); } @@ -212,6 +217,14 @@ else if(body != null) { return sb.toString(); } + public String getString() { + return asString; + } + + public LeafExpression asLeafExpression() { + return new LeafExpression(getString(), getLocationInfo()); + } + @Override public List getParameterDeclarationList() { return getParameters(); 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 8afc0e4d3a..a46e036ebc 100644 --- a/src/main/java/gr/uom/java/xmi/decomposition/UMLOperationBodyMapper.java +++ b/src/main/java/gr/uom/java/xmi/decomposition/UMLOperationBodyMapper.java @@ -954,6 +954,27 @@ private boolean nestedUnderSplitConditional(AbstractCodeMapping mapping) { return false; } + public UMLOperationBodyMapper(AbstractCodeFragment fragment1, AbstractCodeFragment fragment2, + VariableDeclarationContainer container1, VariableDeclarationContainer container2, + UMLAbstractClassDiff classDiff, UMLModelDiff modelDiff) throws RefactoringMinerTimedOutException { + this.classDiff = classDiff; + this.modelDiff = modelDiff; + this.container1 = container1; + this.container2 = container2; + this.mappings = new LinkedHashSet(); + this.nonMappedLeavesT1 = new ArrayList(); + this.nonMappedLeavesT2 = new ArrayList(); + this.nonMappedInnerNodesT1 = new ArrayList(); + this.nonMappedInnerNodesT2 = new ArrayList(); + if(fragment1 != null && fragment2 != null) { + List leaves1 = new ArrayList(); + leaves1.add(fragment1); + List leaves2 = new ArrayList(); + leaves2.add(fragment2); + processLeaves(leaves1, leaves2, new LinkedHashMap(), false); + } + } + public UMLOperationBodyMapper(UMLAttribute removedAttribute, UMLAttribute addedAttribute, UMLAbstractClassDiff classDiff, UMLModelDiff modelDiff) throws RefactoringMinerTimedOutException { this.classDiff = classDiff; this.modelDiff = modelDiff; diff --git a/src/main/java/gr/uom/java/xmi/diff/UMLAttributeDiff.java b/src/main/java/gr/uom/java/xmi/diff/UMLAttributeDiff.java index 818323cd27..12faaf35c3 100644 --- a/src/main/java/gr/uom/java/xmi/diff/UMLAttributeDiff.java +++ b/src/main/java/gr/uom/java/xmi/diff/UMLAttributeDiff.java @@ -16,8 +16,10 @@ import gr.uom.java.xmi.UMLAttribute; import gr.uom.java.xmi.UMLOperation; import gr.uom.java.xmi.UMLParameter; +import gr.uom.java.xmi.decomposition.AbstractCodeFragment; import gr.uom.java.xmi.decomposition.AbstractCodeMapping; import gr.uom.java.xmi.decomposition.AbstractExpression; +import gr.uom.java.xmi.decomposition.LeafExpression; import gr.uom.java.xmi.decomposition.UMLOperationBodyMapper; import gr.uom.java.xmi.decomposition.VariableDeclaration; import gr.uom.java.xmi.decomposition.VariableReferenceExtractor; @@ -131,6 +133,22 @@ else if(initializer1 == null && initializer2 != null) { } else if(initializer1 != null && initializer2 == null) { initializerChanged = true; + for(UMLOperationBodyMapper operationBodyMapper : operationBodyMapperList) { + if(operationBodyMapper.getContainer1().isConstructor() && operationBodyMapper.getContainer2().isConstructor()) { + for(AbstractCodeFragment fragment2 : operationBodyMapper.getNonMappedLeavesT2()) { + String fragment = fragment2.getString(); + if((fragment.startsWith(removedAttribute.getName() + "=") || + fragment.startsWith("this." + removedAttribute.getName() + "=")) && + fragment.endsWith(";\n")) { + String variableInitializer = fragment.substring(fragment.indexOf("=")+1, fragment.lastIndexOf(";\n")); + List leafExpressions2 = fragment2.findExpression(variableInitializer); + if(leafExpressions2.size() == 1) { + this.mapper = new UMLOperationBodyMapper(initializer1, leafExpressions2.get(0), operationBodyMapper.getContainer1(), operationBodyMapper.getContainer2(), classDiff, modelDiff); + } + } + } + } + } } }