Search in sources :

Example 56 with PrefixExpression

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

the class ObsoleteTernaryOperatorRatherThanDuplicateConditionsCleanUp method getBasisExpression.

private Expression getBasisExpression(final Expression originalExpression, final AtomicBoolean isExprPositive) {
    Expression basisExpression;
    PrefixExpression negateExpression = ASTNodes.as(originalExpression, PrefixExpression.class);
    if (ASTNodes.hasOperator(negateExpression, PrefixExpression.Operator.NOT)) {
        basisExpression = negateExpression.getOperand();
        isExprPositive.lazySet(false);
    } else {
        basisExpression = originalExpression;
        isExprPositive.lazySet(true);
    }
    return basisExpression;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression)

Example 57 with PrefixExpression

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

the class UpdateSetRatherThanTestingFirstCleanUp method visit.

@Override
public boolean visit(final IfStatement visited) {
    Statement elseStatement = visited.getElseStatement();
    Statement thenStatement = visited.getThenStatement();
    PrefixExpression prefixExpression = ASTNodes.as(visited.getExpression(), PrefixExpression.class);
    if (ASTNodes.hasOperator(prefixExpression, PrefixExpression.Operator.NOT)) {
        return maybeReplaceSetContains(visited, prefixExpression.getOperand(), thenStatement, elseStatement, false);
    }
    return maybeReplaceSetContains(visited, visited.getExpression(), elseStatement, thenStatement, 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 58 with PrefixExpression

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

the class ASTNodes method getIntegerLiteral.

/**
 * Integer literal.
 *
 * @param input The input
 * @return Integer literal.
 */
public static Long getIntegerLiteral(final Expression input) {
    if (input == null) {
        return null;
    }
    Object number = input.resolveConstantExpressionValue();
    if (number instanceof Short) {
        return (long) ((Short) number).intValue();
    }
    if (number instanceof Integer) {
        return (long) ((Integer) number).intValue();
    }
    if (number instanceof Long) {
        return (Long) number;
    }
    InfixExpression operation = as(input, InfixExpression.class);
    if (operation != null && hasOperator(operation, // All numerical operators
    InfixExpression.Operator.AND, InfixExpression.Operator.DIVIDE, InfixExpression.Operator.LEFT_SHIFT, InfixExpression.Operator.MINUS, InfixExpression.Operator.OR, InfixExpression.Operator.PLUS, InfixExpression.Operator.REMAINDER, InfixExpression.Operator.RIGHT_SHIFT_SIGNED, InfixExpression.Operator.RIGHT_SHIFT_UNSIGNED, InfixExpression.Operator.TIMES, InfixExpression.Operator.XOR)) {
        List<Expression> operands = allOperands(operation);
        Long leftValue = getIntegerLiteral(operands.remove(0));
        if (leftValue == null) {
            return null;
        }
        long result = leftValue;
        for (Expression operand : operands) {
            Long newValue = getIntegerLiteral(operand);
            if (newValue == null) {
                return null;
            }
            if (hasOperator(operation, InfixExpression.Operator.PLUS)) {
                result = result + newValue;
            } else if (hasOperator(operation, InfixExpression.Operator.MINUS)) {
                result = result - newValue;
            } else if (hasOperator(operation, InfixExpression.Operator.TIMES)) {
                result = result * newValue;
            } else if (hasOperator(operation, InfixExpression.Operator.AND)) {
                result = result & newValue;
            } else if (hasOperator(operation, InfixExpression.Operator.OR)) {
                result = result | newValue;
            } else if (hasOperator(operation, InfixExpression.Operator.XOR)) {
                result = result ^ newValue;
            } else if (hasOperator(operation, InfixExpression.Operator.LEFT_SHIFT)) {
                result = result << newValue;
            } else if (hasOperator(operation, InfixExpression.Operator.REMAINDER)) {
                result = result % newValue;
            } else if (hasOperator(operation, InfixExpression.Operator.RIGHT_SHIFT_SIGNED)) {
                result = result >> newValue;
            } else if (hasOperator(operation, InfixExpression.Operator.RIGHT_SHIFT_UNSIGNED)) {
                result = result >>> newValue;
            } else if (hasOperator(operation, InfixExpression.Operator.DIVIDE) && result % newValue == 0) {
                result = result / newValue;
            } else {
                return null;
            }
        }
        return result;
    }
    PrefixExpression negativeContant = as(input, PrefixExpression.class);
    if (negativeContant != null && hasOperator(negativeContant, PrefixExpression.Operator.MINUS)) {
        Long value = getIntegerLiteral(negativeContant.getOperand());
        if (value != null) {
            return -value;
        }
    }
    return null;
}
Also used : ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression)

Example 59 with PrefixExpression

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

the class ASTNodeFactory method getNegatedOperation.

private Expression getNegatedOperation(final InfixExpression booleanOperation, final InfixExpression.Operator negatedOperator, final boolean isMove) {
    List<Expression> allOperands = ASTNodes.allOperands(booleanOperation);
    List<Expression> allTargetOperands;
    if (ASTNodes.hasOperator(booleanOperation, InfixExpression.Operator.CONDITIONAL_AND, InfixExpression.Operator.CONDITIONAL_OR, InfixExpression.Operator.AND, InfixExpression.Operator.OR)) {
        allTargetOperands = new ArrayList<>(allOperands.size());
        for (Expression booleanOperand : allOperands) {
            Expression negatedOperand = negate(booleanOperand, isMove);
            if (negatedOperand != null) {
                allTargetOperands.add(negatedOperand);
            } else {
                PrefixExpression prefixExpression = newPrefixExpression(PrefixExpression.Operator.NOT, isMove ? createMoveTarget(booleanOperand) : createCopyTarget(booleanOperand));
                allTargetOperands.add(prefixExpression);
            }
        }
    } else {
        allTargetOperands = new ArrayList<>(allOperands.size());
        if (isMove) {
            for (Expression anOperand : allOperands) {
                allTargetOperands.add(createMoveTarget(anOperand));
            }
        } else {
            for (Expression anOperand : allOperands) {
                allTargetOperands.add(createCopyTarget(anOperand));
            }
        }
    }
    return newInfixExpression(negatedOperator, allTargetOperands);
}
Also used : ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression)

