Search in sources :

Example 11 with ReturnStatement

use of org.eclipse.jdt.core.dom.ReturnStatement in project flux by eclipse.

the class QuickAssistProcessor method getChangeLambdaBodyToExpressionProposal.

private static boolean getChangeLambdaBodyToExpressionProposal(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) {
    LambdaExpression lambda;
    if (covering instanceof LambdaExpression) {
        lambda = (LambdaExpression) covering;
    } else if (covering.getLocationInParent() == LambdaExpression.BODY_PROPERTY) {
        lambda = (LambdaExpression) covering.getParent();
    } else {
        return false;
    }
    if (!(lambda.getBody() instanceof Block))
        return false;
    Block lambdaBody = (Block) lambda.getBody();
    if (lambdaBody.statements().size() != 1)
        return false;
    Expression exprBody;
    Statement singleStatement = (Statement) lambdaBody.statements().get(0);
    if (singleStatement instanceof ReturnStatement) {
        Expression returnExpr = ((ReturnStatement) singleStatement).getExpression();
        if (returnExpr == null)
            return false;
        exprBody = returnExpr;
    } else if (singleStatement instanceof ExpressionStatement) {
        Expression expression = ((ExpressionStatement) singleStatement).getExpression();
        if (isValidExpressionBody(expression)) {
            exprBody = expression;
        } else {
            return false;
        }
    } else {
        return false;
    }
    if (resultingCollections == null)
        return true;
    AST ast = lambda.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    Expression movedBody = (Expression) rewrite.createMoveTarget(exprBody);
    rewrite.set(lambda, LambdaExpression.BODY_PROPERTY, movedBody, null);
    // add proposal
    String label = CorrectionMessages.QuickAssistProcessor_change_lambda_body_to_expression;
    //		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.CHANGE_LAMBDA_BODY_TO_EXPRESSION);
    resultingCollections.add(proposal);
    return true;
}
Also used : ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) AST(org.eclipse.jdt.core.dom.AST) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) 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) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression)

Example 12 with ReturnStatement

use of org.eclipse.jdt.core.dom.ReturnStatement in project flux by eclipse.

the class AdvancedQuickAssistProcessor method getIfReturnIntoIfElseAtEndOfVoidMethodProposals.

private static boolean getIfReturnIntoIfElseAtEndOfVoidMethodProposals(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) {
    if (!(covering instanceof IfStatement)) {
        return false;
    }
    IfStatement ifStatement = (IfStatement) covering;
    if (ifStatement.getElseStatement() != null) {
        return false;
    }
    // 'then' block should have 'return' as last statement
    Statement thenStatement = ifStatement.getThenStatement();
    if (!(thenStatement instanceof Block)) {
        return false;
    }
    Block thenBlock = (Block) thenStatement;
    List<Statement> thenStatements = thenBlock.statements();
    if (thenStatements.isEmpty() || !(thenStatements.get(thenStatements.size() - 1) instanceof ReturnStatement)) {
        return false;
    }
    // method should return 'void'
    MethodDeclaration coveringMetod = ASTResolving.findParentMethodDeclaration(covering);
    if (coveringMetod == null) {
        return false;
    }
    Type returnType = coveringMetod.getReturnType2();
    if (!isVoid(returnType)) {
        return false;
    }
    //
    List<Statement> statements = coveringMetod.getBody().statements();
    int ifIndex = statements.indexOf(ifStatement);
    if (ifIndex == -1) {
        return false;
    }
    //  we could produce quick assist
    if (resultingCollections == null) {
        return true;
    }
    //
    AST ast = covering.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    // remove last 'return' in 'then' block
    ListRewrite listRewriter = rewrite.getListRewrite(thenBlock, (ChildListPropertyDescriptor) ifStatement.getLocationInParent());
    listRewriter.remove(thenStatements.get(thenStatements.size() - 1), null);
    // prepare original nodes
    Expression conditionPlaceholder = (Expression) rewrite.createMoveTarget(ifStatement.getExpression());
    Statement thenPlaceholder = (Statement) rewrite.createMoveTarget(ifStatement.getThenStatement());
    // prepare 'else' block
    Block elseBlock = ast.newBlock();
    for (int i = ifIndex + 1; i < statements.size(); i++) {
        Statement statement = statements.get(i);
        elseBlock.statements().add(rewrite.createMoveTarget(statement));
    }
    // prepare new 'if' statement
    IfStatement newIf = ast.newIfStatement();
    newIf.setExpression(conditionPlaceholder);
    newIf.setThenStatement(thenPlaceholder);
    newIf.setElseStatement(elseBlock);
    rewrite.replace(ifStatement, newIf, null);
    // add correction proposal
    String label = CorrectionMessages.AdvancedQuickAssistProcessor_convertToIfElse_description;
    //		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.CONVERT_TO_IF_ELSE);
    resultingCollections.add(proposal);
    return true;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) ContinueStatement(org.eclipse.jdt.core.dom.ContinueStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) AssertStatement(org.eclipse.jdt.core.dom.AssertStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) IfStatement(org.eclipse.jdt.core.dom.IfStatement) Type(org.eclipse.jdt.core.dom.Type) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Block(org.eclipse.jdt.core.dom.Block) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)

