Search in sources :

Example 81 with ReturnStatement

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);
}
Also used : AST(org.eclipse.jdt.core.dom.AST) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup) StructuralPropertyDescriptor(org.eclipse.jdt.core.dom.StructuralPropertyDescriptor)

Example 82 with ReturnStatement

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);
            }
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) MissingReturnTypeInLambdaCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.MissingReturnTypeInLambdaCorrectionProposal) Javadoc(org.eclipse.jdt.core.dom.Javadoc) ASTRewriteCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.ASTRewriteCorrectionProposal) Type(org.eclipse.jdt.core.dom.Type) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) MissingReturnTypeCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.MissingReturnTypeCorrectionProposal) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Block(org.eclipse.jdt.core.dom.Block) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) TagElement(org.eclipse.jdt.core.dom.TagElement) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression)

Example 83 with ReturnStatement

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;
}
Also used : Statement(org.eclipse.jdt.core.dom.Statement) ThrowStatement(org.eclipse.jdt.core.dom.ThrowStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Block(org.eclipse.jdt.core.dom.Block)

Example 84 with ReturnStatement

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;
}
Also used : ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Block(org.eclipse.jdt.core.dom.Block)

Example 85 with ReturnStatement

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;
}
Also used : SimpleName(org.eclipse.jdt.core.dom.SimpleName) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess)

Aggregations

ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)85 Block (org.eclipse.jdt.core.dom.Block)53 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)46 Expression (org.eclipse.jdt.core.dom.Expression)42 ASTNode (org.eclipse.jdt.core.dom.ASTNode)39 AST (org.eclipse.jdt.core.dom.AST)36 Type (org.eclipse.jdt.core.dom.Type)33 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)28 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)27 SimpleName (org.eclipse.jdt.core.dom.SimpleName)23 Statement (org.eclipse.jdt.core.dom.Statement)23 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)22 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)20 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)19 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)18 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)17 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)16 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)15 Javadoc (org.eclipse.jdt.core.dom.Javadoc)15 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)15