Search in sources :

Example 71 with ASTRewrite

use of org.eclipse.jdt.core.dom.rewrite.ASTRewrite in project che by eclipse.

the class AdvancedQuickAssistProcessor method getConvertToIfReturnProposals.

private static boolean getConvertToIfReturnProposals(IInvocationContext context, ASTNode coveringNode, ArrayList<ICommandAccess> resultingCollections) {
    if (!(coveringNode instanceof IfStatement)) {
        return false;
    }
    IfStatement ifStatement = (IfStatement) coveringNode;
    if (ifStatement.getElseStatement() != null) {
        return false;
    }
    // enclosing lambda or method should return 'void'
    LambdaExpression enclosingLambda = ASTResolving.findEnclosingLambdaExpression(ifStatement);
    if (enclosingLambda != null) {
        IMethodBinding lambdaMethodBinding = enclosingLambda.resolveMethodBinding();
        if (lambdaMethodBinding == null) {
            return false;
        }
        if (!(ifStatement.getAST().resolveWellKnownType("void").equals(lambdaMethodBinding.getReturnType()))) {
            //$NON-NLS-1$
            return false;
        }
    } else {
        MethodDeclaration coveringMethod = ASTResolving.findParentMethodDeclaration(ifStatement);
        if (coveringMethod == null) {
            return false;
        }
        Type returnType = coveringMethod.getReturnType2();
        if (!isVoid(returnType)) {
            return false;
        }
    }
    // should be present in a block
    if (!(ifStatement.getParent() instanceof Block)) {
        return false;
    }
    // should have at least one statement in 'then' part other than 'return'
    Statement thenStatement = ifStatement.getThenStatement();
    if (thenStatement instanceof ReturnStatement) {
        return false;
    }
    if (thenStatement instanceof Block) {
        List<Statement> thenStatements = ((Block) thenStatement).statements();
        if (thenStatements.isEmpty() || (thenStatements.size() == 1 && (thenStatements.get(0) instanceof ReturnStatement))) {
            return false;
        }
    }
    // should have no further executable statement
    if (!isLastStatementInEnclosingMethodOrLambda(ifStatement)) {
        return false;
    }
    //  we could produce quick assist
    if (resultingCollections == null) {
        return true;
    }
    AST ast = coveringNode.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    // create inverted 'if' statement
    Expression inversedExpression = getInversedExpression(rewrite, ifStatement.getExpression());
    IfStatement newIf = ast.newIfStatement();
    newIf.setExpression(inversedExpression);
    newIf.setThenStatement(ast.newReturnStatement());
    ListRewrite listRewriter = rewrite.getListRewrite(ifStatement.getParent(), (ChildListPropertyDescriptor) ifStatement.getLocationInParent());
    listRewriter.replace(ifStatement, newIf, null);
    // remove last 'return' in 'then' block
    ArrayList<Statement> statements = getUnwrappedStatements(ifStatement.getThenStatement());
    Statement lastStatement = statements.get(statements.size() - 1);
    if (lastStatement instanceof ReturnStatement) {
        statements.remove(lastStatement);
    }
    // add statements from 'then' to the end of block
    for (Statement statement : statements) {
        listRewriter.insertLast(rewrite.createMoveTarget(statement), null);
    }
    // add correction proposal
    String label = CorrectionMessages.AdvancedQuickAssistProcessor_convertToIfReturn;
    Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.CONVERT_TO_IF_RETURN, image);
    resultingCollections.add(proposal);
    return true;
}
Also used : ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) Image(org.eclipse.swt.graphics.Image) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)

Example 72 with ASTRewrite

use of org.eclipse.jdt.core.dom.rewrite.ASTRewrite in project che by eclipse.

the class LocalCorrectionsSubProcessor method getUnnecessaryElseProposals.

