Skip to content

Commit

Permalink
Support SwitchExpression by representing switch cases as lambdas
Browse files Browse the repository at this point in the history
  • Loading branch information
tsantalis committed Oct 5, 2024
1 parent 55178d8 commit 1816937
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.eclipse.jdt.core.dom.ExpressionMethodReference;
import org.eclipse.jdt.core.dom.LambdaExpression;
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
import org.eclipse.jdt.core.dom.Statement;
import org.eclipse.jdt.core.dom.SuperMethodReference;
import org.eclipse.jdt.core.dom.Type;
import org.eclipse.jdt.core.dom.TypeMethodReference;
Expand Down Expand Up @@ -81,6 +82,21 @@ else if(lambda.getBody() instanceof Expression) {
}
}

public LambdaExpressionObject(CompilationUnit cu, String filePath, Statement switchCaseBody, VariableDeclarationContainer owner) {
this.owner = owner;
this.hasParentheses = false;
this.asString = switchCaseBody.toString();
this.locationInfo = new LocationInfo(cu, filePath, switchCaseBody, CodeElementType.LAMBDA_EXPRESSION);
if(switchCaseBody instanceof Block) {
this.body = new OperationBody(cu, filePath, (Block)switchCaseBody, this, new ArrayList<>());
}
else {
//TODO find a way to support switch-case with a single statement
//this.expression = new AbstractExpression(cu, filePath, (Expression)lambda.getBody(), CodeElementType.LAMBDA_EXPRESSION_BODY, this);
//this.expression.setLambdaOwner(this);
}
}

public LambdaExpressionObject(CompilationUnit cu, String filePath, ExpressionMethodReference reference, VariableDeclarationContainer owner) {
this.owner = owner;
this.asString = reference.toString();
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/gr/uom/java/xmi/decomposition/Visitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.eclipse.jdt.core.dom.ArrayInitializer;
import org.eclipse.jdt.core.dom.ArrayType;
import org.eclipse.jdt.core.dom.Assignment;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.BodyDeclaration;
import org.eclipse.jdt.core.dom.BooleanLiteral;
import org.eclipse.jdt.core.dom.CastExpression;
Expand Down Expand Up @@ -48,10 +49,12 @@
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.SimpleType;
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
import org.eclipse.jdt.core.dom.Statement;
import org.eclipse.jdt.core.dom.StringLiteral;
import org.eclipse.jdt.core.dom.SuperConstructorInvocation;
import org.eclipse.jdt.core.dom.SuperMethodInvocation;
import org.eclipse.jdt.core.dom.SuperMethodReference;
import org.eclipse.jdt.core.dom.SwitchExpression;
import org.eclipse.jdt.core.dom.TextBlock;
import org.eclipse.jdt.core.dom.ThisExpression;
import org.eclipse.jdt.core.dom.Type;
Expand Down Expand Up @@ -850,6 +853,21 @@ public boolean visit(LambdaExpression node) {
return false;
}

public boolean visit(SwitchExpression node) {
List<Statement> statements = node.statements();
for(Statement statement : statements) {
if(statement instanceof Block) {
LambdaExpressionObject lambda = new LambdaExpressionObject(cu, filePath, statement, container);
lambdas.add(lambda);
if(current.getUserObject() != null) {
AnonymousClassDeclarationObject anonymous = (AnonymousClassDeclarationObject)current.getUserObject();
anonymous.getLambdas().add(lambda);
}
}
}
return false;
}

public boolean visit(ParenthesizedExpression node) {
LeafExpression expression = new LeafExpression(cu, filePath, node, CodeElementType.PARENTHESIZED_EXPRESSION, container);
parenthesizedExpressions.add(expression);
Expand Down

0 comments on commit 1816937

Please sign in to comment.