Search in sources :

Example 26 with ParenthesizedExpression

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

the class ExtractMethodRefactoring method createMethodBody.

private Block createMethodBody(ASTNode[] selectedNodes, TextEditGroup substitute, int modifiers) {
    Block result = fAST.newBlock();
    ListRewrite statements = fRewriter.getListRewrite(result, Block.STATEMENTS_PROPERTY);
    // Locals that are not passed as an arguments since the extracted method only
    // writes to them
    IVariableBinding[] methodLocals = fAnalyzer.getMethodLocals();
    for (int i = 0; i < methodLocals.length; i++) {
        if (methodLocals[i] != null) {
            result.statements().add(createDeclaration(methodLocals[i], null));
        }
    }
    for (Iterator<ParameterInfo> iter = fParameterInfos.iterator(); iter.hasNext(); ) {
        ParameterInfo parameter = iter.next();
        if (parameter.isRenamed()) {
            for (int n = 0; n < selectedNodes.length; n++) {
                SimpleName[] oldNames = LinkedNodeFinder.findByBinding(selectedNodes[n], parameter.getOldBinding());
                for (int i = 0; i < oldNames.length; i++) {
                    fRewriter.replace(oldNames[i], fAST.newSimpleName(parameter.getNewName()), null);
                }
            }
        }
    }
    boolean extractsExpression = fAnalyzer.isExpressionSelected();
    ASTNode[] callNodes = createCallNodes(null, modifiers);
    ASTNode replacementNode;
    if (callNodes.length == 1) {
        replacementNode = callNodes[0];
    } else {
        replacementNode = fRewriter.createGroupNode(callNodes);
    }
    if (extractsExpression) {
        // if we have an expression then only one node is selected.
        ITypeBinding binding = fAnalyzer.getExpressionBinding();
        if (binding != null && (!binding.isPrimitive() || !"void".equals(binding.getName()))) {
            //$NON-NLS-1$
            ReturnStatement rs = fAST.newReturnStatement();
            rs.setExpression((Expression) fRewriter.createMoveTarget(selectedNodes[0] instanceof ParenthesizedExpression ? ((ParenthesizedExpression) selectedNodes[0]).getExpression() : selectedNodes[0]));
            statements.insertLast(rs, null);
        } else {
            ExpressionStatement st = fAST.newExpressionStatement((Expression) fRewriter.createMoveTarget(selectedNodes[0]));
            statements.insertLast(st, null);
        }
        fRewriter.replace(selectedNodes[0].getParent() instanceof ParenthesizedExpression ? selectedNodes[0].getParent() : selectedNodes[0], replacementNode, substitute);
    } else {
        if (selectedNodes.length == 1) {
            statements.insertLast(fRewriter.createMoveTarget(selectedNodes[0]), substitute);
            fRewriter.replace(selectedNodes[0], replacementNode, substitute);
        } else {
            ListRewrite source = fRewriter.getListRewrite(selectedNodes[0].getParent(), (ChildListPropertyDescriptor) selectedNodes[0].getLocationInParent());
            ASTNode toMove = source.createMoveTarget(selectedNodes[0], selectedNodes[selectedNodes.length - 1], replacementNode, substitute);
            statements.insertLast(toMove, substitute);
        }
        IVariableBinding returnValue = fAnalyzer.getReturnValue();
        if (returnValue != null) {
            ReturnStatement rs = fAST.newReturnStatement();
            rs.setExpression(fAST.newSimpleName(getName(returnValue)));
            statements.insertLast(rs, null);
        }
    }
    return result;
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) ParameterInfo(org.eclipse.jdt.internal.corext.refactoring.ParameterInfo) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Block(org.eclipse.jdt.core.dom.Block)

Example 27 with ParenthesizedExpression

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

the class AdvancedQuickAssistProcessor method getPullNegationUpProposals.

