Search in sources :

Example 31 with PrefixExpression

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

the class ConvertForLoopOperation method validateUpdaters.

/*
	 * Must be one of:
	 * <ul>
	 * <li>[indexBinding]++</li>
	 * <li>++[indexBinding]</li>
	 * <li>[indexBinding]+= 1</li>
	 * <li>[indexBinding]= [indexBinding] + 1</li>
	 * <li>[indexBinding]= 1 + [indexBinding]</li>
	 * <ul>
	 */
private boolean validateUpdaters(ForStatement statement) {
    List<Expression> updaters = statement.updaters();
    if (updaters.size() != 1)
        return false;
    Expression updater = updaters.get(0);
    if (updater instanceof PostfixExpression) {
        PostfixExpression postfix = (PostfixExpression) updater;
        if (!PostfixExpression.Operator.INCREMENT.equals(postfix.getOperator()))
            return false;
        IBinding binding = getBinding(postfix.getOperand());
        if (!fIndexBinding.equals(binding))
            return false;
        return true;
    } else if (updater instanceof PrefixExpression) {
        PrefixExpression prefix = (PrefixExpression) updater;
        if (!PrefixExpression.Operator.INCREMENT.equals(prefix.getOperator()))
            return false;
        IBinding binding = getBinding(prefix.getOperand());
        if (!fIndexBinding.equals(binding))
            return false;
        return true;
    } else if (updater instanceof Assignment) {
        Assignment assignment = (Assignment) updater;
        Expression left = assignment.getLeftHandSide();
        IBinding binding = getBinding(left);
        if (!fIndexBinding.equals(binding))
            return false;
        if (Assignment.Operator.PLUS_ASSIGN.equals(assignment.getOperator())) {
            return isOneLiteral(assignment.getRightHandSide());
        } else if (Assignment.Operator.ASSIGN.equals(assignment.getOperator())) {
            Expression right = assignment.getRightHandSide();
            if (!(right instanceof InfixExpression))
                return false;
            InfixExpression infixExpression = (InfixExpression) right;
            Expression leftOperand = infixExpression.getLeftOperand();
            IBinding leftBinding = getBinding(leftOperand);
            Expression rightOperand = infixExpression.getRightOperand();
            IBinding rightBinding = getBinding(rightOperand);
            if (fIndexBinding.equals(leftBinding)) {
                return isOneLiteral(rightOperand);
            } else if (fIndexBinding.equals(rightBinding)) {
                return isOneLiteral(leftOperand);
            }
        }
    }
    return false;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) IBinding(org.eclipse.jdt.core.dom.IBinding) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression)

Example 32 with PrefixExpression

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

the class XORRatherThanDuplicateConditionsRefactoring method getBasisExpression.

private Expression getBasisExpression(final Expression originalExpr, final AtomicBoolean isExprPositive) {
    Expression basisExpr = null;
    final PrefixExpression negateExpr = as(originalExpr, PrefixExpression.class);
    if (hasOperator(negateExpr, NOT)) {
        basisExpr = negateExpr.getOperand();
        isExprPositive.set(false);
    } else {
        basisExpr = originalExpr;
        isExprPositive.set(true);
    }
    return basisExpr;
}
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)

Example 33 with PrefixExpression

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

the class ORConditionRatherThanRedundantClausesRefactoring method getBasisExpression.

private Expression getBasisExpression(final Expression originalExpr, final AtomicBoolean isExprPositive) {
    final Expression basisExpr;
    final PrefixExpression negateExpr = as(originalExpr, PrefixExpression.class);
    if (hasOperator(negateExpr, NOT)) {
        basisExpr = negateExpr.getOperand();
        isExprPositive.set(false);
    } else {
        basisExpr = originalExpr;
        isExprPositive.set(true);
    }
    return basisExpr;
}
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)

Example 34 with PrefixExpression

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

the class UpdateSetRatherThanTestingFirstRefactoring method visit.

@Override
public boolean visit(IfStatement node) {
    final Statement elseStmt = node.getElseStatement();
    final Statement thenStmt = node.getThenStatement();
    final PrefixExpression pe = as(node.getExpression(), PrefixExpression.class);
    if (hasOperator(pe, NOT)) {
        return maybeReplaceSetContains(node, pe.getOperand(), thenStmt, elseStmt, false);
    } else {
        return maybeReplaceSetContains(node, node.getExpression(), elseStmt, thenStmt, true);
    }
}
Also used : Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression)

Example 35 with PrefixExpression

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

the class BigNumberRefactoring method visit.

@Override
public boolean visit(MethodInvocation node) {
    if (node.getExpression() == null) {
        return VISIT_SUBTREE;
    }
    if (getJavaMinorVersion() >= 5 && (isMethod(node, "java.math.BigInteger", "valueOf", "long") || 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 String token = ((NumberLiteral) arg0).getToken().replaceFirst("[lLfFdD]$", "");
            if (token.contains(".") && hasType(typeBinding, "java.math.BigDecimal")) {
                this.ctx.getRefactorings().replace(node, getClassInstanceCreatorNode((Name) node.getExpression(), token));
            } else if (ZERO_LONG_LITERAL_RE.matcher(token).matches()) {
                replaceWithQualifiedName(node, typeBinding, "ZERO");
            } else if (ONE_LONG_LITERAL_RE.matcher(token).matches()) {
                replaceWithQualifiedName(node, typeBinding, "ONE");
            } else if (TEN_LONG_LITERAL_RE.matcher(token).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)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