public static void getUnnecessaryElseProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    CompilationUnit root = context.getASTRoot();
    ASTNode selectedNode = problem.getCoveringNode(root);
    if (selectedNode == null) {
        return;
    }
    ASTNode parent = selectedNode.getParent();
    if (parent instanceof ExpressionStatement) {
        parent = parent.getParent();
    }
    if (!(parent instanceof IfStatement)) {
        return;
    }
    IfStatement ifStatement = (IfStatement) parent;
    ASTNode ifParent = ifStatement.getParent();
    if (!(ifParent instanceof Block) && !(ifParent instanceof SwitchStatement) && !ASTNodes.isControlStatementBody(ifStatement.getLocationInParent())) {
        return;
    }
    ASTRewrite rewrite = ASTRewrite.create(root.getAST());
    ASTNode placeholder = QuickAssistProcessor.getCopyOfInner(rewrite, ifStatement.getElseStatement(), false);
    if (placeholder == null) {
        return;
    }
    rewrite.remove(ifStatement.getElseStatement(), null);
    if (ifParent instanceof Block) {
        ListRewrite listRewrite = rewrite.getListRewrite(ifParent, Block.STATEMENTS_PROPERTY);
        listRewrite.insertAfter(placeholder, ifStatement, null);
    } else if (ifParent instanceof SwitchStatement) {
        ListRewrite listRewrite = rewrite.getListRewrite(ifParent, SwitchStatement.STATEMENTS_PROPERTY);
        listRewrite.insertAfter(placeholder, ifStatement, null);
    } else {
        Block block = root.getAST().newBlock();
        rewrite.replace(ifStatement, block, null);
        block.statements().add(rewrite.createCopyTarget(ifStatement));
        block.statements().add(placeholder);
    }
    String label = CorrectionMessages.LocalCorrectionsSubProcessor_removeelse_description;
    Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.REMOVE_ELSE, image);
    proposals.add(proposal);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) Image(org.eclipse.swt.graphics.Image)

Example 73 with ASTRewrite

use of org.eclipse.jdt.core.dom.rewrite.ASTRewrite in project che by eclipse.

the class LocalCorrectionsSubProcessor method getInterfaceExtendsClassProposals.

public static void getInterfaceExtendsClassProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    CompilationUnit root = context.getASTRoot();
    ASTNode selectedNode = problem.getCoveringNode(root);
    if (selectedNode == null) {
        return;
    }
    while (selectedNode.getParent() instanceof Type) {
        selectedNode = selectedNode.getParent();
    }
    StructuralPropertyDescriptor locationInParent = selectedNode.getLocationInParent();
    if (locationInParent != TypeDeclaration.SUPERCLASS_TYPE_PROPERTY) {
        return;
    }
    TypeDeclaration typeDecl = (TypeDeclaration) selectedNode.getParent();
    {
        ASTRewrite rewrite = ASTRewrite.create(root.getAST());
        ASTNode placeHolder = rewrite.createMoveTarget(selectedNode);
        ListRewrite interfaces = rewrite.getListRewrite(typeDecl, TypeDeclaration.SUPER_INTERFACE_TYPES_PROPERTY);
        interfaces.insertFirst(placeHolder, null);
        String label = CorrectionMessages.LocalCorrectionsSubProcessor_extendstoimplements_description;
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
        ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.CHANGE_EXTENDS_TO_IMPLEMENTS, image);
        proposals.add(proposal);
    }
    {
        ASTRewrite rewrite = ASTRewrite.create(root.getAST());
        rewrite.set(typeDecl, TypeDeclaration.INTERFACE_PROPERTY, Boolean.TRUE, null);
        String typeName = typeDecl.getName().getIdentifier();
        String label = Messages.format(CorrectionMessages.LocalCorrectionsSubProcessor_classtointerface_description, BasicElementLabels.getJavaElementName(typeName));
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
        ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.CHANGE_CLASS_TO_INTERFACE, image);
        proposals.add(proposal);
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) IType(org.eclipse.jdt.core.IType) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) Image(org.eclipse.swt.graphics.Image)

Example 74 with ASTRewrite

use of org.eclipse.jdt.core.dom.rewrite.ASTRewrite in project che by eclipse.

the class LocalCorrectionsSubProcessor method getUnreachableCodeProposals.