private static boolean getPullNegationUpProposals(IInvocationContext context, ArrayList<ASTNode> coveredNodes, Collection<ICommandAccess> resultingCollections) {
    if (coveredNodes.size() != 1) {
        return false;
    }
    //
    ASTNode fullyCoveredNode = coveredNodes.get(0);
    Expression expression = getBooleanExpression(fullyCoveredNode);
    if (expression == null || (!(expression instanceof InfixExpression) && !(expression instanceof ConditionalExpression))) {
        return false;
    }
    //  we could produce quick assist
    if (resultingCollections == null) {
        return true;
    }
    //
    AST ast = expression.getAST();
    final ASTRewrite rewrite = ASTRewrite.create(ast);
    // prepared inverted expression
    Expression inversedExpression = getInversedExpression(rewrite, expression);
    // prepare ParenthesizedExpression
    ParenthesizedExpression parenthesizedExpression = ast.newParenthesizedExpression();
    parenthesizedExpression.setExpression(inversedExpression);
    // prepare NOT prefix expression
    PrefixExpression prefixExpression = ast.newPrefixExpression();
    prefixExpression.setOperator(PrefixExpression.Operator.NOT);
    prefixExpression.setOperand(parenthesizedExpression);
    // replace old expression
    rewrite.replace(expression, prefixExpression, null);
    // add correction proposal
    String label = CorrectionMessages.AdvancedQuickAssistProcessor_pullNegationUp;
    //		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.PULL_NEGATION_UP);
    resultingCollections.add(proposal);
    return true;
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) AST(org.eclipse.jdt.core.dom.AST) 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) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)

Example 28 with ParenthesizedExpression

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

the class AdvancedQuickAssistProcessor method getInversedNotExpression.

private static Expression getInversedNotExpression(ASTRewrite rewrite, Expression expression, AST ast) {
    PrefixExpression prefixExpression = ast.newPrefixExpression();
    prefixExpression.setOperator(PrefixExpression.Operator.NOT);
    ParenthesizedExpression parenthesizedExpression = getParenthesizedExpression(ast, (Expression) rewrite.createCopyTarget(expression));
    prefixExpression.setOperand(parenthesizedExpression);
    return prefixExpression;
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression)

Example 29 with ParenthesizedExpression

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

the class AdvancedQuickAssistProcessor method getReplaceConditionalWithIfElseProposals.

private static boolean getReplaceConditionalWithIfElseProposals(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) {
    ASTNode node = covering;
    while (!(node instanceof ConditionalExpression) && node instanceof Expression) {
        node = node.getParent();
    }
    if (!(node instanceof ConditionalExpression)) {
        node = covering;
        while (node != null && !(node instanceof Statement)) {
            node = node.getParent();
        }
        if (node instanceof VariableDeclarationStatement) {
            node = (ASTNode) (((VariableDeclarationStatement) node).fragments().get(0));
            node = ((VariableDeclarationFragment) node).getInitializer();
        }
        if (node instanceof ExpressionStatement) {
            node = ((ExpressionStatement) node).getExpression();
            if (node instanceof Assignment) {
                node = ((Assignment) node).getRightHandSide();
            }
        }
        if (node instanceof ReturnStatement) {
            node = ((ReturnStatement) node).getExpression();
        }
    }
    if (!(node instanceof ConditionalExpression)) {
        return false;
    }
    covering = node;
    StructuralPropertyDescriptor locationInParent = covering.getLocationInParent();
    if (locationInParent == Assignment.RIGHT_HAND_SIDE_PROPERTY) {
        if (covering.getParent().getLocationInParent() != ExpressionStatement.EXPRESSION_PROPERTY) {
            return false;
        }
    } else if (locationInParent == VariableDeclarationFragment.INITIALIZER_PROPERTY) {
        ASTNode statement = covering.getParent().getParent();
        if (!(statement instanceof VariableDeclarationStatement) || statement.getLocationInParent() != Block.STATEMENTS_PROPERTY) {
            return false;
        }
    } else if (locationInParent != ReturnStatement.EXPRESSION_PROPERTY) {
        return false;
    }
    ConditionalExpression conditional = (ConditionalExpression) covering;
    //  we could produce quick assist
    if (resultingCollections == null) {
        return true;
    }
    //
    AST ast = covering.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    // prepare new 'if' statement
    Expression expression = conditional.getExpression();
    while (expression instanceof ParenthesizedExpression) {
        expression = ((ParenthesizedExpression) expression).getExpression();
    }
    IfStatement ifStatement = ast.newIfStatement();
    ifStatement.setExpression((Expression) rewrite.createCopyTarget(expression));
    if (locationInParent == Assignment.RIGHT_HAND_SIDE_PROPERTY) {
        Assignment assignment = (Assignment) covering.getParent();
        Expression assignee = assignment.getLeftHandSide();
        Assignment.Operator op = assignment.getOperator();
        ifStatement.setThenStatement(createAssignmentStatement(rewrite, op, assignee, conditional.getThenExpression()));
        ifStatement.setElseStatement(createAssignmentStatement(rewrite, op, assignee, conditional.getElseExpression()));
        // replace return conditional expression with if/then/else/return
        rewrite.replace(covering.getParent().getParent(), ifStatement, null);
    } else if (locationInParent == ReturnStatement.EXPRESSION_PROPERTY) {
        ifStatement.setThenStatement(createReturnExpression(rewrite, conditional.getThenExpression()));
        ifStatement.setElseStatement(createReturnExpression(rewrite, conditional.getElseExpression()));
        //
        // replace return conditional expression with if/then/else/return
        rewrite.replace(conditional.getParent(), ifStatement, null);
    } else if (locationInParent == VariableDeclarationFragment.INITIALIZER_PROPERTY) {
        VariableDeclarationFragment frag = (VariableDeclarationFragment) covering.getParent();
        Assignment.Operator op = Assignment.Operator.ASSIGN;
        Expression assignee = frag.getName();
        ifStatement.setThenStatement(createAssignmentStatement(rewrite, op, assignee, conditional.getThenExpression()));
        ifStatement.setElseStatement(createAssignmentStatement(rewrite, op, assignee, conditional.getElseExpression()));
        // clear initializer
        rewrite.set(frag, VariableDeclarationFragment.INITIALIZER_PROPERTY, null, null);
        ASTNode statement = frag.getParent();
        rewrite.getListRewrite(statement.getParent(), Block.STATEMENTS_PROPERTY).insertAfter(ifStatement, statement, null);
    }
    // add correction proposal
    String label = CorrectionMessages.AdvancedQuickAssistProcessor_replaceConditionalWithIf;
    //		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.REPLACE_CONDITIONAL_WITH_IF_ELSE);
    resultingCollections.add(proposal);
    return true;
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) 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) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) Assignment(org.eclipse.jdt.core.dom.Assignment) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) IfStatement(org.eclipse.jdt.core.dom.IfStatement) 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) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) StructuralPropertyDescriptor(org.eclipse.jdt.core.dom.StructuralPropertyDescriptor)

