Search in sources :

Example 36 with PrefixExpression

use of org.eclipse.jdt.core.dom.PrefixExpression in project AutoRefactor by JnRouvignac.

the class PushNegationDownRefactoring method visit.

@Override
public boolean visit(PrefixExpression node) {
    if (!hasOperator(node, NOT)) {
        return VISIT_SUBTREE;
    }
    final ASTBuilder b = ctx.getASTBuilder();
    final Refactorings r = ctx.getRefactorings();
    final Expression operand = removeParentheses(node.getOperand());
    if (operand instanceof PrefixExpression) {
        final PrefixExpression pe = (PrefixExpression) operand;
        if (hasOperator(pe, NOT)) {
            r.replace(node, b.move(pe.getOperand()));
            return DO_NOT_VISIT_SUBTREE;
        }
    } else if (operand instanceof InfixExpression) {
        final InfixExpression ie = (InfixExpression) operand;
        final Operator reverseOp = (Operator) OperatorEnum.getOperator(ie).getReverseBooleanOperator();
        if (reverseOp != null) {
            List<Expression> allOperands = new ArrayList<Expression>(allOperands(ie));
            if (Arrays.<Operator>asList(CONDITIONAL_AND, CONDITIONAL_OR, AND, OR).contains(ie.getOperator())) {
                for (ListIterator<Expression> it = allOperands.listIterator(); it.hasNext(); ) {
                    it.set(b.negate(it.next()));
                }
            } else {
                allOperands = b.move(allOperands);
            }
            r.replace(node, b.parenthesize(b.infixExpr(reverseOp, allOperands)));
            return DO_NOT_VISIT_SUBTREE;
        }
    } else {
        final Boolean constant = getBooleanLiteral(operand);
        if (constant != null) {
            r.replace(node, b.boolean0(!constant));
            return DO_NOT_VISIT_SUBTREE;
        }
    }
    return VISIT_SUBTREE;
}
Also used : ASTHelper.hasOperator(org.autorefactor.refactoring.ASTHelper.hasOperator) Operator(org.eclipse.jdt.core.dom.InfixExpression.Operator) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) Refactorings(org.autorefactor.refactoring.Refactorings) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ArrayList(java.util.ArrayList) List(java.util.List) ListIterator(java.util.ListIterator) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 37 with PrefixExpression

use of org.eclipse.jdt.core.dom.PrefixExpression in project eclipse.jdt.ls by eclipse.

the class GetterSetterUtil method getAssignedValue.

/**
 * Converts an assignment, postfix expression or prefix expression into an
 * assignable equivalent expression using the getter.
 *
 * @param node
 *            the assignment/prefix/postfix node
 * @param astRewrite
 *            the astRewrite to use
 * @param getterExpression
 *            the expression to insert for read accesses or
 *            <code>null</code> if such an expression does not exist
 * @param variableType
 *            the type of the variable that the result will be assigned to
 * @param is50OrHigher
 *            <code>true</code> if a 5.0 or higher environment can be used
 * @return an expression that can be assigned to the type variableType with
 *         node being replaced by a equivalent expression using the getter
 */
public static Expression getAssignedValue(ASTNode node, ASTRewrite astRewrite, Expression getterExpression, ITypeBinding variableType, boolean is50OrHigher) {
    InfixExpression.Operator op = null;
    AST ast = astRewrite.getAST();
    if (isNotInBlock(node)) {
        return null;
    }
    if (node.getNodeType() == ASTNode.ASSIGNMENT) {
        Assignment assignment = ((Assignment) node);
        Expression rightHandSide = assignment.getRightHandSide();
        Expression copiedRightOp = (Expression) astRewrite.createCopyTarget(rightHandSide);
        if (assignment.getOperator() == Operator.ASSIGN) {
            ITypeBinding rightHandSideType = rightHandSide.resolveTypeBinding();
            copiedRightOp = createNarrowCastIfNessecary(copiedRightOp, rightHandSideType, ast, variableType, is50OrHigher);
            return copiedRightOp;
        }
        if (getterExpression != null) {
            InfixExpression infix = ast.newInfixExpression();
            infix.setLeftOperand(getterExpression);
            infix.setOperator(ASTNodes.convertToInfixOperator(assignment.getOperator()));
            ITypeBinding infixType = infix.resolveTypeBinding();
            if (NecessaryParenthesesChecker.needsParenthesesForRightOperand(rightHandSide, infix, variableType)) {
                ParenthesizedExpression p = ast.newParenthesizedExpression();
                p.setExpression(copiedRightOp);
                copiedRightOp = p;
            }
            infix.setRightOperand(copiedRightOp);
            return createNarrowCastIfNessecary(infix, infixType, ast, variableType, is50OrHigher);
        }
    } else if (node.getNodeType() == ASTNode.POSTFIX_EXPRESSION) {
        PostfixExpression po = (PostfixExpression) node;
        if (po.getOperator() == PostfixExpression.Operator.INCREMENT) {
            op = InfixExpression.Operator.PLUS;
        }
        if (po.getOperator() == PostfixExpression.Operator.DECREMENT) {
            op = InfixExpression.Operator.MINUS;
        }
    } else if (node.getNodeType() == ASTNode.PREFIX_EXPRESSION) {
        PrefixExpression pe = (PrefixExpression) node;
        if (pe.getOperator() == PrefixExpression.Operator.INCREMENT) {
            op = InfixExpression.Operator.PLUS;
        }
        if (pe.getOperator() == PrefixExpression.Operator.DECREMENT) {
            op = InfixExpression.Operator.MINUS;
        }
    }
    if (op != null && getterExpression != null) {
        return createInfixInvocationFromPostPrefixExpression(op, getterExpression, ast, variableType, is50OrHigher);
    }
    return null;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) AST(org.eclipse.jdt.core.dom.AST) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression)

