Search in sources :

Example 26 with PrefixExpression

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

the class ObsoleteObjectsEqualsRatherThanEqualsAndNullCheckCleanUp method maybeReplaceEquals.

private boolean maybeReplaceEquals(final IfStatement node, final Expression firstField, final InfixExpression nullityCondition, final ReturnStatement returnStatement1, final PrefixExpression equalsCondition, final ReturnStatement returnStatement2, final Set<String> classesToUseWithImport, final Set<String> importsToAdd) {
    OrderedInfixExpression<Expression, NullLiteral> nullityOrderedCondition = ASTNodes.orderedInfix(nullityCondition, Expression.class, NullLiteral.class);
    MethodInvocation equalsMethod = ASTNodes.as(equalsCondition.getOperand(), MethodInvocation.class);
    if (nullityOrderedCondition != null && returnStatement1 != null && returnStatement2 != null && equalsMethod != null && equalsMethod.getExpression() != null && EQUALS_METHOD.equals(equalsMethod.getName().getIdentifier()) && equalsMethod.arguments() != null && equalsMethod.arguments().size() == 1) {
        Expression secondField = nullityOrderedCondition.getFirstOperand();
        if (secondField != null && (match(firstField, secondField, equalsMethod.getExpression(), (ASTNode) equalsMethod.arguments().get(0)) || match(secondField, firstField, equalsMethod.getExpression(), (ASTNode) equalsMethod.arguments().get(0)))) {
            BooleanLiteral returnFalse1 = ASTNodes.as(returnStatement1.getExpression(), BooleanLiteral.class);
            BooleanLiteral returnFalse2 = ASTNodes.as(returnStatement2.getExpression(), BooleanLiteral.class);
            if (returnFalse1 != null && !returnFalse1.booleanValue() && returnFalse2 != null && !returnFalse2.booleanValue()) {
                replaceEquals(node, firstField, classesToUseWithImport, importsToAdd, secondField, returnStatement1);
                return false;
            }
        }
    }
    return true;
}
Also used : InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) OrderedInfixExpression(org.autorefactor.jdt.internal.corext.dom.OrderedInfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) BooleanLiteral(org.eclipse.jdt.core.dom.BooleanLiteral) ASTNode(org.eclipse.jdt.core.dom.ASTNode) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) NullLiteral(org.eclipse.jdt.core.dom.NullLiteral)

Example 27 with PrefixExpression

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

the class ObsoleteXORRatherThanDuplicateConditionsCleanUp method getBasisExpression.

private Expression getBasisExpression(final Expression originalExpression, final AtomicBoolean isExprPositive) {
    Expression basisExpression = null;
    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) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression)

Example 28 with PrefixExpression

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

the class ObsoleteOppositeConditionRatherThanDuplicateConditionCleanUp method refactorCondition.

private void refactorCondition(final IfStatement node, final Expression duplicateExpression, final Expression notDuplicateExpression, final Statement positiveStatement, final Statement negativeStatement) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteOppositeConditionRatherThanDuplicateConditionCleanUp_description);
    Statement negativeStmtCopy;
    if (negativeStatement instanceof IfStatement) {
        Block newBlock = ast.newBlock();
        newBlock.statements().add(ASTNodes.createMoveTarget(rewrite, negativeStatement));
        negativeStmtCopy = newBlock;
    } else {
        negativeStmtCopy = ASTNodes.createMoveTarget(rewrite, negativeStatement);
    }
    Expression secondCond;
    Statement secondStmtCopy;
    Statement thirdStmtCopy;
    PrefixExpression negativeCond = ASTNodes.as(notDuplicateExpression, PrefixExpression.class);
    if (negativeCond != null && ASTNodes.hasOperator(negativeCond, PrefixExpression.Operator.NOT)) {
        secondCond = negativeCond.getOperand();
        secondStmtCopy = ASTNodes.createMoveTarget(rewrite, positiveStatement);
        thirdStmtCopy = ASTNodes.createMoveTarget(rewrite, node.getThenStatement());
    } else {
        secondCond = notDuplicateExpression;
        secondStmtCopy = ASTNodes.createMoveTarget(rewrite, node.getThenStatement());
        thirdStmtCopy = ASTNodes.createMoveTarget(rewrite, positiveStatement);
    }
    ASTNodes.replaceButKeepComment(rewrite, node.getExpression(), ast.negate(duplicateExpression, true), group);
    ASTNodes.replaceButKeepComment(rewrite, node.getThenStatement(), negativeStmtCopy, group);
    IfStatement newIfStatement = ast.newIfStatement();
    newIfStatement.setExpression(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(secondCond)));
    newIfStatement.setThenStatement(secondStmtCopy);
    newIfStatement.setElseStatement(thirdStmtCopy);
    ASTNodes.replaceButKeepComment(rewrite, node.getElseStatement(), newIfStatement, group);
}
Also used : IfStatement(org.eclipse.jdt.core.dom.IfStatement) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 29 with PrefixExpression

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

