Search in sources :

Example 1 with ArrayIndex

use of org.evosuite.testcase.ArrayIndex in project evosuite by EvoSuite.

the class TestExtractingVisitor method retrieveVariableReference.

private VariableReference retrieveVariableReference(ArrayAccess arrayAccess) {
    Expression expr = arrayAccess.getArray();
    List<Integer> indices = new ArrayList<Integer>();
    // TODO This is a shortcut
    // we need a variable reference for the index value
    indices.add(getArrayIndex(arrayAccess.getIndex()));
    while (expr instanceof ArrayAccess) {
        ArrayAccess current = (ArrayAccess) expr;
        expr = current.getArray();
        indices.add(getArrayIndex(current.getIndex()));
    }
    Collections.reverse(indices);
    VariableReference varRef = retrieveVariableReference(expr, null);
    ArrayReference arrayReference = (ArrayReference) varRef;
    assert indices.size() == arrayReference.getArrayDimensions();
    ArrayIndex arrayIndex = new ArrayIndex(testCase.getReference(), arrayReference, indices);
    return arrayIndex;
}
Also used : ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess) ArrayReference(org.evosuite.testcase.ArrayReference) VariableReference(org.evosuite.testcase.VariableReference) 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) ArrayList(java.util.ArrayList) ArrayIndex(org.evosuite.testcase.ArrayIndex)

Example 2 with ArrayIndex

use of org.evosuite.testcase.ArrayIndex in project evosuite by EvoSuite.

the class TestExtractingVisitor method convertParams.

private List<VariableReference> convertParams(List<?> params, List<?> argumentTypes) {
    List<VariableReference> result = new ArrayList<VariableReference>();
    if ((params.size() == 0) && (argumentTypes.size() == 0)) {
        return result;
    }
    if ((argumentTypes.size() > params.size()) && (argumentTypes.size() - 1 != params.size())) {
        throw new RuntimeException("Number of declared and actual params do not match!");
    }
    int limit = argumentTypes.size();
    Class<?> lastDeclaredParamType = retrieveTypeClass(argumentTypes.get(argumentTypes.size() - 1));
    if (lastDeclaredParamType.isArray()) {
        limit = argumentTypes.size() - 1;
    }
    for (int idx = 0; idx < limit; idx++) {
        if (idx >= params.size()) {
            break;
        }
        Object argument = params.get(idx);
        if ((argument instanceof MethodInvocation) || (argument instanceof ClassInstanceCreation)) {
            assert !nestedCallResults.isEmpty();
            result.add(nestedCallResults.pop());
            continue;
        }
        Class<?> argClass = retrieveTypeClass(argumentTypes.get(idx));
        VariableReference argRef = retrieveVariableReference(argument, argClass);
        argRef.setOriginalCode(argument.toString());
        result.add(argRef);
    }
    if (limit == argumentTypes.size()) {
        return result;
    }
    assert lastDeclaredParamType.isArray();
    if (argumentTypes.size() == params.size()) {
        Object lastParam = params.get(params.size() - 1);
        Class<?> lastActualParamType = retrieveTypeClass(lastParam);
        if (lastParam instanceof MethodInvocation) {
            assert !nestedCallResults.isEmpty();
            lastActualParamType = nestedCallResults.peek().getVariableClass();
        }
        if (lastActualParamType.isArray()) {
            if ((lastParam instanceof MethodInvocation) || (lastParam instanceof ClassInstanceCreation)) {
                assert !nestedCallResults.isEmpty();
                result.add(nestedCallResults.pop());
            } else {
                result.add(retrieveVariableReference(lastParam, null));
            }
            return result;
        }
    }
    ArrayReference arrayReference = new ValidArrayReference(testCase.getReference(), lastDeclaredParamType);
    arrayReference.setOriginalCode(params.toString());
    ArrayStatement arrayStatement = new ArrayStatement(testCase.getReference(), arrayReference);
    testCase.addStatement(arrayStatement);
    result.add(arrayStatement.getReturnValue());
    arrayStatement.setSize(params.size() - (argumentTypes.size() - 1));
    int arrayIdx = 0;
    for (int idx = argumentTypes.size() - 1; idx < params.size(); idx++) {
        ArrayIndex arrayIndex = new ArrayIndex(testCase.getReference(), arrayReference, arrayIdx);
        Object param = params.get(idx);
        VariableReference paramRef = null;
        if ((param instanceof MethodInvocation) || (param instanceof ClassInstanceCreation)) {
            assert !nestedCallResults.isEmpty();
            paramRef = nestedCallResults.pop();
        } else {
            paramRef = retrieveVariableReference(param, lastDeclaredParamType.getComponentType());
        }
        paramRef.setOriginalCode(param.toString());
        AssignmentStatement assignment = new AssignmentStatement(testCase.getReference(), arrayIndex, paramRef);
        testCase.addStatement(assignment);
        arrayIdx++;
    }
    return result;
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) ArrayReference(org.evosuite.testcase.ArrayReference) VariableReference(org.evosuite.testcase.VariableReference) ArrayList(java.util.ArrayList) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) ArrayIndex(org.evosuite.testcase.ArrayIndex) AssignmentStatement(org.evosuite.testcase.AssignmentStatement) ArrayStatement(org.evosuite.testcase.ArrayStatement)