Example 38 with PrefixExpression

use of org.eclipse.jdt.core.dom.PrefixExpression in project eclipse.jdt.ls by eclipse.

the class NecessaryParenthesesChecker method needsParentheses.

/**
 * Does the <code>expression</code> need parentheses when inserted into <code>parent</code> at
 * <code>locationInParent</code> ?
 *
 * @param expression the expression
 * @param parent the parent node
 * @param locationInParent location of expression in the parent
 * @param leftOperandType the type of the left operand in <code>parent</code> if
 *            <code>parent</code> is an infix expression with no bindings and
 *            <code>expression</code> is the right operand in it, <code>null</code> otherwise
 * @return <code>true</code> if <code>expression</code> needs parentheses, <code>false</code>
 *         otherwise.
 *
 * @since 3.9
 */
private static boolean needsParentheses(Expression expression, ASTNode parent, StructuralPropertyDescriptor locationInParent, ITypeBinding leftOperandType) {
    if (!expressionTypeNeedsParentheses(expression)) {
        return false;
    }
    if (!locationNeedsParentheses(locationInParent)) {
        return false;
    }
    if (parent instanceof Expression) {
        Expression parentExpression = (Expression) parent;
        if (expression instanceof PrefixExpression) {
            // see bug 405096
            return needsParenthesesForPrefixExpression(parentExpression, ((PrefixExpression) expression).getOperator());
        }
        if (expression instanceof ArrayCreation) {
            // see bug 394721
            return parentExpression instanceof ArrayAccess && ((ArrayCreation) expression).getInitializer() == null;
        }
        int expressionPrecedence = OperatorPrecedence.getExpressionPrecedence(expression);
        int parentPrecedence = OperatorPrecedence.getExpressionPrecedence(parentExpression);
        if (expressionPrecedence > parentPrecedence) {
            // (opEx) opParent and opEx binds more -> parentheses not needed
            return false;
        }
        if (expressionPrecedence < parentPrecedence) {
            // (opEx) opParent and opEx binds less -> parentheses needed
            return true;
        }
        if (parentExpression instanceof InfixExpression) {
            return needsParenthesesInInfixExpression(expression, (InfixExpression) parentExpression, locationInParent, leftOperandType);
        }
        if (parentExpression instanceof ConditionalExpression && locationInParent == ConditionalExpression.EXPRESSION_PROPERTY) {
            return true;
        }
        return false;
    }
    return true;
}
Also used : ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) Expression(org.eclipse.jdt.core.dom.Expression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) ArrayCreation(org.eclipse.jdt.core.dom.ArrayCreation) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression)

Example 39 with PrefixExpression

use of org.eclipse.jdt.core.dom.PrefixExpression in project whole by wholeplatform.

the class CompilationUnitBuilder method newPrefixExpression.

public PrefixExpression newPrefixExpression(Expression exp, String op) {
    PrefixExpression prefixExp = ast.newPrefixExpression();
    prefixExp.setOperand(exp);
    prefixExp.setOperator(PrefixExpression.Operator.toOperator(op));
    return prefixExp;
}
Also used : PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression)

Example 40 with PrefixExpression

use of org.eclipse.jdt.core.dom.PrefixExpression 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)

Aggregations

PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)60 Expression (org.eclipse.jdt.core.dom.Expression)48 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)47 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)27 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)22 CastExpression (org.eclipse.jdt.core.dom.CastExpression)19 PostfixExpression (org.eclipse.jdt.core.dom.PostfixExpression)15 InstanceofExpression (org.eclipse.jdt.core.dom.InstanceofExpression)12 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)12 ASTNode (org.eclipse.jdt.core.dom.ASTNode)10 AST (org.eclipse.jdt.core.dom.AST)9 Assignment (org.eclipse.jdt.core.dom.Assignment)9 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)9 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)9 NumberLiteral (org.eclipse.jdt.core.dom.NumberLiteral)7 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)7 SimpleName (org.eclipse.jdt.core.dom.SimpleName)6 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)6 List (java.util.List)5 ASTRewrite (org.autorefactor.jdt.core.dom.ASTRewrite)5