Search in sources :

Example 6 with Operator

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

the class NecessaryParenthesesChecker method needsParenthesesInInfixExpression.

private static boolean needsParenthesesInInfixExpression(Expression expression, InfixExpression parentInfix, StructuralPropertyDescriptor locationInParent, ITypeBinding leftOperandType) {
    InfixExpression.Operator parentInfixOperator = parentInfix.getOperator();
    ITypeBinding rightOperandType;
    ITypeBinding parentInfixExprType;
    if (leftOperandType == null) {
        // parentInfix has bindings
        leftOperandType = parentInfix.getLeftOperand().resolveTypeBinding();
        rightOperandType = parentInfix.getRightOperand().resolveTypeBinding();
        parentInfixExprType = parentInfix.resolveTypeBinding();
    } else {
        rightOperandType = expression.resolveTypeBinding();
        parentInfixExprType = getInfixExpressionType(parentInfixOperator, leftOperandType, rightOperandType);
    }
    boolean isAllOperandsHaveSameType = isAllOperandsHaveSameType(parentInfix, leftOperandType, rightOperandType);
    if (locationInParent == InfixExpression.LEFT_OPERAND_PROPERTY) {
        //infix expressions are evaluated from left to right -> parentheses not needed
        return false;
    } else if (isAssociative(parentInfixOperator, parentInfixExprType, isAllOperandsHaveSameType)) {
        //left op (right) == (right) op left == right op left
        if (expression instanceof InfixExpression) {
            InfixExpression infixExpression = (InfixExpression) expression;
            Operator operator = infixExpression.getOperator();
            if (isStringType(parentInfixExprType)) {
                if (parentInfixOperator == InfixExpression.Operator.PLUS && operator == InfixExpression.Operator.PLUS && isStringType(infixExpression.resolveTypeBinding())) {
                    // "" + (2 + "") == "" + 2 + ""
                    return !isStringType(infixExpression.getLeftOperand().resolveTypeBinding()) && !isStringType(leftOperandType);
                }
                //"" + (1 + 2), "" + (1 - 2) etc
                return true;
            }
            if (parentInfixOperator != InfixExpression.Operator.TIMES)
                return false;
            if (operator == InfixExpression.Operator.TIMES)
                // x * (y * z) == x * y * z
                return false;
            if (operator == InfixExpression.Operator.REMAINDER || operator == InfixExpression.Operator.DIVIDE)
                // x * (y % z) != x * y % z , x * (y / z) == x * y / z rounding involved
                return true;
            return false;
        }
        return false;
    } else {
        return true;
    }
}
Also used : Operator(org.eclipse.jdt.core.dom.InfixExpression.Operator) Operator(org.eclipse.jdt.core.dom.InfixExpression.Operator) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression)

Example 7 with Operator

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

the class AdvancedQuickAssistProcessor method getSplitAndConditionProposals.

public static boolean getSplitAndConditionProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
    Operator andOperator = InfixExpression.Operator.CONDITIONAL_AND;
    // check that user invokes quick assist on infix expression
    if (!(node instanceof InfixExpression)) {
        return false;
    }
    InfixExpression infixExpression = (InfixExpression) node;
    if (infixExpression.getOperator() != andOperator) {
        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() == andOperator) {
        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, andOperator, offset, true, newOperands);
    Expression leftCondition = newOperands[0];
    Expression rightCondition = newOperands[1];
    // replace conditions in outer IfStatement
    rewrite.set(ifStatement, IfStatement.EXPRESSION_PROPERTY, leftCondition, null);
    // prepare inner IfStatement
    IfStatement innerIf = ast.newIfStatement();
    innerIf.setExpression(rightCondition);
    innerIf.setThenStatement((Statement) rewrite.createMoveTarget(ifStatement.getThenStatement()));
    Block innerBlock = ast.newBlock();
    innerBlock.statements().add(innerIf);
    Statement elseStatement = ifStatement.getElseStatement();
    if (elseStatement != null) {
        innerIf.setElseStatement((Statement) rewrite.createCopyTarget(elseStatement));
    }
    // replace outer thenStatement
    rewrite.replace(ifStatement.getThenStatement(), innerBlock, null);
    // add correction proposal
    String label = CorrectionMessages.AdvancedQuickAssistProcessor_splitAndCondition_description;
    //		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.SPLIT_AND_CONDITION);
    resultingCollections.add(proposal);
    return true;
}
Also used : Operator(org.eclipse.jdt.core.dom.InfixExpression.Operator) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) IfStatement(org.eclipse.jdt.core.dom.IfStatement) 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) 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) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block)

Example 8 with Operator

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

the class AdvancedQuickAssistProcessor method getExchangeOperandsProposals.