Example 3 with ArrayIndex

use of org.evosuite.testcase.ArrayIndex in project evosuite by EvoSuite.

the class TestExtractingVisitor method endVisit.

/**
 * {@inheritDoc}
 */
@Override
public void endVisit(Assignment assignment) {
    if ((assignment.getRightHandSide() instanceof MethodInvocation) || (assignment.getRightHandSide() instanceof ClassInstanceCreation)) {
        // treated in respective endVisit methods
        return;
    }
    VariableReference varRef = retrieveVariableReference(assignment.getLeftHandSide(), null);
    varRef.setOriginalCode(assignment.getLeftHandSide().toString());
    VariableReference newAssignment = retrieveVariableReference(assignment.getRightHandSide(), null);
    newAssignment.setOriginalCode(assignment.getRightHandSide().toString());
    if (varRef instanceof ArrayIndex) {
        AssignmentStatement assignmentStatement = new AssignmentStatement(testCase.getReference(), varRef, newAssignment);
        testCase.addStatement(assignmentStatement);
        return;
    }
    testCase.variableAssignment(varRef, newAssignment);
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) VariableReference(org.evosuite.testcase.VariableReference) AssignmentStatement(org.evosuite.testcase.AssignmentStatement) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) ArrayIndex(org.evosuite.testcase.ArrayIndex)

Example 4 with ArrayIndex

use of org.evosuite.testcase.ArrayIndex 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)

Aggregations

ArrayIndex (org.evosuite.testcase.ArrayIndex)4 VariableReference (org.evosuite.testcase.VariableReference)4 ArrayList (java.util.ArrayList)3 ArrayReference (org.evosuite.testcase.ArrayReference)3 AssignmentStatement (org.evosuite.testcase.AssignmentStatement)3 CastExpression (org.eclipse.jdt.core.dom.CastExpression)2 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)2 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)2 Expression (org.eclipse.jdt.core.dom.Expression)2 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)2 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)2 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)2 SuperMethodInvocation (org.eclipse.jdt.core.dom.SuperMethodInvocation)2 ArrayStatement (org.evosuite.testcase.ArrayStatement)2 PrimitiveExpression (org.evosuite.testcase.PrimitiveExpression)2 AbstractList (java.util.AbstractList)1 List (java.util.List)1 ArrayAccess (org.eclipse.jdt.core.dom.ArrayAccess)1 ArrayCreation (org.eclipse.jdt.core.dom.ArrayCreation)1 ArrayInitializer (org.eclipse.jdt.core.dom.ArrayInitializer)1