Example 30 with ParenthesizedExpression

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

the class AdvancedQuickAssistProcessor method getInversedExpression.

private static Expression getInversedExpression(ASTRewrite rewrite, Expression expression, SimpleNameRenameProvider provider) {
    AST ast = rewrite.getAST();
    //
    if (expression instanceof BooleanLiteral) {
        return ast.newBooleanLiteral(!((BooleanLiteral) expression).booleanValue());
    }
    if (expression instanceof InfixExpression) {
        InfixExpression infixExpression = (InfixExpression) expression;
        InfixExpression.Operator operator = infixExpression.getOperator();
        if (operator == InfixExpression.Operator.LESS) {
            return getInversedInfixExpression(rewrite, infixExpression, InfixExpression.Operator.GREATER_EQUALS, provider);
        }
        if (operator == InfixExpression.Operator.GREATER) {
            return getInversedInfixExpression(rewrite, infixExpression, InfixExpression.Operator.LESS_EQUALS, provider);
        }
        if (operator == InfixExpression.Operator.LESS_EQUALS) {
            return getInversedInfixExpression(rewrite, infixExpression, InfixExpression.Operator.GREATER, provider);
        }
        if (operator == InfixExpression.Operator.GREATER_EQUALS) {
            return getInversedInfixExpression(rewrite, infixExpression, InfixExpression.Operator.LESS, provider);
        }
        if (operator == InfixExpression.Operator.EQUALS) {
            return getInversedInfixExpression(rewrite, infixExpression, InfixExpression.Operator.NOT_EQUALS, provider);
        }
        if (operator == InfixExpression.Operator.NOT_EQUALS) {
            return getInversedInfixExpression(rewrite, infixExpression, InfixExpression.Operator.EQUALS, provider);
        }
        if (operator == InfixExpression.Operator.CONDITIONAL_AND) {
            return getInversedAndOrExpression(rewrite, infixExpression, InfixExpression.Operator.CONDITIONAL_OR, provider);
        }
        if (operator == InfixExpression.Operator.CONDITIONAL_OR) {
            return getInversedAndOrExpression(rewrite, infixExpression, InfixExpression.Operator.CONDITIONAL_AND, provider);
        }
        if (operator == InfixExpression.Operator.AND) {
            return getInversedAndOrExpression(rewrite, infixExpression, InfixExpression.Operator.OR, provider);
        }
        if (operator == InfixExpression.Operator.OR) {
            return getInversedAndOrExpression(rewrite, infixExpression, InfixExpression.Operator.AND, provider);
        }
        if (operator == InfixExpression.Operator.XOR) {
            return getInversedNotExpression(rewrite, expression, ast);
        }
    }
    if (expression instanceof PrefixExpression) {
        PrefixExpression prefixExpression = (PrefixExpression) expression;
        if (prefixExpression.getOperator() == PrefixExpression.Operator.NOT) {
            Expression operand = prefixExpression.getOperand();
            if ((operand instanceof ParenthesizedExpression) && NecessaryParenthesesChecker.canRemoveParentheses(operand, expression.getParent(), expression.getLocationInParent())) {
                operand = ((ParenthesizedExpression) operand).getExpression();
            }
            Expression renamedNameCopy = getRenamedNameCopy(provider, rewrite, operand);
            if (renamedNameCopy instanceof InfixExpression) {
                InfixExpression infixExpression = (InfixExpression) renamedNameCopy;
                infixExpression.setOperator(((InfixExpression) operand).getOperator());
            }
            return renamedNameCopy;
        }
    }
    if (expression instanceof InstanceofExpression) {
        return getInversedNotExpression(rewrite, expression, ast);
    }
    if (expression instanceof ParenthesizedExpression) {
        ParenthesizedExpression parenthesizedExpression = (ParenthesizedExpression) expression;
        Expression innerExpression = parenthesizedExpression.getExpression();
        while (innerExpression instanceof ParenthesizedExpression) {
            innerExpression = ((ParenthesizedExpression) innerExpression).getExpression();
        }
        if (innerExpression instanceof InstanceofExpression) {
            return getInversedExpression(rewrite, innerExpression, provider);
        }
        parenthesizedExpression = getParenthesizedExpression(ast, getInversedExpression(rewrite, innerExpression, provider));
        return parenthesizedExpression;
    }
    if (expression instanceof ConditionalExpression) {
        ConditionalExpression conditionalExpression = (ConditionalExpression) expression;
        ConditionalExpression newExpression = ast.newConditionalExpression();
        newExpression.setExpression((Expression) rewrite.createCopyTarget(conditionalExpression.getExpression()));
        newExpression.setThenExpression(getInversedExpression(rewrite, conditionalExpression.getThenExpression()));
        newExpression.setElseExpression(getInversedExpression(rewrite, conditionalExpression.getElseExpression()));
        return newExpression;
    }
    PrefixExpression prefixExpression = ast.newPrefixExpression();
    prefixExpression.setOperator(PrefixExpression.Operator.NOT);
    Expression renamedNameCopy = getRenamedNameCopy(provider, rewrite, expression);
    if (NecessaryParenthesesChecker.needsParentheses(renamedNameCopy, prefixExpression, PrefixExpression.OPERAND_PROPERTY)) {
        renamedNameCopy = getParenthesizedExpression(ast, renamedNameCopy);
    }
    prefixExpression.setOperand(renamedNameCopy);
    return prefixExpression;
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) Operator(org.eclipse.jdt.core.dom.InfixExpression.Operator) AST(org.eclipse.jdt.core.dom.AST) 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) BooleanLiteral(org.eclipse.jdt.core.dom.BooleanLiteral) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression)

Aggregations

ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)30 CastExpression (org.eclipse.jdt.core.dom.CastExpression)20 ASTNode (org.eclipse.jdt.core.dom.ASTNode)19 Expression (org.eclipse.jdt.core.dom.Expression)17 AST (org.eclipse.jdt.core.dom.AST)13 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)11 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)10 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)8 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)7 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)7 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)6 Assignment (org.eclipse.jdt.core.dom.Assignment)5 InstanceofExpression (org.eclipse.jdt.core.dom.InstanceofExpression)5 SimpleName (org.eclipse.jdt.core.dom.SimpleName)5 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)4 PostfixExpression (org.eclipse.jdt.core.dom.PostfixExpression)4 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)3 StructuralPropertyDescriptor (org.eclipse.jdt.core.dom.StructuralPropertyDescriptor)3 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)3 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)3