Search in sources :

Example 26 with ASTRewriteCorrectionProposal

use of org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal in project che by eclipse.

the class QuickAssistProcessor method getUnWrapProposals.

private static boolean getUnWrapProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
    ASTNode outer = node;
    Block block = null;
    if (outer.getNodeType() == ASTNode.BLOCK) {
        block = (Block) outer;
        outer = block.getParent();
    }
    ASTNode body = null;
    String label = null;
    if (outer instanceof IfStatement) {
        IfStatement ifStatement = (IfStatement) outer;
        Statement elseBlock = ifStatement.getElseStatement();
        if (elseBlock == null || elseBlock instanceof Block && ((Block) elseBlock).statements().isEmpty()) {
            body = ifStatement.getThenStatement();
        }
        label = CorrectionMessages.QuickAssistProcessor_unwrap_ifstatement;
    } else if (outer instanceof WhileStatement) {
        body = ((WhileStatement) outer).getBody();
        label = CorrectionMessages.QuickAssistProcessor_unwrap_whilestatement;
    } else if (outer instanceof ForStatement) {
        body = ((ForStatement) outer).getBody();
        label = CorrectionMessages.QuickAssistProcessor_unwrap_forstatement;
    } else if (outer instanceof EnhancedForStatement) {
        body = ((EnhancedForStatement) outer).getBody();
        label = CorrectionMessages.QuickAssistProcessor_unwrap_forstatement;
    } else if (outer instanceof SynchronizedStatement) {
        body = ((SynchronizedStatement) outer).getBody();
        label = CorrectionMessages.QuickAssistProcessor_unwrap_synchronizedstatement;
    } else if (outer instanceof SimpleName && outer.getParent() instanceof LabeledStatement) {
        LabeledStatement labeledStatement = (LabeledStatement) outer.getParent();
        outer = labeledStatement;
        body = labeledStatement.getBody();
        label = CorrectionMessages.QuickAssistProcessor_unwrap_labeledstatement;
    } else if (outer instanceof LabeledStatement) {
        body = ((LabeledStatement) outer).getBody();
        label = CorrectionMessages.QuickAssistProcessor_unwrap_labeledstatement;
    } else if (outer instanceof DoStatement) {
        body = ((DoStatement) outer).getBody();
        label = CorrectionMessages.QuickAssistProcessor_unwrap_dostatement;
    } else if (outer instanceof TryStatement) {
        TryStatement tryStatement = (TryStatement) outer;
        if (tryStatement.catchClauses().isEmpty() && tryStatement.resources().isEmpty()) {
            body = tryStatement.getBody();
        }
        label = CorrectionMessages.QuickAssistProcessor_unwrap_trystatement;
    } else if (outer instanceof AnonymousClassDeclaration) {
        List<BodyDeclaration> decls = ((AnonymousClassDeclaration) outer).bodyDeclarations();
        for (int i = 0; i < decls.size(); i++) {
            BodyDeclaration elem = decls.get(i);
            if (elem instanceof MethodDeclaration) {
                Block curr = ((MethodDeclaration) elem).getBody();
                if (curr != null && !curr.statements().isEmpty()) {
                    if (body != null) {
                        return false;
                    }
                    body = curr;
                }
            } else if (elem instanceof TypeDeclaration) {
                return false;
            }
        }
        label = CorrectionMessages.QuickAssistProcessor_unwrap_anonymous;
        outer = ASTResolving.findParentStatement(outer);
        if (outer == null) {
            // private Object o= new Object() { ... };
            return false;
        }
    } else if (outer instanceof Block) {
        //	-> a block in a block
        body = block;
        outer = block;
        label = CorrectionMessages.QuickAssistProcessor_unwrap_block;
    } else if (outer instanceof ParenthesizedExpression) {
    //ParenthesizedExpression expression= (ParenthesizedExpression) outer;
    //body= expression.getExpression();
    //label= CorrectionMessages.getString("QuickAssistProcessor.unwrap.parenthesis");	 //$NON-NLS-1$
    } else if (outer instanceof MethodInvocation) {
        MethodInvocation invocation = (MethodInvocation) outer;
        if (invocation.arguments().size() == 1) {
            body = (ASTNode) invocation.arguments().get(0);
            if (invocation.getParent().getNodeType() == ASTNode.EXPRESSION_STATEMENT) {
                int kind = body.getNodeType();
                if (kind != ASTNode.ASSIGNMENT && kind != ASTNode.PREFIX_EXPRESSION && kind != ASTNode.POSTFIX_EXPRESSION && kind != ASTNode.METHOD_INVOCATION && kind != ASTNode.SUPER_METHOD_INVOCATION) {
                    body = null;
                }
            }
            label = CorrectionMessages.QuickAssistProcessor_unwrap_methodinvocation;
        }
    }
    if (body == null) {
        return false;
    }
    ASTRewrite rewrite = ASTRewrite.create(outer.getAST());
    ASTNode inner = getCopyOfInner(rewrite, body, ASTNodes.isControlStatementBody(outer.getLocationInParent()));
    if (inner == null) {
        return false;
    }
    if (resultingCollections == null) {
        return true;
    }
    rewrite.replace(outer, inner, null);
    Image image = JavaPluginImages.get(JavaPluginImages.IMG_OBJS_EXCEPTION);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.UNWRAP_STATEMENTS, image);
    resultingCollections.add(proposal);
    return true;
}
Also used : Image(org.eclipse.swt.graphics.Image) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) List(java.util.List) ArrayList(java.util.ArrayList)

