Search in sources :

Example 21 with PrefixExpression

use of org.eclipse.jdt.core.dom.PrefixExpression 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 22 with PrefixExpression

use of org.eclipse.jdt.core.dom.PrefixExpression 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)

Example 23 with PrefixExpression

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

the class ObsoleteDoubleNegationCleanUp method visit.

@Override
public boolean visit(final InfixExpression visited) {
    if (!visited.hasExtendedOperands() && ASTNodes.hasOperator(visited, InfixExpression.Operator.EQUALS, InfixExpression.Operator.NOT_EQUALS, InfixExpression.Operator.XOR)) {
        Expression leftExpression = visited.getLeftOperand();
        Expression rightExpression = visited.getRightOperand();
        Expression leftNegatedExpression = null;
        PrefixExpression leftPrefix = ASTNodes.as(leftExpression, PrefixExpression.class);
        if (leftPrefix != null && ASTNodes.hasOperator(leftPrefix, PrefixExpression.Operator.NOT)) {
            leftNegatedExpression = leftPrefix.getOperand();
        }
        Expression rightNegatedExpression = null;
        PrefixExpression rightPrefix = ASTNodes.as(rightExpression, PrefixExpression.class);
        if (rightPrefix != null && ASTNodes.hasOperator(rightPrefix, PrefixExpression.Operator.NOT)) {
            rightNegatedExpression = rightPrefix.getOperand();
        }
        if (leftNegatedExpression != null || rightNegatedExpression != null) {
            removeDoubleNegation(visited, leftExpression, rightExpression, leftNegatedExpression, rightNegatedExpression);
            return false;
        }
    }
    return true;
}
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 24 with PrefixExpression

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

the class ObsoleteLambdaExpressionRatherThanComparatorCleanUp method isReturnedExpressionToRefactor.

private boolean isReturnedExpressionToRefactor(final Expression returnExpression, final AtomicReference<Expression> criteria, final AtomicBoolean isForward, final SimpleName name1, final SimpleName name2) {
    PrefixExpression negativeExpression = ASTNodes.as(returnExpression, PrefixExpression.class);
    if (negativeExpression != null && ASTNodes.hasOperator(negativeExpression, PrefixExpression.Operator.MINUS)) {
        isForward.lazySet(!isForward.get());
        return isReturnedExpressionToRefactor(negativeExpression.getOperand(), criteria, isForward, name1, name2);
    }
    MethodInvocation compareToMethod = ASTNodes.as(returnExpression, MethodInvocation.class);
    if (compareToMethod != null && compareToMethod.getExpression() != null) {
        ITypeBinding comparisonType = compareToMethod.getExpression().resolveTypeBinding();
        if (comparisonType != null) {
            if (compareToMethod.getExpression() != null && ASTNodes.usesGivenSignature(compareToMethod, comparisonType.getQualifiedName(), "compareTo", comparisonType.getQualifiedName())) {
                // $NON-NLS-1$
                return isRefactorComparisonToRefactor(criteria, isForward, name1, name2, compareToMethod.getExpression(), (Expression) compareToMethod.arguments().get(0));
            }
            String primitiveType = Bindings.getUnboxedTypeName(comparisonType.getQualifiedName());
            if (primitiveType != null && ASTNodes.usesGivenSignature(compareToMethod, comparisonType.getQualifiedName(), "compare", primitiveType, primitiveType)) {
                // $NON-NLS-1$
                return isRefactorComparisonToRefactor(criteria, isForward, name1, name2, (Expression) compareToMethod.arguments().get(0), (Expression) compareToMethod.arguments().get(1));
            }
        }
    }
    return false;
}
Also used : PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Example 25 with PrefixExpression

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

the class ObsoleteObjectsEqualsRatherThanEqualsAndNullCheckCleanUp method maybeReplaceCode.

private boolean maybeReplaceCode(final IfStatement node, final InfixExpression condition, final List<Statement> thenStatements, final List<Statement> elseStatements, final Expression firstField, final Set<String> classesToUseWithImport, final Set<String> importsToAdd) {
    IfStatement checkNullityStatement;
    IfStatement checkEqualsStatement;
    if (ASTNodes.hasOperator(condition, InfixExpression.Operator.EQUALS)) {
        checkNullityStatement = ASTNodes.as(thenStatements.get(0), IfStatement.class);
        checkEqualsStatement = ASTNodes.as(elseStatements.get(0), IfStatement.class);
    } else {
        checkEqualsStatement = ASTNodes.as(thenStatements.get(0), IfStatement.class);
        checkNullityStatement = ASTNodes.as(elseStatements.get(0), IfStatement.class);
    }
    if (checkNullityStatement != null && checkNullityStatement.getElseStatement() == null && checkEqualsStatement != null && checkEqualsStatement.getElseStatement() == null) {
        InfixExpression nullityCondition = ASTNodes.as(checkNullityStatement.getExpression(), InfixExpression.class);
        ReturnStatement nullityStatement = ASTNodes.as(checkNullityStatement.getThenStatement(), ReturnStatement.class);
        PrefixExpression equalsCondition = ASTNodes.as(checkEqualsStatement.getExpression(), PrefixExpression.class);
        ReturnStatement equalsStatement = ASTNodes.as(checkEqualsStatement.getThenStatement(), ReturnStatement.class);
        if (nullityCondition != null && !nullityCondition.hasExtendedOperands() && ASTNodes.hasOperator(nullityCondition, InfixExpression.Operator.NOT_EQUALS) && nullityStatement != null && equalsCondition != null && ASTNodes.hasOperator(equalsCondition, PrefixExpression.Operator.NOT) && equalsStatement != null) {
            return maybeReplaceEquals(node, firstField, nullityCondition, nullityStatement, equalsCondition, equalsStatement, classesToUseWithImport, importsToAdd);
        }
    }
    return true;
}
Also used : IfStatement(org.eclipse.jdt.core.dom.IfStatement) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) OrderedInfixExpression(org.autorefactor.jdt.internal.corext.dom.OrderedInfixExpression) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement)

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