Search in sources :

Example 76 with InfixExpression

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

the class ASTNodes method peremptoryValue.

/**
 * Returns a peremptory value, if any.
 *
 * @param peremptoryExpression A possible peremptory expression
 * @return A peremptory value, if any
 */
public static Object peremptoryValue(final Expression peremptoryExpression) {
    Object constantExpression = peremptoryExpression.resolveConstantExpressionValue();
    if (constantExpression != null) {
        return constantExpression;
    }
    InfixExpression infixExpression = as(peremptoryExpression, InfixExpression.class);
    if (infixExpression != null && !infixExpression.hasExtendedOperands() && hasOperator(infixExpression, InfixExpression.Operator.EQUALS, InfixExpression.Operator.NOT_EQUALS)) {
        if (match(infixExpression.getLeftOperand(), infixExpression.getRightOperand())) {
            return hasOperator(infixExpression, InfixExpression.Operator.EQUALS);
        }
        if (ASTSemanticMatcher.INSTANCE.matchNegative(infixExpression.getLeftOperand(), infixExpression.getRightOperand())) {
            return hasOperator(infixExpression, InfixExpression.Operator.NOT_EQUALS);
        }
    }
    return null;
}
Also used : InfixExpression(org.eclipse.jdt.core.dom.InfixExpression)

Example 77 with InfixExpression

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

the class ASTNodeFactory method newInfixExpression.

/**
 * Builds a new {@link InfixExpression} instance.
 *
 * @param leftOperand      the left operand
 * @param operator         the infix operator
 * @param rightOperand     the right operand
 * @param extendedOperands the extended operands
 * @return a new infix expression
 */
public InfixExpression newInfixExpression(Expression leftOperand, InfixExpression.Operator operator, Expression rightOperand, Expression... extendedOperands) {
    InfixExpression newInfixExpression = newInfixExpression();
    newInfixExpression.setLeftOperand(leftOperand);
    newInfixExpression.setOperator(operator);
    newInfixExpression.setRightOperand(rightOperand);
    Collections.addAll(newInfixExpression.extendedOperands(), extendedOperands);
    return newInfixExpression;
}
Also used : InfixExpression(org.eclipse.jdt.core.dom.InfixExpression)

Example 78 with InfixExpression

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

the class ASTSemanticMatcher method isOperandsMatching.

private boolean isOperandsMatching(final InfixExpression ie1, final InfixExpression ie2, final boolean equal) {
    List<Expression> operands1 = getConsistentOperands(ie1);
    List<Expression> operands2 = getConsistentOperands(ie2);
    if (operands1.size() != operands2.size()) {
        return false;
    }
    boolean isMatching = true;
    Iterator<Expression> iterator1 = operands1.iterator();
    Iterator<Expression> iterator2 = operands2.iterator();
    while (iterator1.hasNext() && iterator2.hasNext()) {
        Expression expression = iterator1.next();
        Expression otherExpression = iterator2.next();
        if (equal ? !safeSubtreeMatch(expression, otherExpression) : !matchNegative(expression, otherExpression)) {
            isMatching = false;
            break;
        }
    }
    if (isMatching) {
        return true;
    }
    for (Expression expression : operands1) {
        if (!ASTNodes.isPassiveWithoutFallingThrough(expression)) {
            return false;
        }
    }
    for (Expression expression : operands2) {
        if (!ASTNodes.isPassiveWithoutFallingThrough(expression)) {
            return false;
        }
    }
    for (Iterator<Expression> iterator3 = operands1.iterator(); iterator3.hasNext(); ) {
        Expression expression = iterator3.next();
        for (Iterator<Expression> iterator4 = operands2.iterator(); iterator4.hasNext(); ) {
            Expression otherExpression = iterator4.next();
            if (equal ? safeSubtreeMatch(expression, otherExpression) : matchNegative(expression, otherExpression)) {
                iterator3.remove();
                iterator4.remove();
                break;
            }
        }
    }
    return operands1.isEmpty() && operands2.isEmpty();
}
Also used : 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) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression)

Example 79 with InfixExpression

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

the class CFGBuilder method addVariableAccess.

/**
 * @return whether the current variable access can throw an exception.
 */