Example 13 with ReturnStatement

use of org.eclipse.jdt.core.dom.ReturnStatement in project flux by eclipse.

the class AdvancedQuickAssistProcessor method hasStopAsLastExecutableStatement.

private static boolean hasStopAsLastExecutableStatement(Statement lastStatement) {
    if (lastStatement instanceof ReturnStatement || lastStatement instanceof BreakStatement) {
        return true;
    }
    if (lastStatement instanceof Block) {
        Block block = (Block) lastStatement;
        lastStatement = (Statement) block.statements().get(block.statements().size() - 1);
        return hasStopAsLastExecutableStatement(lastStatement);
    }
    return false;
}
Also used : BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Block(org.eclipse.jdt.core.dom.Block)

Example 14 with ReturnStatement

use of org.eclipse.jdt.core.dom.ReturnStatement in project flux by eclipse.

the class ModifierChangeCorrectionProposal method getRewrite.

@Override
protected ASTRewrite getRewrite() throws CoreException {
    CompilationUnit astRoot = ASTResolving.findParentCompilationUnit(fNode);
    ASTNode boundNode = astRoot.findDeclaringNode(fBinding);
    ASTNode declNode = null;
    if (boundNode != null) {
        // is same CU
        declNode = boundNode;
    } else {
        //setSelectionDescription(selectionDescription);
        CompilationUnit newRoot = ASTResolving.createQuickFixAST(getCompilationUnit(), null);
        declNode = newRoot.findDeclaringNode(fBinding.getKey());
    }
    if (declNode != null) {
        AST ast = declNode.getAST();
        ASTRewrite rewrite = ASTRewrite.create(ast);
        if (declNode.getNodeType() == ASTNode.VARIABLE_DECLARATION_FRAGMENT) {
            VariableDeclarationFragment fragment = (VariableDeclarationFragment) declNode;
            ASTNode parent = declNode.getParent();
            if (parent instanceof FieldDeclaration) {
                FieldDeclaration fieldDecl = (FieldDeclaration) parent;
                if (fieldDecl.fragments().size() > 1 && (fieldDecl.getParent() instanceof AbstractTypeDeclaration)) {
                    // split
                    VariableDeclarationRewrite.rewriteModifiers(fieldDecl, new VariableDeclarationFragment[] { fragment }, fIncludedModifiers, fExcludedModifiers, rewrite, null);
                    return rewrite;
                }
            } else if (parent instanceof VariableDeclarationStatement) {
                VariableDeclarationStatement varDecl = (VariableDeclarationStatement) parent;
                if (varDecl.fragments().size() > 1 && (varDecl.getParent() instanceof Block)) {
                    // split
                    VariableDeclarationRewrite.rewriteModifiers(varDecl, new VariableDeclarationFragment[] { fragment }, fIncludedModifiers, fExcludedModifiers, rewrite, null);
                    return rewrite;
                }
            } else if (parent instanceof VariableDeclarationExpression) {
            // can't separate
            }
            declNode = parent;
        } else if (declNode.getNodeType() == ASTNode.METHOD_DECLARATION) {
            MethodDeclaration methodDecl = (MethodDeclaration) declNode;
            if (!methodDecl.isConstructor()) {
                IMethodBinding methodBinding = methodDecl.resolveBinding();
                if (methodDecl.getBody() == null && methodBinding != null && Modifier.isAbstract(methodBinding.getModifiers()) && Modifier.isStatic(fIncludedModifiers)) {
                    // add body
                    ICompilationUnit unit = getCompilationUnit();
                    String delimiter = unit.findRecommendedLineSeparator();
                    //$NON-NLS-1$
                    String bodyStatement = "";
                    Block body = ast.newBlock();
                    rewrite.set(methodDecl, MethodDeclaration.BODY_PROPERTY, body, null);
                    Type returnType = methodDecl.getReturnType2();
                    if (returnType != null) {
                        Expression expression = ASTNodeFactory.newDefaultExpression(ast, returnType, methodDecl.getExtraDimensions());
                        if (expression != null) {
                            ReturnStatement returnStatement = ast.newReturnStatement();
                            returnStatement.setExpression(expression);
                            bodyStatement = ASTNodes.asFormattedString(returnStatement, 0, delimiter, unit.getJavaProject().getOptions(true));
                        }
                    }
                    String placeHolder = CodeGeneration.getMethodBodyContent(unit, methodBinding.getDeclaringClass().getName(), methodBinding.getName(), false, bodyStatement, delimiter);
                    if (placeHolder != null) {
                        ReturnStatement todoNode = (ReturnStatement) rewrite.createStringPlaceholder(placeHolder, ASTNode.RETURN_STATEMENT);
                        body.statements().add(todoNode);
                    }
                }
            }
        }
        ModifierRewrite listRewrite = ModifierRewrite.create(rewrite, declNode);
        PositionInformation trackedDeclNode = listRewrite.setModifiers(fIncludedModifiers, fExcludedModifiers, null);
        //$NON-NLS-1$
        LinkedProposalPositionGroup positionGroup = new LinkedProposalPositionGroup("group");
        positionGroup.addPosition(trackedDeclNode);
        getLinkedProposalModel().addPositionGroup(positionGroup);
        if (boundNode != null) {
            // only set end position if in same CU
            setEndPosition(rewrite.track(fNode));
        }
        return rewrite;
    }
    return null;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ModifierRewrite(org.eclipse.jdt.internal.corext.dom.ModifierRewrite) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PositionInformation(org.eclipse.jdt.internal.corext.fix.LinkedProposalPositionGroup.PositionInformation) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) Type(org.eclipse.jdt.core.dom.Type) Expression(org.eclipse.jdt.core.dom.Expression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) Block(org.eclipse.jdt.core.dom.Block) LinkedProposalPositionGroup(org.eclipse.jdt.internal.corext.fix.LinkedProposalPositionGroup) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 15 with ReturnStatement

