Skip to content

Commit

Permalink
Fixed Migration service refactoring that return void.
Browse files Browse the repository at this point in the history
  • Loading branch information
ylussaud committed Dec 5, 2023
1 parent 2692a6c commit 9e3f12c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020 Obeo.
* Copyright (c) 2020, 2023 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand All @@ -18,10 +18,12 @@
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.ExpressionStatement;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.ReturnStatement;
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
import org.eclipse.jdt.core.dom.Statement;
import org.eclipse.jdt.core.dom.Type;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
Expand Down Expand Up @@ -86,17 +88,26 @@ public boolean visit(MethodDeclaration method) {
}

// newMethod body: return <serviceName>(...);
// or <serviceName>(...);
final Block body = newMethod.getAST().newBlock();
final ReturnStatement returnStatement = newMethod.getAST().newReturnStatement();
final MethodInvocation methodInvocation = newMethod.getAST().newMethodInvocation();
methodInvocation.setName(newMethod.getAST().newSimpleName(serviceName));
for (Object parameter : method.parameters()) {
final ASTNode argument = rewrite.createCopyTarget(((SingleVariableDeclaration)parameter)
.getName());
methodInvocation.arguments().add(argument);
}
returnStatement.setExpression(methodInvocation);
body.statements().add(returnStatement);
final Statement statement;
if (method.getReturnType2() != null && !"void".equals(method.getReturnType2().toString())) {
final ReturnStatement returnStatement = newMethod.getAST().newReturnStatement();
returnStatement.setExpression(methodInvocation);
statement = returnStatement;
} else {
final ExpressionStatement expressionStatement = newMethod.getAST().newExpressionStatement(
methodInvocation);
statement = expressionStatement;
}
body.statements().add(statement);
newMethod.setBody(body);

// add the newMethod to the containing Class
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020 Obeo.
* Copyright (c) 2020, 2023 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand All @@ -15,10 +15,12 @@
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.ExpressionStatement;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.ReturnStatement;
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
import org.eclipse.jdt.core.dom.Statement;
import org.eclipse.jdt.core.dom.Type;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
Expand Down Expand Up @@ -77,12 +79,21 @@ public boolean visit(MethodDeclaration method) {
newMethod.parameters().add(parameter);

// newMethod body: return <serviceName>();
// or <serviceName>();
final Block body = newMethod.getAST().newBlock();
final ReturnStatement returnStatement = newMethod.getAST().newReturnStatement();
final MethodInvocation methodInvocation = newMethod.getAST().newMethodInvocation();
methodInvocation.setName(newMethod.getAST().newSimpleName(serviceName));
returnStatement.setExpression(methodInvocation);
body.statements().add(returnStatement);
final Statement statement;
if (method.getReturnType2() != null && !"void".equals(method.getReturnType2().toString())) {
final ReturnStatement returnStatement = newMethod.getAST().newReturnStatement();
returnStatement.setExpression(methodInvocation);
statement = returnStatement;
} else {
final ExpressionStatement expressionStatement = newMethod.getAST().newExpressionStatement(
methodInvocation);
statement = expressionStatement;
}
body.statements().add(statement);
newMethod.setBody(body);

// add the newMethod to the containing Class
Expand Down

0 comments on commit 9e3f12c

Please sign in to comment.