private boolean addVariableAccess(final CFGBasicBlock basicBlock, final Expression node, final int flags, final ThrowerBlocks throwers) {
    if (node == null) {
        return false;
    }
    switch(node.getNodeType()) {
        case ASTNode.ARRAY_ACCESS:
            ArrayAccess aa = (ArrayAccess) node;
            addVariableAccess(basicBlock, aa.getArray(), flags, throwers);
            addVariableAccess(basicBlock, aa.getIndex(), flags, throwers);
            throwers.addThrow(aa, newException(node, ArrayIndexOutOfBoundsException.class.getCanonicalName()));
            return true;
        case ASTNode.ARRAY_CREATION:
            ArrayCreation ac = (ArrayCreation) node;
            boolean acMightThrow1 = addVariableAccess(basicBlock, ac.getInitializer(), flags, throwers);
            boolean acMightThrow2 = addVariableAccesses(basicBlock, ac.dimensions(), flags, throwers);
            return acMightThrow1 || acMightThrow2;
        case ASTNode.ARRAY_INITIALIZER:
            ArrayInitializer ai = (ArrayInitializer) node;
            return addVariableAccesses(basicBlock, ai.expressions(), flags, throwers);
        case ASTNode.ASSIGNMENT:
            Assignment a = (Assignment) node;
            boolean aMightThrow1 = addVariableAccess(basicBlock, a.getLeftHandSide(), VariableAccess.WRITE, throwers);
            boolean aMightThrow2 = addVariableAccess(basicBlock, a.getRightHandSide(), VariableAccess.READ, throwers);
            return aMightThrow1 || aMightThrow2;
        case ASTNode.BOOLEAN_LITERAL:
        case ASTNode.CHARACTER_LITERAL:
        case ASTNode.NULL_LITERAL:
        case ASTNode.NUMBER_LITERAL:
        case ASTNode.STRING_LITERAL:
        case ASTNode.TYPE_LITERAL:
            // Nothing to do
            return false;
        case ASTNode.CAST_EXPRESSION:
            CastExpression cae = (CastExpression) node;
            return addVariableAccess(basicBlock, cae.getExpression(), flags, throwers);
        case ASTNode.CLASS_INSTANCE_CREATION:
            ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation) node;
            addVariableAccess(basicBlock, classInstanceCreation.getExpression(), flags, throwers);
            addVariableAccesses(basicBlock, classInstanceCreation.arguments(), flags, throwers);
            IMethodBinding cicBinding = classInstanceCreation.resolveConstructorBinding();
            if (cicBinding != null) {
                ITypeBinding[] declaredThrows = cicBinding.getExceptionTypes();
                throwers.addThrow(classInstanceCreation, declaredThrows);
                return declaredThrows.length > 0;
            }
            return false;
        case ASTNode.CONDITIONAL_EXPRESSION:
            ConditionalExpression coe = (ConditionalExpression) node;
            boolean mightThrow1 = addVariableAccess(basicBlock, coe.getExpression(), flags, throwers);
            boolean mightThrow2 = addVariableAccess(basicBlock, coe.getThenExpression(), flags, throwers);
            boolean mightThrow3 = addVariableAccess(basicBlock, coe.getElseExpression(), flags, throwers);
            return mightThrow1 || mightThrow2 || mightThrow3;
        case ASTNode.FIELD_ACCESS:
            FieldAccess fa = (FieldAccess) node;
            boolean mightThrow = addVariableAccess(basicBlock, fa.getExpression(), flags, throwers);
            basicBlock.addVariableAccess(new VariableAccess(fa, flags));
            if (is(flags, VariableAccess.READ)) {
                throwers.addThrow(fa, newException(node, NullPointerException.class.getCanonicalName()));
                mightThrow = true;
            }
            return mightThrow;
        case ASTNode.INFIX_EXPRESSION:
            InfixExpression infixExpression = (InfixExpression) node;
            boolean ieMightThrow1 = addVariableAccess(basicBlock, infixExpression.getLeftOperand(), flags, throwers);
            boolean ieMightThrow2 = addVariableAccess(basicBlock, infixExpression.getRightOperand(), flags, throwers);
            return ieMightThrow1 || ieMightThrow2;
        case ASTNode.INSTANCEOF_EXPRESSION:
            InstanceofExpression ioe = (InstanceofExpression) node;
            return addVariableAccess(basicBlock, ioe.getLeftOperand(), flags, throwers);
        case ASTNode.METHOD_INVOCATION:
            MethodInvocation methodInvocation = (MethodInvocation) node;
            addVariableAccess(basicBlock, methodInvocation.getExpression(), flags, throwers);
            addVariableAccesses(basicBlock, methodInvocation.arguments(), flags, throwers);
            IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
            if (methodBinding != null) {
                ITypeBinding[] declaredThrows = methodBinding.getExceptionTypes();
                throwers.addThrow(methodInvocation, declaredThrows);
                return declaredThrows.length > 0;
            }
            return false;
        case ASTNode.SIMPLE_NAME:
            SimpleName sn = (SimpleName) node;
            basicBlock.addVariableAccess(new VariableAccess(sn, flags));
            if (is(flags, VariableAccess.READ)) {
                throwers.addThrow(sn, newException(node, NullPointerException.class.getCanonicalName()));
                return true;
            }
            return false;
        case ASTNode.QUALIFIED_NAME:
            QualifiedName qn = (QualifiedName) node;
            basicBlock.addVariableAccess(new VariableAccess(qn, flags));
            throwers.addThrow(qn, newException(node, NullPointerException.class.getCanonicalName()));
            return true;
        case ASTNode.PARENTHESIZED_EXPRESSION:
            ParenthesizedExpression pe = (ParenthesizedExpression) node;
            return addVariableAccess(basicBlock, pe.getExpression(), flags, throwers);
        case ASTNode.POSTFIX_EXPRESSION:
            PostfixExpression poe = (PostfixExpression) node;
            return addVariableAccess(basicBlock, poe.getOperand(), flags, throwers);
        case ASTNode.PREFIX_EXPRESSION:
            PrefixExpression pre = (PrefixExpression) node;
            return addVariableAccess(basicBlock, pre.getOperand(), flags, throwers);
        case ASTNode.SUPER_FIELD_ACCESS:
            SuperFieldAccess sfa = (SuperFieldAccess) node;
            boolean sfaMightThrow1 = addVariableAccess(basicBlock, sfa.getQualifier(), flags, throwers);
            boolean sfaMightThrow2 = addVariableAccess(basicBlock, sfa.getName(), flags, throwers);
            return sfaMightThrow1 || sfaMightThrow2;
        case ASTNode.SUPER_METHOD_INVOCATION:
            SuperMethodInvocation smi = (SuperMethodInvocation) node;
            addVariableAccess(basicBlock, smi.getQualifier(), flags, throwers);
            addVariableAccess(basicBlock, smi.getName(), flags, throwers);
            IMethodBinding sMethodBinding = smi.resolveMethodBinding();
            if (sMethodBinding != null) {
                ITypeBinding[] declaredThrows = sMethodBinding.getExceptionTypes();
                throwers.addThrow(smi, declaredThrows);
                return declaredThrows.length > 0;
            }
            return false;
        case ASTNode.THIS_EXPRESSION:
            ThisExpression te = (ThisExpression) node;
            // TODO JNR remember use of "this" here
            return addVariableAccess(basicBlock, te.getQualifier(), flags, throwers);
        case ASTNode.VARIABLE_DECLARATION_EXPRESSION:
            return addDeclarations(basicBlock, (VariableDeclarationExpression) node, throwers);
        default:
            throw new NotImplementedException(node);
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) SimpleName(org.eclipse.jdt.core.dom.SimpleName) NotImplementedException(org.autorefactor.util.NotImplementedException) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) Assignment(org.eclipse.jdt.core.dom.Assignment) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) SuperFieldAccess(org.eclipse.jdt.core.dom.SuperFieldAccess) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ArrayCreation(org.eclipse.jdt.core.dom.ArrayCreation) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) SuperFieldAccess(org.eclipse.jdt.core.dom.SuperFieldAccess) ArrayInitializer(org.eclipse.jdt.core.dom.ArrayInitializer)

