From 181693790e43d016bfcd176268cc95b22b954042 Mon Sep 17 00:00:00 2001 From: tsantalis Date: Fri, 4 Oct 2024 21:45:10 -0400 Subject: [PATCH] Support SwitchExpression by representing switch cases as lambdas --- .../decomposition/LambdaExpressionObject.java | 16 ++++++++++++++++ .../gr/uom/java/xmi/decomposition/Visitor.java | 18 ++++++++++++++++++ 2 files changed, 34 insertions(+) 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 6379b8f6a..63c679154 100644 --- a/src/main/java/gr/uom/java/xmi/decomposition/LambdaExpressionObject.java +++ b/src/main/java/gr/uom/java/xmi/decomposition/LambdaExpressionObject.java @@ -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; @@ -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(); diff --git a/src/main/java/gr/uom/java/xmi/decomposition/Visitor.java b/src/main/java/gr/uom/java/xmi/decomposition/Visitor.java index 5272cb40a..9925d6dad 100644 --- a/src/main/java/gr/uom/java/xmi/decomposition/Visitor.java +++ b/src/main/java/gr/uom/java/xmi/decomposition/Visitor.java @@ -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; @@ -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; @@ -850,6 +853,21 @@ public boolean visit(LambdaExpression node) { return false; } + public boolean visit(SwitchExpression node) { + List 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);