private static boolean getExchangeOperandsProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
    // check that user invokes quick assist on infix expression
    if (!(node instanceof InfixExpression)) {
        return false;
    }
    InfixExpression infixExpression = (InfixExpression) node;
    Operator operator = infixExpression.getOperator();
    if (operator != InfixExpression.Operator.CONDITIONAL_AND && operator != InfixExpression.Operator.AND && operator != InfixExpression.Operator.CONDITIONAL_OR && operator != InfixExpression.Operator.OR && operator != InfixExpression.Operator.EQUALS && operator != InfixExpression.Operator.NOT_EQUALS && operator != InfixExpression.Operator.LESS && operator != InfixExpression.Operator.LESS_EQUALS && operator != InfixExpression.Operator.GREATER && operator != InfixExpression.Operator.GREATER_EQUALS && operator != InfixExpression.Operator.PLUS && operator != InfixExpression.Operator.TIMES && operator != InfixExpression.Operator.XOR) {
        return false;
    }
    int offset = isOperatorSelected(infixExpression, context.getSelectionOffset(), context.getSelectionLength());
    if (offset == -1) {
        return false;
    }
    //  we could produce quick assist
    if (resultingCollections == null) {
        return true;
    }
    AST ast = infixExpression.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    // prepare left and right expressions
    Expression leftExpression = null;
    Expression rightExpression = null;
    InfixExpression currentExpression = infixExpression;
    leftExpression = combineOperands(rewrite, leftExpression, infixExpression.getLeftOperand(), false, operator);
    if (infixExpression.getRightOperand().getStartPosition() <= context.getSelectionOffset()) {
        leftExpression = combineOperands(rewrite, leftExpression, infixExpression.getRightOperand(), false, operator);
    } else {
        rightExpression = combineOperands(rewrite, rightExpression, infixExpression.getRightOperand(), false, operator);
    }
    for (Iterator<Expression> iter = currentExpression.extendedOperands().iterator(); iter.hasNext(); ) {
        Expression extendedOperand = iter.next();
        if (extendedOperand.getStartPosition() <= context.getSelectionOffset()) {
            leftExpression = combineOperands(rewrite, leftExpression, extendedOperand, false, operator);
        } else {
            rightExpression = combineOperands(rewrite, rightExpression, extendedOperand, false, operator);
        }
    }
    if (NecessaryParenthesesChecker.needsParentheses(leftExpression, infixExpression, InfixExpression.RIGHT_OPERAND_PROPERTY)) {
        leftExpression = getParenthesizedExpression(ast, leftExpression);
    }
    if (NecessaryParenthesesChecker.needsParentheses(rightExpression, infixExpression, InfixExpression.LEFT_OPERAND_PROPERTY)) {
        rightExpression = getParenthesizedExpression(ast, rightExpression);
    }
    if (operator == InfixExpression.Operator.LESS) {
        operator = InfixExpression.Operator.GREATER;
    } else if (operator == InfixExpression.Operator.LESS_EQUALS) {
        operator = InfixExpression.Operator.GREATER_EQUALS;
    } else if (operator == InfixExpression.Operator.GREATER) {
        operator = InfixExpression.Operator.LESS;
    } else if (operator == InfixExpression.Operator.GREATER_EQUALS) {
        operator = InfixExpression.Operator.LESS_EQUALS;
    }
    // create new infix expression
    InfixExpression newInfix = ast.newInfixExpression();
    newInfix.setOperator(operator);
    newInfix.setLeftOperand(rightExpression);
    newInfix.setRightOperand(leftExpression);
    rewrite.replace(infixExpression, newInfix, null);
    // add correction proposal
    String label = CorrectionMessages.AdvancedQuickAssistProcessor_exchangeOperands_description;
    //		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.EXCHANGE_OPERANDS);
    resultingCollections.add(proposal);
    return true;
}
Also used : Operator(org.eclipse.jdt.core.dom.InfixExpression.Operator) 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) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)

Example 9 with Operator

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

the class AdvancedQuickAssistProcessor method getJoinOrIfStatementsProposals.