Example 80 with InfixExpression

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

the class ASTNodeFactory method negate.

/**
 * Negates the provided expression and applies the provided copy operation on
 * the returned expression:
 *
 * isValid                        =>  !isValid
 * !isValid                       =>  isValid
 * true                           =>  false
 * false                          =>  true
 * i > 0                          =>  i <= 0
 * isValid || isEnabled           =>  !isValid && !isEnabled
 * !isValid || !isEnabled         =>  isValid && isEnabled
 * isValid ? (i > 0) : !isEnabled =>  isValid ? (i <= 0) : isEnabled
 * @param booleanExpression the expression to negate
 * @param isMove False if the returned nodes need to be new nodes
 *
 * @return the negated expression, as move or copy
 */
public Expression negate(final Expression booleanExpression, final boolean isMove) {
    Expression unparenthesedExpression = ASTNodes.getUnparenthesedExpression(booleanExpression);
    if (unparenthesedExpression instanceof PrefixExpression) {
        PrefixExpression prefixExpression = (PrefixExpression) unparenthesedExpression;
        if (ASTNodes.hasOperator(prefixExpression, PrefixExpression.Operator.NOT)) {
            Expression otherExpression = prefixExpression.getOperand();
            PrefixExpression otherPrefixExpression = ASTNodes.as(otherExpression, PrefixExpression.class);
            if (otherPrefixExpression != null && ASTNodes.hasOperator(otherPrefixExpression, PrefixExpression.Operator.NOT)) {
                return negate(otherPrefixExpression.getOperand(), isMove);
            }
            return isMove ? createMoveTarget(otherExpression) : createCopyTarget(otherExpression);
        }
    } else if (unparenthesedExpression instanceof InfixExpression) {
        InfixExpression booleanOperation = (InfixExpression) unparenthesedExpression;
        InfixExpression.Operator negatedOperator = ASTNodes.negatedInfixOperator(booleanOperation.getOperator());
        if (negatedOperator != null) {
            return getNegatedOperation(booleanOperation, negatedOperator, isMove);
        }
    } else if (unparenthesedExpression instanceof ConditionalExpression) {
        ConditionalExpression aConditionalExpression = (ConditionalExpression) unparenthesedExpression;
        return newConditionalExpression(isMove ? createMoveTarget(aConditionalExpression.getExpression()) : createCopyTarget(aConditionalExpression.getExpression()), negate(aConditionalExpression.getThenExpression(), isMove), negate(aConditionalExpression.getElseExpression(), isMove));
    } else {
        Boolean constant = ASTNodes.getBooleanLiteral(unparenthesedExpression);
        if (constant != null) {
            return newBooleanLiteral(!constant);
        }
    }
    if (isMove) {
        return not(createMoveTarget(unparenthesedExpression));
    }
    return not(createCopyTarget(unparenthesedExpression));
}
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) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression)

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