Search in sources :

Example 61 with PrefixExpression

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

the class ASTSemanticMatcher method matchNegative.

/**
 * Match the boolean opposite.
 *
 * @param node        Node to check
 * @param otherObject Node to compare
 * @return True if it is the boolean opposite.
 */
public boolean matchNegative(final ASTNode node, final Object otherObject) {
    Object other = unbracket(otherObject);
    if (node instanceof ParenthesizedExpression) {
        return matchNegative(((ParenthesizedExpression) node).getExpression(), other);
    }
    if (node instanceof PrefixExpression) {
        PrefixExpression pe = (PrefixExpression) node;
        if (ASTNodes.hasOperator(pe, PrefixExpression.Operator.NOT)) {
            if (other instanceof PrefixExpression && ASTNodes.hasOperator((PrefixExpression) other, PrefixExpression.Operator.NOT)) {
                return matchNegative(pe.getOperand(), ((PrefixExpression) other).getOperand());
            }
            return safeSubtreeMatch(pe.getOperand(), other);
        }
    } else if (other instanceof PrefixExpression && ASTNodes.hasOperator((PrefixExpression) other, PrefixExpression.Operator.NOT)) {
        return safeSubtreeMatch(node, ((PrefixExpression) other).getOperand());
    }
    if (other instanceof ASTNode) {
        Boolean value = ASTNodes.getBooleanLiteral(node);
        Boolean otherValue = ASTNodes.getBooleanLiteral((ASTNode) other);
        if (value != null && otherValue != null) {
            return value ^ otherValue;
        }
    }
    if (!(node instanceof InfixExpression) || !(other instanceof InfixExpression)) {
        return false;
    }
    InfixExpression infixExpression1 = (InfixExpression) node;
    InfixExpression infixExpression2 = (InfixExpression) other;
    Expression leftOperand1 = infixExpression1.getLeftOperand();
    Expression rightOperand1 = infixExpression1.getRightOperand();
    Expression leftOperand2 = infixExpression2.getLeftOperand();
    Expression rightOperand2 = infixExpression2.getRightOperand();
    if (infixExpression1.getOperator().equals(infixExpression2.getOperator()) && !infixExpression1.hasExtendedOperands() && !infixExpression2.hasExtendedOperands() && ASTNodes.hasOperator(infixExpression1, InfixExpression.Operator.EQUALS, InfixExpression.Operator.NOT_EQUALS, InfixExpression.Operator.XOR)) {
        return matchOneNegativeOther(leftOperand1, leftOperand2, rightOperand2, rightOperand1) || matchOneNegativeOther(rightOperand2, rightOperand1, leftOperand1, leftOperand2) || ASTNodes.isPassiveWithoutFallingThrough(leftOperand1) && ASTNodes.isPassiveWithoutFallingThrough(rightOperand1) && ASTNodes.isPassiveWithoutFallingThrough(leftOperand2) && ASTNodes.isPassiveWithoutFallingThrough(rightOperand2) && (matchOneNegativeOther(leftOperand1, leftOperand2, rightOperand2, rightOperand1) || matchOneNegativeOther(rightOperand2, rightOperand1, leftOperand1, leftOperand2));
    }
    InfixExpression.Operator negatedOperator = ASTNodes.negatedInfixOperator(infixExpression1.getOperator());
    if (infixExpression2.getOperator().equals(negatedOperator)) {
        if (ASTNodes.hasOperator(infixExpression1, InfixExpression.Operator.CONDITIONAL_AND, InfixExpression.Operator.CONDITIONAL_OR, InfixExpression.Operator.AND, InfixExpression.Operator.OR)) {
            return isOperandsMatching(infixExpression1, infixExpression2, false);
        }
        if (ASTNodes.hasOperator(infixExpression1, InfixExpression.Operator.EQUALS, InfixExpression.Operator.NOT_EQUALS)) {
            return isOperandsMatching(infixExpression1, infixExpression2, true);
        }
        if (ASTNodes.hasOperator(infixExpression1, InfixExpression.Operator.GREATER, InfixExpression.Operator.GREATER_EQUALS, InfixExpression.Operator.LESS, InfixExpression.Operator.LESS_EQUALS) && ASTNodes.isPassiveWithoutFallingThrough(leftOperand1) && ASTNodes.isPassiveWithoutFallingThrough(rightOperand1) && ASTNodes.isPassiveWithoutFallingThrough(leftOperand2) && ASTNodes.isPassiveWithoutFallingThrough(rightOperand2)) {
            return safeSubtreeMatch(leftOperand1, leftOperand2) && safeSubtreeMatch(rightOperand1, rightOperand2);
        }
        return false;
    }
    return (ASTNodes.hasOperator(infixExpression1, InfixExpression.Operator.GREATER) && ASTNodes.hasOperator(infixExpression2, InfixExpression.Operator.GREATER_EQUALS) || ASTNodes.hasOperator(infixExpression1, InfixExpression.Operator.GREATER_EQUALS) && ASTNodes.hasOperator(infixExpression2, InfixExpression.Operator.GREATER) || ASTNodes.hasOperator(infixExpression1, InfixExpression.Operator.LESS) && ASTNodes.hasOperator(infixExpression2, InfixExpression.Operator.LESS_EQUALS) || ASTNodes.hasOperator(infixExpression1, InfixExpression.Operator.LESS_EQUALS) && ASTNodes.hasOperator(infixExpression2, InfixExpression.Operator.LESS)) && !infixExpression1.hasExtendedOperands() && !infixExpression2.hasExtendedOperands() && ASTNodes.isPassiveWithoutFallingThrough(leftOperand1) && ASTNodes.isPassiveWithoutFallingThrough(rightOperand1) && ASTNodes.isPassiveWithoutFallingThrough(leftOperand2) && ASTNodes.isPassiveWithoutFallingThrough(rightOperand2) && safeSubtreeMatch(leftOperand1, rightOperand2) && safeSubtreeMatch(rightOperand1, leftOperand2);
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) 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) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression)