private static boolean getJoinOrIfStatementsProposals(IInvocationContext context, ASTNode covering, ArrayList<ASTNode> coveredNodes, Collection<ICommandAccess> resultingCollections) {
    Operator orOperator = InfixExpression.Operator.CONDITIONAL_OR;
    if (coveredNodes.size() < 2)
        return false;
    // check that all covered nodes are IfStatement's with same 'then' statement and without 'else'
    String commonThenSource = null;
    for (Iterator<ASTNode> iter = coveredNodes.iterator(); iter.hasNext(); ) {
        ASTNode node = iter.next();
        if (!(node instanceof IfStatement))
            return false;
        //
        IfStatement ifStatement = (IfStatement) node;
        if (ifStatement.getElseStatement() != null)
            return false;
        //
        Statement thenStatement = ifStatement.getThenStatement();
        try {
            String thenSource = context.getCompilationUnit().getBuffer().getText(thenStatement.getStartPosition(), thenStatement.getLength());
            if (commonThenSource == null) {
                commonThenSource = thenSource;
            } else {
                if (!commonThenSource.equals(thenSource))
                    return false;
            }
        } catch (Throwable e) {
            return false;
        }
    }
    if (resultingCollections == null) {
        return true;
    }
    //
    final AST ast = covering.getAST();
    final ASTRewrite rewrite = ASTRewrite.create(ast);
    // prepare OR'ed condition
    InfixExpression condition = null;
    boolean hasRightOperand = false;
    Statement thenStatement = null;
    for (Iterator<ASTNode> iter = coveredNodes.iterator(); iter.hasNext(); ) {
        IfStatement ifStatement = (IfStatement) iter.next();
        if (thenStatement == null)
            thenStatement = (Statement) rewrite.createCopyTarget(ifStatement.getThenStatement());
        if (condition == null) {
            condition = ast.newInfixExpression();
            condition.setOperator(orOperator);
            condition.setLeftOperand(getParenthesizedExpressionIfNeeded(ast, rewrite, ifStatement.getExpression(), condition, InfixExpression.LEFT_OPERAND_PROPERTY));
        } else if (!hasRightOperand) {
            condition.setRightOperand(getParenthesizedExpressionIfNeeded(ast, rewrite, ifStatement.getExpression(), condition, InfixExpression.RIGHT_OPERAND_PROPERTY));
            hasRightOperand = true;
        } else {
            InfixExpression newCondition = ast.newInfixExpression();
            newCondition.setOperator(orOperator);
            newCondition.setLeftOperand(condition);
            newCondition.setRightOperand(getParenthesizedExpressionIfNeeded(ast, rewrite, ifStatement.getExpression(), condition, InfixExpression.RIGHT_OPERAND_PROPERTY));
            condition = newCondition;
        }
    }
    // prepare new IfStatement with OR'ed condition
    IfStatement newIf = ast.newIfStatement();
    newIf.setExpression(condition);
    newIf.setThenStatement(thenStatement);
    //
    ListRewrite listRewriter = null;
    for (Iterator<ASTNode> iter = coveredNodes.iterator(); iter.hasNext(); ) {
        IfStatement ifStatement = (IfStatement) iter.next();
        if (listRewriter == null) {
            Block sourceBlock = (Block) ifStatement.getParent();
            //int insertIndex = sourceBlock.statements().indexOf(ifStatement);
            listRewriter = rewrite.getListRewrite(sourceBlock, (ChildListPropertyDescriptor) ifStatement.getLocationInParent());
        }
        if (newIf != null) {
            listRewriter.replace(ifStatement, newIf, null);
            newIf = null;
        } else {
            listRewriter.remove(ifStatement, null);
        }
    }
    // add correction proposal
    String label = CorrectionMessages.AdvancedQuickAssistProcessor_joinWithOr_description;
    //		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.JOIN_IF_STATEMENTS_WITH_OR);
    resultingCollections.add(proposal);
    return true;
}
Also used : Operator(org.eclipse.jdt.core.dom.InfixExpression.Operator) 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) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) ChildListPropertyDescriptor(org.eclipse.jdt.core.dom.ChildListPropertyDescriptor) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) IfStatement(org.eclipse.jdt.core.dom.IfStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block)

Example 10 with Operator

use of org.eclipse.jdt.core.dom.InfixExpression.Operator in project Main by SpartanRefactoring.

the class InfixComparisonSizeToZero method replacement.

@Override
public ASTNode replacement(final InfixExpression x) {
    final Operator $ = x.getOperator();
    if (!iz.comparison($))
        return null;
    final Expression right = right(x), left = left(x);
    return !validTypes(right, left) ? null : iz.methodInvocation(left) ? replacement($, az.methodInvocation(left), right) : replacement(op.conjugate($), az.methodInvocation(right), left);
}
Also used : Operator(org.eclipse.jdt.core.dom.InfixExpression.Operator) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression)

Aggregations

Operator (org.eclipse.jdt.core.dom.InfixExpression.Operator)18 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)11 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)11 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)9 AST (org.eclipse.jdt.core.dom.AST)6 Expression (org.eclipse.jdt.core.dom.Expression)6 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)6 CastExpression (org.eclipse.jdt.core.dom.CastExpression)5 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)5 InstanceofExpression (org.eclipse.jdt.core.dom.InstanceofExpression)5 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)5 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)5 Image (org.eclipse.swt.graphics.Image)4 Iterator (java.util.Iterator)3 AssertStatement (org.eclipse.jdt.core.dom.AssertStatement)3 BreakStatement (org.eclipse.jdt.core.dom.BreakStatement)3 ContinueStatement (org.eclipse.jdt.core.dom.ContinueStatement)3 DoStatement (org.eclipse.jdt.core.dom.DoStatement)3 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)3 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)3