Search in sources :

Example 1 with PrefixExpression

use of org.eclipse.jdt.core.dom.PrefixExpression in project che 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 2 with PrefixExpression

use of org.eclipse.jdt.core.dom.PrefixExpression in project che 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());
        }
        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 : 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) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression)

Example 3 with PrefixExpression

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

the class AccessAnalyzer method visit.

@Override
public boolean visit(PrefixExpression node) {
    Expression operand = node.getOperand();
    if (!considerBinding(resolveBinding(operand), operand))
        return true;
    PrefixExpression.Operator operator = node.getOperator();
    if (operator != PrefixExpression.Operator.INCREMENT && operator != PrefixExpression.Operator.DECREMENT)
        return true;
    checkParent(node);
    fRewriter.replace(node, createInvocation(node.getAST(), node.getOperand(), node.getOperator().toString()), createGroupDescription(PREFIX_ACCESS));
    return false;
}
Also used : 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) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression)

Example 4 with PrefixExpression

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

the class BooleanEqualsRatherThanNullCheckRefactoring method visit.

@Override
public boolean visit(InfixExpression node) {
    if (hasOperator(node, CONDITIONAL_AND) || hasOperator(node, CONDITIONAL_OR)) {
        final Expression leftOperand = node.getLeftOperand();
        final Expression rightOperand = node.getRightOperand();
        final InfixExpression condition = as(leftOperand, InfixExpression.class);
        final boolean isNullCheck = hasOperator(condition, EQUALS);
        final boolean isAndExpr = hasOperator(node, CONDITIONAL_AND);
        if (!node.hasExtendedOperands() && (isNullCheck ^ isAndExpr) && condition != null && (hasOperator(condition, EQUALS) || hasOperator(condition, NOT_EQUALS))) {
            Expression firstExpr = null;
            if (isNullLiteral(condition.getLeftOperand())) {
                firstExpr = condition.getRightOperand();
            } else if (isNullLiteral(condition.getRightOperand())) {
                firstExpr = condition.getLeftOperand();
            }
            Expression secondExpr = null;
            final PrefixExpression negateSecondExpr = as(rightOperand, PrefixExpression.class);
            final boolean isPositiveExpr;
            if (negateSecondExpr != null && hasOperator(negateSecondExpr, NOT)) {
                secondExpr = negateSecondExpr.getOperand();
                isPositiveExpr = false;
            } else {
                secondExpr = rightOperand;
                isPositiveExpr = true;
            }
            if (firstExpr != null && hasType(firstExpr, "java.lang.Boolean") && isPassive(firstExpr) && match(new ASTMatcher(), firstExpr, secondExpr)) {
                replaceNullCheck(node, firstExpr, isNullCheck, isAndExpr, isPositiveExpr);
                return DO_NOT_VISIT_SUBTREE;
            }
        }
    }
    return VISIT_SUBTREE;
}
Also used : 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) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTMatcher(org.eclipse.jdt.core.dom.ASTMatcher)

Example 5 with PrefixExpression

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

the class BigDecimalRefactoring method visit.

@Override
public boolean visit(MethodInvocation node) {
    if (node.getExpression() == null) {
        return VISIT_SUBTREE;
    }
    if (getJavaMinorVersion() >= 5 && (isMethod(node, "java.math.BigDecimal", "valueOf", "long") || isMethod(node, "java.math.BigDecimal", "valueOf", "double"))) {
        final ITypeBinding typeBinding = node.getExpression().resolveTypeBinding();
        final Expression arg0 = arg0(node);
        if (arg0 instanceof NumberLiteral) {
            final NumberLiteral nb = (NumberLiteral) arg0;
            if (nb.getToken().contains(".")) {
                this.ctx.getRefactorings().replace(node, getClassInstanceCreatorNode((Name) node.getExpression(), nb.getToken()));
            } else if (ZERO_LONG_LITERAL_RE.matcher(nb.getToken()).matches()) {
                replaceWithQualifiedName(node, typeBinding, "ZERO");
            } else if (ONE_LONG_LITERAL_RE.matcher(nb.getToken()).matches()) {
                replaceWithQualifiedName(node, typeBinding, "ONE");
            } else if (TEN_LONG_LITERAL_RE.matcher(nb.getToken()).matches()) {
                replaceWithQualifiedName(node, typeBinding, "TEN");
            } else {
                return VISIT_SUBTREE;
            }
            return DO_NOT_VISIT_SUBTREE;
        }
    } else if (!(node.getParent() instanceof PrefixExpression) || !hasOperator((PrefixExpression) node.getParent(), NOT)) {
        return maybeReplaceEquals(true, node, node);
    }
    return VISIT_SUBTREE;
}
Also used : 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) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral) Name(org.eclipse.jdt.core.dom.Name)

Aggregations

PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)35 Expression (org.eclipse.jdt.core.dom.Expression)28 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)28 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)19 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)14 CastExpression (org.eclipse.jdt.core.dom.CastExpression)13 AST (org.eclipse.jdt.core.dom.AST)9 InstanceofExpression (org.eclipse.jdt.core.dom.InstanceofExpression)9 Assignment (org.eclipse.jdt.core.dom.Assignment)8 PostfixExpression (org.eclipse.jdt.core.dom.PostfixExpression)8 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)7 ASTNode (org.eclipse.jdt.core.dom.ASTNode)6 ArrayCreation (org.eclipse.jdt.core.dom.ArrayCreation)5 Operator (org.eclipse.jdt.core.dom.InfixExpression.Operator)5 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)5 List (java.util.List)4 ArrayAccess (org.eclipse.jdt.core.dom.ArrayAccess)4 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)4 NumberLiteral (org.eclipse.jdt.core.dom.NumberLiteral)4 SimpleName (org.eclipse.jdt.core.dom.SimpleName)4