Example 60 with PrefixExpression

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

the class AbstractPrimitiveRatherThanWrapperCleanUp method isNotNull.

private boolean isNotNull(final Expression expression) {
    if (expression instanceof ParenthesizedExpression) {
        ParenthesizedExpression parenthesizedExpression = (ParenthesizedExpression) expression;
        return isNotNull(parenthesizedExpression.getExpression());
    }
    if (expression instanceof ConditionalExpression) {
        ConditionalExpression prefixExpression = (ConditionalExpression) expression;
        return isNotNull(prefixExpression.getThenExpression()) && isNotNull(prefixExpression.getElseExpression());
    }
    if (getLiteralClass().equals(expression.getClass())) {
        return true;
    }
    if (expression instanceof QualifiedName) {
        QualifiedName qualifiedName = (QualifiedName) expression;
        return ASTNodes.hasType(qualifiedName.getQualifier(), getWrapperFullyQualifiedName()) && (ASTNodes.isField(qualifiedName, getWrapperFullyQualifiedName(), getSafeInConstants()) || ASTNodes.isField(qualifiedName, getPrimitiveTypeName(), getSafeInConstants()));
    }
    if (expression instanceof InfixExpression) {
        InfixExpression infixExpression = (InfixExpression) expression;
        return getInfixInSafeOperators().contains(infixExpression.getOperator());
    }
    if (expression instanceof PrefixExpression) {
        PrefixExpression prefixExpression = (PrefixExpression) expression;
        return getPrefixInSafeOperators().contains(prefixExpression.getOperator());
    }
    if (expression instanceof PostfixExpression) {
        PostfixExpression postfixExpression = (PostfixExpression) expression;
        return getPostfixInSafeOperators().contains(postfixExpression.getOperator());
    }
    if (expression instanceof CastExpression) {
        CastExpression castExpression = (CastExpression) expression;
        return ASTNodes.hasType(castExpression.getType().resolveBinding(), getPrimitiveTypeName()) || ASTNodes.hasType(castExpression.getType().resolveBinding(), getWrapperFullyQualifiedName()) && isNotNull(castExpression.getExpression());
    }
    if (expression instanceof MethodInvocation) {
        MethodInvocation methodInvocation = (MethodInvocation) expression;
        return // $NON-NLS-1$
        ASTNodes.usesGivenSignature(methodInvocation, getWrapperFullyQualifiedName(), "valueOf", getPrimitiveTypeName()) || getParsingMethodName(getWrapperFullyQualifiedName()) != null && (// $NON-NLS-1$
        ASTNodes.usesGivenSignature(methodInvocation, getWrapperFullyQualifiedName(), "valueOf", String.class.getCanonicalName()) || // $NON-NLS-1$
        ASTNodes.usesGivenSignature(methodInvocation, getWrapperFullyQualifiedName(), "valueOf", String.class.getCanonicalName(), int.class.getSimpleName()));
    }
    if (expression instanceof ClassInstanceCreation) {
        ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation) expression;
        List<Expression> classInstanceCreationArguments = classInstanceCreation.arguments();
        if (classInstanceCreationArguments.size() == 1) {
            Expression arg0 = classInstanceCreationArguments.get(0);
            return ASTNodes.hasType(arg0, String.class.getCanonicalName());
        }
    }
    return false;
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) 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) CastExpression(org.eclipse.jdt.core.dom.CastExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) CastExpression(org.eclipse.jdt.core.dom.CastExpression)

Aggregations

PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)64 Expression (org.eclipse.jdt.core.dom.Expression)51 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)50 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)31 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)24 CastExpression (org.eclipse.jdt.core.dom.CastExpression)19 PostfixExpression (org.eclipse.jdt.core.dom.PostfixExpression)16 InstanceofExpression (org.eclipse.jdt.core.dom.InstanceofExpression)14 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)12 AST (org.eclipse.jdt.core.dom.AST)11 ASTNode (org.eclipse.jdt.core.dom.ASTNode)10 Assignment (org.eclipse.jdt.core.dom.Assignment)10 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)9 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)9 Operator (org.eclipse.jdt.core.dom.InfixExpression.Operator)7 NumberLiteral (org.eclipse.jdt.core.dom.NumberLiteral)7 SimpleName (org.eclipse.jdt.core.dom.SimpleName)7 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)7 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)6 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)6