Skip to content

Commit

Permalink
Support matching of field declaration initializer moved to constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
tsantalis committed Feb 18, 2024
1 parent 819b5b2 commit 6941fa0
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,13 @@ public List<LeafExpression> 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()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ public class LambdaExpressionObject implements VariableDeclarationContainer, Loc
private List<UMLParameter> umlParameters = new ArrayList<UMLParameter>();
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<org.eclipse.jdt.core.dom.VariableDeclaration> params = lambda.parameters();
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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<VariableDeclaration> getParameterDeclarationList() {
return getParameters();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<AbstractCodeMapping>();
this.nonMappedLeavesT1 = new ArrayList<AbstractCodeFragment>();
this.nonMappedLeavesT2 = new ArrayList<AbstractCodeFragment>();
this.nonMappedInnerNodesT1 = new ArrayList<CompositeStatementObject>();
this.nonMappedInnerNodesT2 = new ArrayList<CompositeStatementObject>();
if(fragment1 != null && fragment2 != null) {
List<AbstractCodeFragment> leaves1 = new ArrayList<AbstractCodeFragment>();
leaves1.add(fragment1);
List<AbstractCodeFragment> leaves2 = new ArrayList<AbstractCodeFragment>();
leaves2.add(fragment2);
processLeaves(leaves1, leaves2, new LinkedHashMap<String, String>(), false);
}
}

public UMLOperationBodyMapper(UMLAttribute removedAttribute, UMLAttribute addedAttribute, UMLAbstractClassDiff classDiff, UMLModelDiff modelDiff) throws RefactoringMinerTimedOutException {
this.classDiff = classDiff;
this.modelDiff = modelDiff;
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/gr/uom/java/xmi/diff/UMLAttributeDiff.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<LeafExpression> leafExpressions2 = fragment2.findExpression(variableInitializer);
if(leafExpressions2.size() == 1) {
this.mapper = new UMLOperationBodyMapper(initializer1, leafExpressions2.get(0), operationBodyMapper.getContainer1(), operationBodyMapper.getContainer2(), classDiff, modelDiff);
}
}
}
}
}
}
}

Expand Down

0 comments on commit 6941fa0

Please sign in to comment.