Search in sources :

Example 96 with InfixExpression

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

the class ObsoleteXORRatherThanDuplicateConditionsCleanUp method replaceDuplicateExpression.

private void replaceDuplicateExpression(final InfixExpression node, final Expression firstExpression, final Expression secondExpression, final AtomicBoolean isFirstExprPositive, final AtomicBoolean isSecondExprPositive) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteXORRatherThanDuplicateConditionsCleanUp_description);
    InfixExpression newInfixExpression = ast.newInfixExpression();
    newInfixExpression.setLeftOperand(ASTNodes.createMoveTarget(rewrite, firstExpression));
    newInfixExpression.setRightOperand(ASTNodes.createMoveTarget(rewrite, secondExpression));
    if (isFirstExprPositive.get() == isSecondExprPositive.get()) {
        newInfixExpression.setOperator(InfixExpression.Operator.EQUALS);
    } else {
        newInfixExpression.setOperator(InfixExpression.Operator.XOR);
    }
    ASTNodes.replaceButKeepComment(rewrite, node, newInfixExpression, group);
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 97 with InfixExpression

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

the class ObsoleteJava7HashRatherThanEclipseJava6HashCleanUp method isGreatNumberValid.

private boolean isGreatNumberValid(final CollectedData data, final CastExpression newHash) {
    OrderedInfixExpression<Expression, InfixExpression> orderedBitwise = ASTNodes.orderedInfix(newHash.getExpression(), Expression.class, InfixExpression.class);
    if (ASTNodes.hasType(newHash, int.class.getSimpleName()) && orderedBitwise != null && ASTNodes.hasType(newHash.getExpression(), long.class.getSimpleName(), double.class.getSimpleName()) && InfixExpression.Operator.XOR.equals(orderedBitwise.getOperator())) {
        SimpleName field = getField(orderedBitwise.getFirstOperand());
        InfixExpression moveExpression = orderedBitwise.getSecondOperand();
        if (field != null && moveExpression != null && !ASTNodes.isSameVariable(field, data.getPrimeId()) && !ASTNodes.isSameVariable(field, data.getResultId()) && ASTNodes.hasOperator(moveExpression, InfixExpression.Operator.RIGHT_SHIFT_UNSIGNED)) {
            SimpleName againFieldName = getField(moveExpression.getLeftOperand());
            Long hash = ASTNodes.getIntegerLiteral(moveExpression.getRightOperand());
            if (againFieldName != null && ASTNodes.isSameVariable(againFieldName, field) && Long.valueOf(32L).equals(hash)) {
                if (data.isTempValueUsed()) {
                    data.getFields().add(ASTNodes.getUnparenthesedExpression(againFieldName));
                    return true;
                }
                if (ASTNodes.isSameVariable(data.getTempVar(), field)) {
                    data.setTempValueUsed(true);
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) 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) OrderedInfixExpression(org.autorefactor.jdt.internal.corext.dom.OrderedInfixExpression) SimpleName(org.eclipse.jdt.core.dom.SimpleName) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) OrderedInfixExpression(org.autorefactor.jdt.internal.corext.dom.OrderedInfixExpression)

Example 98 with InfixExpression

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

the class ObsoleteOperandFactorizationCleanUp method visit.

@Override
public boolean visit(final InfixExpression visited) {
    if (!visited.hasExtendedOperands() && ASTNodes.hasOperator(visited, InfixExpression.Operator.CONDITIONAL_OR, InfixExpression.Operator.OR)) {
        InfixExpression firstCondition = ASTNodes.as(visited.getLeftOperand(), InfixExpression.class);
        InfixExpression secondCondition = ASTNodes.as(visited.getRightOperand(), InfixExpression.class);
        if (firstCondition != null && secondCondition != null && !firstCondition.hasExtendedOperands() && !secondCondition.hasExtendedOperands() && ASTNodes.hasOperator(firstCondition, InfixExpression.Operator.CONDITIONAL_AND, InfixExpression.Operator.AND) && ASTNodes.hasOperator(secondCondition, InfixExpression.Operator.CONDITIONAL_AND, InfixExpression.Operator.AND) && ASTNodes.isPrimitive(firstCondition.getLeftOperand()) && ASTNodes.isPrimitive(firstCondition.getRightOperand()) && ASTNodes.isPrimitive(secondCondition.getLeftOperand()) && ASTNodes.isPrimitive(secondCondition.getRightOperand()) && ASTNodes.isPassiveWithoutFallingThrough(firstCondition.getLeftOperand()) && ASTNodes.isPassiveWithoutFallingThrough(firstCondition.getRightOperand()) && ASTNodes.isPassiveWithoutFallingThrough(secondCondition.getLeftOperand()) && ASTNodes.isPassiveWithoutFallingThrough(secondCondition.getRightOperand())) {
            return maybeReplaceDuplicateExpression(visited, firstCondition, firstCondition.getLeftOperand(), secondCondition.getLeftOperand(), firstCondition.getRightOperand(), secondCondition.getRightOperand()) && maybeReplaceDuplicateExpression(visited, firstCondition, firstCondition.getLeftOperand(), secondCondition.getRightOperand(), firstCondition.getRightOperand(), secondCondition.getLeftOperand()) && maybeReplaceDuplicateExpression(visited, firstCondition, firstCondition.getRightOperand(), secondCondition.getLeftOperand(), firstCondition.getLeftOperand(), secondCondition.getRightOperand()) && maybeReplaceDuplicateExpression(visited, firstCondition, firstCondition.getRightOperand(), secondCondition.getRightOperand(), firstCondition.getLeftOperand(), secondCondition.getLeftOperand());
        }
    }
    return true;
}
Also used : InfixExpression(org.eclipse.jdt.core.dom.InfixExpression)

Example 99 with InfixExpression

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

the class ObsoleteOperandFactorizationCleanUp method replaceDuplicateExpression.

private void replaceDuplicateExpression(final InfixExpression visited, final InfixExpression firstCondition, final Expression firstExpression, final Expression secondExpression, final Expression secondOppositeExpression) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteOperandFactorizationCleanUp_description);
    InfixExpression newInnerInfixExpression = ast.newInfixExpression();
    newInnerInfixExpression.setOperator(visited.getOperator());
    newInnerInfixExpression.setLeftOperand(ASTNodes.createMoveTarget(rewrite, secondExpression));
    newInnerInfixExpression.setRightOperand(ASTNodes.createMoveTarget(rewrite, secondOppositeExpression));
    InfixExpression newMainInfixExpression = ast.newInfixExpression();
    newMainInfixExpression.setOperator(firstCondition.getOperator());
    newMainInfixExpression.setLeftOperand(ASTNodes.createMoveTarget(rewrite, firstExpression));
    newMainInfixExpression.setRightOperand(ASTNodeFactory.parenthesizeIfNeeded(ast, newInnerInfixExpression));
    ASTNodes.replaceButKeepComment(rewrite, visited, ASTNodeFactory.parenthesizeIfNeeded(ast, newMainInfixExpression), group);
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 100 with InfixExpression

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

the class ObsoleteRedundantTruthCleanUp method maybeRefactorInfixExpression.

private boolean maybeRefactorInfixExpression(final ASTNode visited, final InfixExpression originalCondition) {
    if (!originalCondition.hasExtendedOperands() && ASTNodes.hasOperator(originalCondition, InfixExpression.Operator.EQUALS, InfixExpression.Operator.NOT_EQUALS, InfixExpression.Operator.XOR) && // fearing we would introduce a previously non existing NPE.
    (ASTNodes.isPrimitive(originalCondition.getLeftOperand(), boolean.class.getSimpleName()) || ASTNodes.isPrimitive(originalCondition.getRightOperand(), boolean.class.getSimpleName()))) {
        Expression leftExpression = originalCondition.getLeftOperand();
        Expression rightExpression = originalCondition.getRightOperand();
        boolean isEquals = ASTNodes.hasOperator(originalCondition, InfixExpression.Operator.EQUALS);
        return maybeRemoveConstantOperand(visited, leftExpression, rightExpression, isEquals) && maybeRemoveConstantOperand(visited, rightExpression, leftExpression, isEquals);
    }
    return true;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression)

Aggregations

InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)196 Expression (org.eclipse.jdt.core.dom.Expression)137 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)85 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)67 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)55 CastExpression (org.eclipse.jdt.core.dom.CastExpression)47 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)37 PostfixExpression (org.eclipse.jdt.core.dom.PostfixExpression)34 InstanceofExpression (org.eclipse.jdt.core.dom.InstanceofExpression)31 AST (org.eclipse.jdt.core.dom.AST)29 ASTNode (org.eclipse.jdt.core.dom.ASTNode)29 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)25 ASTRewrite (org.autorefactor.jdt.core.dom.ASTRewrite)24 ASTNodeFactory (org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory)24 TextEditGroup (org.eclipse.text.edits.TextEditGroup)22 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)20 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)20 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)18 Assignment (org.eclipse.jdt.core.dom.Assignment)17 IfStatement (org.eclipse.jdt.core.dom.IfStatement)17