use of org.eclipse.jdt.core.dom.ReturnStatement in project flux by eclipse.

the class ModifierCorrectionSubProcessor method addNativeMethodProposals.

public static void addNativeMethodProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    ICompilationUnit cu = context.getCompilationUnit();
    CompilationUnit astRoot = context.getASTRoot();
    ASTNode selectedNode = problem.getCoveringNode(astRoot);
    if (selectedNode == null) {
        return;
    }
    MethodDeclaration decl;
    if (selectedNode instanceof SimpleName) {
        decl = (MethodDeclaration) selectedNode.getParent();
    } else if (selectedNode instanceof MethodDeclaration) {
        decl = (MethodDeclaration) selectedNode;
    } else {
        return;
    }
    {
        AST ast = astRoot.getAST();
        ASTRewrite rewrite = ASTRewrite.create(ast);
        removeModifier(decl, rewrite, Modifier.NATIVE);
        Block newBody = ast.newBlock();
        rewrite.set(decl, MethodDeclaration.BODY_PROPERTY, newBody, null);
        Type returnType = decl.getReturnType2();
        if (returnType != null) {
            Expression expr = ASTNodeFactory.newDefaultExpression(ast, returnType, decl.getExtraDimensions());
            if (expr != null) {
                ReturnStatement returnStatement = ast.newReturnStatement();
                returnStatement.setExpression(expr);
                newBody.statements().add(returnStatement);
            }
        }
        String label = CorrectionMessages.ModifierCorrectionSubProcessor_removenative_description;
        //			Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
        ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.REMOVE_NATIVE);
        proposals.add(proposal);
    }
    if (decl.getBody() != null) {
        ASTRewrite rewrite = ASTRewrite.create(decl.getAST());
        rewrite.remove(decl.getBody(), null);
        String label = CorrectionMessages.ModifierCorrectionSubProcessor_removebody_description;
        //			Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
        ASTRewriteCorrectionProposal proposal2 = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.REMOVE_METHOD_BODY);
        proposals.add(proposal2);
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) Expression(org.eclipse.jdt.core.dom.Expression) 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)

Aggregations

ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)34 Block (org.eclipse.jdt.core.dom.Block)24 AST (org.eclipse.jdt.core.dom.AST)23 Expression (org.eclipse.jdt.core.dom.Expression)22 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)21 ASTNode (org.eclipse.jdt.core.dom.ASTNode)20 Type (org.eclipse.jdt.core.dom.Type)19 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)18 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)14 Statement (org.eclipse.jdt.core.dom.Statement)12 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)11 SimpleName (org.eclipse.jdt.core.dom.SimpleName)11 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)11 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)10 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)9 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)9 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)9 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)8 DoStatement (org.eclipse.jdt.core.dom.DoStatement)8 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)8