use of org.eclipse.jdt.core.dom.ReturnStatement in project eclipse.jdt.ls by eclipse.
the class ExtractTempRefactoring method insertAt.
private void insertAt(ASTNode target, Statement declaration) {
ASTRewrite rewrite = fCURewrite.getASTRewrite();
TextEditGroup groupDescription = fCURewrite.createGroupDescription(RefactoringCoreMessages.ExtractTempRefactoring_declare_local_variable);
ASTNode parent = target.getParent();
StructuralPropertyDescriptor locationInParent = target.getLocationInParent();
while (locationInParent != Block.STATEMENTS_PROPERTY && locationInParent != SwitchStatement.STATEMENTS_PROPERTY) {
if (locationInParent == IfStatement.THEN_STATEMENT_PROPERTY || locationInParent == IfStatement.ELSE_STATEMENT_PROPERTY || locationInParent == ForStatement.BODY_PROPERTY || locationInParent == EnhancedForStatement.BODY_PROPERTY || locationInParent == DoStatement.BODY_PROPERTY || locationInParent == WhileStatement.BODY_PROPERTY) {
// create intermediate block if target was the body property of a control statement:
Block replacement = rewrite.getAST().newBlock();
ListRewrite replacementRewrite = rewrite.getListRewrite(replacement, Block.STATEMENTS_PROPERTY);
replacementRewrite.insertFirst(declaration, null);
replacementRewrite.insertLast(rewrite.createMoveTarget(target), null);
rewrite.replace(target, replacement, groupDescription);
return;
} else if (locationInParent == LambdaExpression.BODY_PROPERTY && ((LambdaExpression) parent).getBody() instanceof Expression) {
Block replacement = rewrite.getAST().newBlock();
ListRewrite replacementRewrite = rewrite.getListRewrite(replacement, Block.STATEMENTS_PROPERTY);
replacementRewrite.insertFirst(declaration, null);
ASTNode moveTarget = rewrite.createMoveTarget(target);
AST ast = rewrite.getAST();
if (Bindings.isVoidType(((LambdaExpression) parent).resolveMethodBinding().getReturnType())) {
ExpressionStatement expressionStatement = ast.newExpressionStatement((Expression) moveTarget);
moveTarget = expressionStatement;
} else {
ReturnStatement returnStatement = ast.newReturnStatement();
returnStatement.setExpression((Expression) moveTarget);
moveTarget = returnStatement;
}
replacementRewrite.insertLast(moveTarget, null);
rewrite.replace(target, replacement, groupDescription);
return;
}
target = parent;
parent = parent.getParent();
locationInParent = target.getLocationInParent();
}
ListRewrite listRewrite = rewrite.getListRewrite(parent, (ChildListPropertyDescriptor) locationInParent);
listRewrite.insertBefore(declaration, target, groupDescription);
}
use of org.eclipse.jdt.core.dom.ReturnStatement in project eclipse.jdt.ls by eclipse.
the class ReturnTypeSubProcessor method addMissingReturnStatementProposals.
public static void addMissingReturnStatementProposals(IInvocationContext context, IProblemLocationCore problem, Collection<ChangeCorrectionProposal> proposals) {
ICompilationUnit cu = context.getCompilationUnit();
ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
if (selectedNode == null) {
return;
}
ReturnStatement existingStatement = (selectedNode instanceof ReturnStatement) ? (ReturnStatement) selectedNode : null;
// Lambda Expression can be in a MethodDeclaration or a Field Declaration
if (selectedNode instanceof LambdaExpression) {
MissingReturnTypeInLambdaCorrectionProposal proposal = new MissingReturnTypeInLambdaCorrectionProposal(cu, (LambdaExpression) selectedNode, existingStatement, IProposalRelevance.MISSING_RETURN_TYPE);
proposals.add(proposal);
} else {
BodyDeclaration decl = ASTResolving.findParentBodyDeclaration(selectedNode);
if (decl instanceof MethodDeclaration) {
MethodDeclaration methodDecl = (MethodDeclaration) decl;
Block block = methodDecl.getBody();
if (block == null) {
return;
}
proposals.add(new MissingReturnTypeCorrectionProposal(cu, methodDecl, existingStatement, IProposalRelevance.MISSING_RETURN_TYPE));
Type returnType = methodDecl.getReturnType2();
if (returnType != null && !"void".equals(ASTNodes.asString(returnType))) {
// $NON-NLS-1$
AST ast = methodDecl.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
rewrite.replace(returnType, ast.newPrimitiveType(PrimitiveType.VOID), null);
Javadoc javadoc = methodDecl.getJavadoc();
if (javadoc != null) {
TagElement tagElement = JavadocTagsSubProcessor.findTag(javadoc, TagElement.TAG_RETURN, null);
if (tagElement != null) {
rewrite.remove(tagElement, null);
}
}
String label = CorrectionMessages.ReturnTypeSubProcessor_changetovoid_description;
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, CodeActionKind.QuickFix, cu, rewrite, IProposalRelevance.CHANGE_RETURN_TYPE_TO_VOID);
proposals.add(proposal);
}
}
}
}
use of org.eclipse.jdt.core.dom.ReturnStatement in project eclipse.jdt.ls by eclipse.
the class QuickAssistProcessor method getBlockBodyForLambda.
private static Block getBlockBodyForLambda(Expression bodyExpr, ITypeBinding returnTypeBinding, AST ast) {
Statement statementInBlockBody;
if (ast.resolveWellKnownType("void").isEqualTo(returnTypeBinding)) {
// $NON-NLS-1$
ExpressionStatement expressionStatement = ast.newExpressionStatement(bodyExpr);
statementInBlockBody = expressionStatement;
} else {
ReturnStatement returnStatement = ast.newReturnStatement();
returnStatement.setExpression(bodyExpr);
statementInBlockBody = returnStatement;
}
Block blockBody = ast.newBlock();
blockBody.statements().add(statementInBlockBody);
return blockBody;
}
use of org.eclipse.jdt.core.dom.ReturnStatement in project eclipse.jdt.ls by eclipse.
the class QuickAssistProcessor method getNewReturnBlock.
private static Block getNewReturnBlock(AST ast, ITypeBinding returnTypeBinding) {
Block newBlock = ast.newBlock();
if (!"void".equals(returnTypeBinding.getName())) {
// $NON-NLS-1$
ReturnStatement newReturnStatement = ast.newReturnStatement();
String bName = returnTypeBinding.getBinaryName();
if ("Z".equals(bName)) {
// $NON-NLS-1$
newReturnStatement.setExpression(ast.newBooleanLiteral(false));
} else if (returnTypeBinding.isPrimitive()) {
newReturnStatement.setExpression(ast.newNumberLiteral());
} else if ("java.lang.String".equals(bName)) {
// $NON-NLS-1$
newReturnStatement.setExpression(ast.newStringLiteral());
} else {
newReturnStatement.setExpression(ast.newNullLiteral());
}
newBlock.statements().add(newReturnStatement);
}
return newBlock;
}
use of org.eclipse.jdt.core.dom.ReturnStatement in project jbosstools-hibernate by jbosstools.
the class CollectEntityInfo method getReturnIdentifier.
public static String getReturnIdentifier(MethodDeclaration node) {
String res = null;
if (node.getBody() == null) {
return res;
}
List<?> bodyStatemants = node.getBody().statements();
Iterator<?> it = bodyStatemants.iterator();
for (; it.hasNext(); ) {
Object obj = it.next();
if (obj instanceof ReturnStatement) {
ReturnStatement ret_statement = (ReturnStatement) obj;
obj = ret_statement.getExpression();
if (obj instanceof SimpleName) {
SimpleName sn = (SimpleName) obj;
res = sn.getIdentifier();
} else if (obj instanceof FieldAccess) {
FieldAccess fa = (FieldAccess) obj;
SimpleName sn = fa.getName();
res = sn.getIdentifier();
}
break;
}
}
return res;
}
Aggregations