Example 62 with PrefixExpression

use of org.eclipse.jdt.core.dom.PrefixExpression in project evosuite by EvoSuite.

the class TestExtractingVisitor method visit.

/**
 * {@inheritDoc}
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public boolean visit(VariableDeclarationStatement variableDeclStmt) {
    Class<?> varType = retrieveTypeClass(variableDeclStmt.getType());
    if (varType.isPrimitive() || PRIMITIVE_CLASSES.contains(varType)) {
        // Can only happen to primitive types and String,
        // otherwise it is a constructor call which is handled elsewhere
        logger.debug("Variable has not been treated elsewhere...");
        VariableDeclarationFragment varDeclFrgmnt = (VariableDeclarationFragment) variableDeclStmt.fragments().get(0);
        Expression expression = varDeclFrgmnt.getInitializer();
        VariableReference varRef = retrieveVariableReference(expression, null);
        varRef.setOriginalCode(variableDeclStmt.toString());
        // TODO Use the name here as well?
        // String name = varDeclFrgmt.getName().getIdentifier();
        // new BoundVariableReferenceImpl(testCase, varType, name);
        testCase.addVariable(varDeclFrgmnt.resolveBinding(), varRef);
        return true;
    }
    if (varType.isArray()) {
        // if (varType.getComponentType().isPrimitive() ||
        // varType.getComponentType().equals(String.class)) {
        // ... or to primitive and string arrays
        VariableDeclarationFragment varDeclFrgmnt = (VariableDeclarationFragment) variableDeclStmt.fragments().get(0);
        Expression expression = varDeclFrgmnt.getInitializer();
        if (expression instanceof ArrayInitializer) {
            ArrayReference arrayReference = new ValidArrayReference(testCase.getReference(), varType);
            ArrayStatement arrayStatement = new ArrayStatement(testCase.getReference(), arrayReference);
            arrayReference.setOriginalCode(variableDeclStmt.toString());
            testCase.addStatement(arrayStatement);
            testCase.addVariable(varDeclFrgmnt.resolveBinding(), arrayReference);
            ArrayInitializer arrayInitializer = (ArrayInitializer) expression;
            for (int idx = 0; idx < arrayInitializer.expressions().size(); idx++) {
                Expression expr = (Expression) arrayInitializer.expressions().get(idx);
                VariableReference valueRef;
                if (expr instanceof NumberLiteral) {
                    valueRef = retrieveVariableReference((NumberLiteral) expr, varType.getComponentType());
                } else if (expr instanceof PrefixExpression) {
                    valueRef = retrieveVariableReference((PrefixExpression) expr, varType.getComponentType());
                } else {
                    valueRef = retrieveVariableReference(expr, null);
                }
                valueRef.setOriginalCode(expr.toString());
                VariableReference arrayElementRef = new ArrayIndex(testCase.getReference(), arrayReference, idx);
                arrayElementRef.setOriginalCode(expr.toString());
                arrayStatement.getVariableReferences().add(arrayElementRef);
                AssignmentStatement arrayAssignment = new AssignmentStatement(testCase.getReference(), arrayElementRef, valueRef);
                testCase.addStatement(arrayAssignment);
            }
            // }
            return true;
        }
        if (expression instanceof ArrayCreation) {
            ArrayCreation arrayCreation = ((ArrayCreation) expression);
            List paramTypes = new ArrayList();
            for (int idx = 0; idx < arrayCreation.dimensions().size(); idx++) {
                paramTypes.add(int.class);
            }
            List<VariableReference> lengthsVarRefs = convertParams(arrayCreation.dimensions(), paramTypes);
            ArrayReference arrayReference = new ValidArrayReference(testCase.getReference(), varType);
            arrayReference.setOriginalCode(variableDeclStmt.toString());
            ArrayStatement arrayStatement = new ArrayStatement(testCase.getReference(), arrayReference);
            arrayStatement.setLengths(getLengths(variableDeclStmt, lengthsVarRefs));
            testCase.addVariable(varDeclFrgmnt.resolveBinding(), arrayStatement.getReturnValue());
            testCase.addStatement(arrayStatement);
        }
    }
    return true;
}
Also used : ArrayReference(org.evosuite.testcase.ArrayReference) VariableReference(org.evosuite.testcase.VariableReference) ArrayList(java.util.ArrayList) ArrayIndex(org.evosuite.testcase.ArrayIndex) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) PrimitiveExpression(org.evosuite.testcase.PrimitiveExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) AssignmentStatement(org.evosuite.testcase.AssignmentStatement) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ArrayCreation(org.eclipse.jdt.core.dom.ArrayCreation) ArrayStatement(org.evosuite.testcase.ArrayStatement) ArrayList(java.util.ArrayList) AbstractList(java.util.AbstractList) List(java.util.List) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral) ArrayInitializer(org.eclipse.jdt.core.dom.ArrayInitializer)

Example 63 with PrefixExpression

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

the class InvertBooleanUtility method getInversedNotExpression.

private static Expression getInversedNotExpression(ASTRewrite rewrite, Expression expression, AST ast) {
    PrefixExpression prefixExpression = ast.newPrefixExpression();
    prefixExpression.setOperator(PrefixExpression.Operator.NOT);
    ParenthesizedExpression parenthesizedExpression = getParenthesizedExpression(ast, (Expression) rewrite.createCopyTarget(expression));
    prefixExpression.setOperand(parenthesizedExpression);
    return prefixExpression;
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression)

Example 64 with PrefixExpression

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

the class AccessAnalyzer method visit.

@Override
public boolean visit(PrefixExpression node) {
    Expression operand = node.getOperand();
    if (!considerBinding(resolveBinding(operand), operand)) {
        return true;
    }
    PrefixExpression.Operator operator = node.getOperator();
    if (operator != PrefixExpression.Operator.INCREMENT && operator != PrefixExpression.Operator.DECREMENT) {
        return true;
    }
    checkParent(node);
    fRewriter.replace(node, createInvocation(node.getAST(), node.getOperand(), node.getOperator().toString()), createGroupDescription(PREFIX_ACCESS));
    return false;
}
Also used : PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) 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) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression)

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