Example 27 with ASTRewriteCorrectionProposal

use of org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal in project che by eclipse.

the class AdvancedQuickAssistProcessor method getSplitOrConditionProposals.

public static boolean getSplitOrConditionProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
    Operator orOperator = InfixExpression.Operator.CONDITIONAL_OR;
    // check that user invokes quick assist on infix expression
    if (!(node instanceof InfixExpression)) {
        return false;
    }
    InfixExpression infixExpression = (InfixExpression) node;
    if (infixExpression.getOperator() != orOperator) {
        return false;
    }
    int offset = isOperatorSelected(infixExpression, context.getSelectionOffset(), context.getSelectionLength());
    if (offset == -1) {
        return false;
    }
    // check that infix expression belongs to IfStatement
    Statement statement = ASTResolving.findParentStatement(node);
    if (!(statement instanceof IfStatement)) {
        return false;
    }
    IfStatement ifStatement = (IfStatement) statement;
    // check that infix expression is part of first level || condition of IfStatement
    InfixExpression topInfixExpression = infixExpression;
    while (topInfixExpression.getParent() instanceof InfixExpression && ((InfixExpression) topInfixExpression.getParent()).getOperator() == orOperator) {
        topInfixExpression = (InfixExpression) topInfixExpression.getParent();
    }
    if (ifStatement.getExpression() != topInfixExpression) {
        return false;
    }
    //
    if (resultingCollections == null) {
        return true;
    }
    AST ast = ifStatement.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    // prepare left and right conditions
    Expression[] newOperands = { null, null };
    breakInfixOperationAtOperation(rewrite, topInfixExpression, orOperator, offset, true, newOperands);
    Expression leftCondition = newOperands[0];
    Expression rightCondition = newOperands[1];
    // prepare first statement
    rewrite.replace(ifStatement.getExpression(), leftCondition, null);
    IfStatement secondIf = ast.newIfStatement();
    secondIf.setExpression(rightCondition);
    secondIf.setThenStatement((Statement) rewrite.createCopyTarget(ifStatement.getThenStatement()));
    Statement elseStatement = ifStatement.getElseStatement();
    if (elseStatement == null) {
        rewrite.set(ifStatement, IfStatement.ELSE_STATEMENT_PROPERTY, secondIf, null);
    } else {
        rewrite.replace(elseStatement, secondIf, null);
        secondIf.setElseStatement((Statement) rewrite.createMoveTarget(elseStatement));
    }
    // add correction proposal
    String label = CorrectionMessages.AdvancedQuickAssistProcessor_splitOrCondition_description;
    Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.SPLIT_OR_CONDITION, image);
    resultingCollections.add(proposal);
    return true;
}
Also used : Operator(org.eclipse.jdt.core.dom.InfixExpression.Operator) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Image(org.eclipse.swt.graphics.Image)

Example 28 with ASTRewriteCorrectionProposal

use of org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal 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 29 with ASTRewriteCorrectionProposal

use of org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal 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 30 with ASTRewriteCorrectionProposal

use of org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal 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)

Aggregations

ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)125 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)93 Image (org.eclipse.swt.graphics.Image)70 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)49 AST (org.eclipse.jdt.core.dom.AST)38 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)35 ASTNode (org.eclipse.jdt.core.dom.ASTNode)33 Expression (org.eclipse.jdt.core.dom.Expression)32 ArrayList (java.util.ArrayList)29 CastExpression (org.eclipse.jdt.core.dom.CastExpression)24 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)23 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)23 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)23 Test (org.junit.Test)23 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)21 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)21 Block (org.eclipse.jdt.core.dom.Block)19 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)19 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)19 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)19