the class OppositeComparisonRatherThanNegativeExpressionCleanUp method visit.

@Override
public boolean visit(final PrefixExpression visited) {
    if (ASTNodes.hasOperator(visited, PrefixExpression.Operator.MINUS)) {
        MethodInvocation methodInvocation = ASTNodes.as(visited.getOperand(), MethodInvocation.class);
        if (methodInvocation != null && methodInvocation.getExpression() != null && methodInvocation.arguments().size() == 1) {
            Expression argument = (Expression) methodInvocation.arguments().get(0);
            String[] classes = { Double.class.getCanonicalName(), Float.class.getCanonicalName(), Short.class.getCanonicalName(), Integer.class.getCanonicalName(), Long.class.getCanonicalName(), Character.class.getCanonicalName(), Byte.class.getCanonicalName(), Boolean.class.getCanonicalName() };
            for (String klass : classes) {
                if (ASTNodes.usesGivenSignature(methodInvocation, klass, "compareTo", klass)) {
                    // $NON-NLS-1$
                    if (ASTNodes.hasType(argument, klass)) {
                        reverseObjects(visited, methodInvocation);
                        return false;
                    }
                    return true;
                }
            }
        }
    }
    return true;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Example 30 with PrefixExpression

use of org.eclipse.jdt.core.dom.PrefixExpression in project eclipse-pmd by acanda.

the class UseCollectionIsEmptyQuickFix method apply.

/**
 * Replaces {@code x.size() == 0} or {@code 0 == x.size()} with {@code x.isEmpty()}. Replaces {@code x.size() != 0}
 * or {@code 0 != x.size()} with {@code !x.isEmpty()}.
 */
@Override
protected boolean apply(final InfixExpression node) {
    final MethodInvocation size;
    if (node.getLeftOperand() instanceof MethodInvocation) {
        size = (MethodInvocation) node.getLeftOperand();
    } else if (node.getRightOperand() instanceof MethodInvocation) {
        size = (MethodInvocation) node.getRightOperand();
    } else {
        return false;
    }
    final AST ast = node.getAST();
    final MethodInvocation invocation = (MethodInvocation) ast.createInstance(MethodInvocation.class);
    invocation.setExpression(ASTUtil.copy(size.getExpression()));
    final SimpleName isEmpty = (SimpleName) ast.createInstance(SimpleName.class);
    isEmpty.setIdentifier("isEmpty");
    invocation.setName(isEmpty);
    final Expression replacement;
    if (isNotEmpty(node)) {
        final PrefixExpression not = (PrefixExpression) ast.createInstance(PrefixExpression.class);
        not.setOperator(org.eclipse.jdt.core.dom.PrefixExpression.Operator.NOT);
        not.setOperand(invocation);
        replacement = not;
    } else {
        replacement = invocation;
    }
    ASTUtil.replace(node, replacement);
    return true;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) SimpleName(org.eclipse.jdt.core.dom.SimpleName) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

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