Search in sources :

Example 26 with VariableReference

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

the class TestExtractingVisitor method getLengths.

private int[] getLengths(VariableDeclarationStatement variableDeclStmt, List<VariableReference> lengthsVarRefs) {
    int[] lengths = new int[lengthsVarRefs.size()];
    String variable = retrieveVariableName(variableDeclStmt);
    int lineNumber = testReader.getLineNumber(variableDeclStmt.getStartPosition());
    Object value = null;
    if (cursorableTrace != null) {
        value = cursorableTrace.getVariableValueAfter(lineNumber, variable);
    }
    int idx = 0;
    while ((value != null) && value.getClass().isArray()) {
        lengths[idx] = Array.getLength(value);
        if (lengths[idx] > 0) {
            value = Array.get(value, 0);
        } else {
            value = null;
        }
        idx++;
    }
    if (idx == lengths.length) {
        return lengths;
    }
    idx = 0;
    for (VariableReference lengthVarRef : lengthsVarRefs) {
        // TODO This is a hack. We should use the variablereferences
        // instead!
        lengths[idx] = Integer.valueOf(lengthVarRef.toString());
        idx++;
    }
    return lengths;
}
Also used : VariableReference(org.evosuite.testcase.VariableReference)

Example 27 with VariableReference

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

VariableReference (org.evosuite.testcase.VariableReference)27 PrimitiveExpression (org.evosuite.testcase.PrimitiveExpression)7 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)6 SuperMethodInvocation (org.eclipse.jdt.core.dom.SuperMethodInvocation)6 AssignmentStatement (org.evosuite.testcase.AssignmentStatement)6 ArrayList (java.util.ArrayList)5 CastExpression (org.eclipse.jdt.core.dom.CastExpression)5 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)5 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)5 Expression (org.eclipse.jdt.core.dom.Expression)5 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)5 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)5 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)5 ArrayIndex (org.evosuite.testcase.ArrayIndex)4 ArrayReference (org.evosuite.testcase.ArrayReference)4 GenericMethod (org.evosuite.utils.GenericMethod)4 ASTNode (org.eclipse.jdt.core.dom.ASTNode)3 Assignment (org.eclipse.jdt.core.dom.Assignment)3 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)3 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)3