public static void getUnreachableCodeProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    CompilationUnit root = context.getASTRoot();
    ASTNode selectedNode = problem.getCoveringNode(root);
    if (selectedNode == null) {
        return;
    }
    ASTNode parent = selectedNode.getParent();
    while (parent instanceof ExpressionStatement) {
        selectedNode = parent;
        parent = selectedNode.getParent();
    }
    if (parent instanceof WhileStatement) {
        addRemoveIncludingConditionProposal(context, parent, null, proposals);
    } else if (selectedNode.getLocationInParent() == IfStatement.THEN_STATEMENT_PROPERTY) {
        Statement elseStatement = ((IfStatement) parent).getElseStatement();
        addRemoveIncludingConditionProposal(context, parent, elseStatement, proposals);
    } else if (selectedNode.getLocationInParent() == IfStatement.ELSE_STATEMENT_PROPERTY) {
        Statement thenStatement = ((IfStatement) parent).getThenStatement();
        addRemoveIncludingConditionProposal(context, parent, thenStatement, proposals);
    } else if (selectedNode.getLocationInParent() == ForStatement.BODY_PROPERTY) {
        Statement body = ((ForStatement) parent).getBody();
        addRemoveIncludingConditionProposal(context, parent, body, proposals);
    } else if (selectedNode.getLocationInParent() == ConditionalExpression.THEN_EXPRESSION_PROPERTY) {
        Expression elseExpression = ((ConditionalExpression) parent).getElseExpression();
        addRemoveIncludingConditionProposal(context, parent, elseExpression, proposals);
    } else if (selectedNode.getLocationInParent() == ConditionalExpression.ELSE_EXPRESSION_PROPERTY) {
        Expression thenExpression = ((ConditionalExpression) parent).getThenExpression();
        addRemoveIncludingConditionProposal(context, parent, thenExpression, proposals);
    } else if (selectedNode.getLocationInParent() == InfixExpression.RIGHT_OPERAND_PROPERTY) {
        // also offer split && / || condition proposals:
        InfixExpression infixExpression = (InfixExpression) parent;
        Expression leftOperand = infixExpression.getLeftOperand();
        List<Expression> extendedOperands = infixExpression.extendedOperands();
        ASTRewrite rewrite = ASTRewrite.create(parent.getAST());
        if (extendedOperands.size() == 0) {
            rewrite.replace(infixExpression, rewrite.createMoveTarget(leftOperand), null);
        } else {
            ASTNode firstExtendedOp = rewrite.createMoveTarget(extendedOperands.get(0));
            rewrite.set(infixExpression, InfixExpression.RIGHT_OPERAND_PROPERTY, firstExtendedOp, null);
            rewrite.remove(leftOperand, null);
        }
        String label = CorrectionMessages.LocalCorrectionsSubProcessor_removeunreachablecode_description;
        addRemoveProposal(context, rewrite, label, proposals);
        AssistContext assistContext = new AssistContext(context.getCompilationUnit(), infixExpression.getRightOperand().getStartPosition() - 1, 0);
        assistContext.setASTRoot(root);
        AdvancedQuickAssistProcessor.getSplitAndConditionProposals(assistContext, infixExpression, proposals);
        AdvancedQuickAssistProcessor.getSplitOrConditionProposals(assistContext, infixExpression, proposals);
    } else if (selectedNode instanceof Statement && selectedNode.getLocationInParent().isChildListProperty()) {
        // remove all statements following the unreachable:
        List<Statement> statements = ASTNodes.<Statement>getChildListProperty(selectedNode.getParent(), (ChildListPropertyDescriptor) selectedNode.getLocationInParent());
        int idx = statements.indexOf(selectedNode);
        ASTRewrite rewrite = ASTRewrite.create(selectedNode.getAST());
        String label = CorrectionMessages.LocalCorrectionsSubProcessor_removeunreachablecode_description;
        if (idx > 0) {
            Object prevStatement = statements.get(idx - 1);
            if (prevStatement instanceof IfStatement) {
                IfStatement ifStatement = (IfStatement) prevStatement;
                if (ifStatement.getElseStatement() == null) {
                    // remove if (true), see https://bugs.eclipse.org/bugs/show_bug.cgi?id=261519
                    rewrite.replace(ifStatement, rewrite.createMoveTarget(ifStatement.getThenStatement()), null);
                    label = CorrectionMessages.LocalCorrectionsSubProcessor_removeunreachablecode_including_condition_description;
                }
            }
        }
        for (int i = idx; i < statements.size(); i++) {
            ASTNode statement = statements.get(i);
            if (statement instanceof SwitchCase)
                // stop at case *: and default:
                break;
            rewrite.remove(statement, null);
        }
        addRemoveProposal(context, rewrite, label, proposals);
    } else {
        // no special case, just remove the node:
        addRemoveProposal(context, selectedNode, proposals);
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) List(java.util.List) ArrayList(java.util.ArrayList)

Example 75 with ASTRewrite

use of org.eclipse.jdt.core.dom.rewrite.ASTRewrite in project che 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, image);
    resultingCollections.add(proposal);
    return true;
}
Also used : ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Image(org.eclipse.swt.graphics.Image)

Aggregations

ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)180 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)93 Image (org.eclipse.swt.graphics.Image)80 AST (org.eclipse.jdt.core.dom.AST)78 ASTNode (org.eclipse.jdt.core.dom.ASTNode)69 Expression (org.eclipse.jdt.core.dom.Expression)54 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)49 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)44 Block (org.eclipse.jdt.core.dom.Block)38 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)35 SimpleName (org.eclipse.jdt.core.dom.SimpleName)34 ImportRewrite (org.eclipse.jdt.core.dom.rewrite.ImportRewrite)34 CastExpression (org.eclipse.jdt.core.dom.CastExpression)33 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)32 Type (org.eclipse.jdt.core.dom.Type)30 ContextSensitiveImportRewriteContext (org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext)30 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)29 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)29 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)29 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)29