Search in sources :

Example 1 with AssignmentStatement

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

the class CompoundTestCase method convertMethod.

/**
 * <p>
 * convertMethod
 * </p>
 *
 * @param methodDef
 *            a {@link org.evosuite.junit.CompoundTestCase.MethodDef}
 *            object.
 * @param params
 *            a {@link java.util.List} object.
 * @param retVal
 *            a {@link org.evosuite.testcase.VariableReference} object.
 */
public void convertMethod(MethodDef methodDef, List<VariableReference> params, VariableReference retVal) {
    assert methodDef.getParams().size() == params.size();
    Map<VariableReference, VariableReference> methodVarsMap = new HashMap<VariableReference, VariableReference>();
    for (StatementInterface statement : methodDef.getCode()) {
        for (int idx = 0; idx < params.size(); idx++) {
            statement.replace(methodDef.getParams().get(idx), params.get(idx));
        }
        if (statement instanceof ReturnStatementPlaceholder) {
            VariableReference resultVal = methodVarsMap.get(statement.getReturnValue());
            if (resultVal == null) {
                throw new IllegalStateException();
            }
            AssignmentStatement assignmentStatement = new AssignmentStatement(delegate, retVal, resultVal);
            addStatement(assignmentStatement);
            return;
        }
        StatementInterface newStmt = statement;
        if (!(statement instanceof PrimitiveExpression)) {
            // Since the delegate code is not yet finished,
            // cloning of PrimitiveExpressions does not work.
            newStmt = statement.clone(delegate);
        }
        addReplacementVariable(statement.getReturnValue(), newStmt.getReturnValue());
        methodVarsMap.put(statement.getReturnValue(), newStmt.getReturnValue());
        addStatement(newStmt);
    }
}
Also used : StatementInterface(org.evosuite.testcase.StatementInterface) VariableReference(org.evosuite.testcase.VariableReference) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) AssignmentStatement(org.evosuite.testcase.AssignmentStatement) PrimitiveExpression(org.evosuite.testcase.PrimitiveExpression)

Example 2 with AssignmentStatement

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

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

the class TestCaseCodeGenerator method createFieldWriteAccessStmt.

private void createFieldWriteAccessStmt(final int logRecNo, final TestCase testCase) {
    // assumption: all necessary statements are created and there is one variable for reach referenced object
    final Object[] methodArgs = this.log.params.get(logRecNo);
    final int oid = this.log.objectIds.get(logRecNo);
    final int captureId = this.log.captureIds.get(logRecNo);
    final String fieldName = this.log.namesOfAccessedFields.get(captureId);
    final String typeName = this.log.oidClassNames.get(this.log.oidRecMapping.get(oid));
    try {
        final Class<?> type = getClassForName(typeName);
        final String fieldDesc = this.log.descList.get(logRecNo);
        final Class<?> fieldType = CaptureUtil.getClassFromDesc(fieldDesc);
        final FieldReference targetFieldRef = new FieldReference(testCase, new GenericField(this.getDeclaredField(type, fieldName), type));
        final AssignmentStatement assignment;
        final Integer arg = (Integer) methodArgs[0];
        if (arg == null) {
            final NullStatement nullStmt = new NullStatement(testCase, fieldType);
            final VariableReference nullReference = testCase.addStatement(nullStmt);
            assignment = new AssignmentStatement(testCase, targetFieldRef, nullReference);
        } else {
            assignment = new AssignmentStatement(testCase, targetFieldRef, this.oidToVarRefMap.get(arg));
        }
        final VariableReference varRef = testCase.addStatement(assignment);
        if (arg != null) {
            this.oidToVarRefMap.put(arg, varRef);
        }
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : FieldReference(org.evosuite.testcase.FieldReference) VariableReference(org.evosuite.testcase.VariableReference) NullStatement(org.evosuite.testcase.NullStatement) AssignmentStatement(org.evosuite.testcase.AssignmentStatement) GenericField(org.evosuite.utils.GenericField)

Example 4 with AssignmentStatement

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

the class TestCaseCodeGenerator method createFieldReadAccessStmt.

private void createFieldReadAccessStmt(final int logRecNo, final TestCase testCase) {
    // assumption: all necessary statements are created and there is one variable for reach referenced object
    final int oid = this.log.objectIds.get(logRecNo);
    final int captureId = this.log.captureIds.get(logRecNo);
    final Object returnValue = this.log.returnValues.get(logRecNo);
    if (// TODO necessary?
    !CaptureLog.RETURN_TYPE_VOID.equals(returnValue)) {
        Integer returnValueOID = (Integer) returnValue;
        final String descriptor = this.log.descList.get(logRecNo);
        final org.objectweb.asm.Type fieldTypeType = org.objectweb.asm.Type.getType(descriptor);
        final String typeName = this.log.oidClassNames.get(this.log.oidRecMapping.get(oid));
        final String fieldName = this.log.namesOfAccessedFields.get(captureId);
        try {
            // Class.forName(fieldTypeName, true, StaticTestCluster.classLoader);
            final Class<?> fieldType = getClassFromType(fieldTypeType);
            // Class.forName(typeName, true, StaticTestCluster.classLoader);
            final Class<?> type = getClassForName(typeName);
            final FieldReference valueRef = new FieldReference(testCase, new GenericField(type.getField(fieldName), type));
            final VariableReference targetVar = new VariableReferenceImpl(testCase, fieldType);
            final AssignmentStatement assignment = new AssignmentStatement(testCase, targetVar, valueRef);
            VariableReference varRef = testCase.addStatement(assignment);
            this.oidToVarRefMap.put(returnValueOID, varRef);
        } catch (final Exception e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : FieldReference(org.evosuite.testcase.FieldReference) VariableReference(org.evosuite.testcase.VariableReference) AssignmentStatement(org.evosuite.testcase.AssignmentStatement) VariableReferenceImpl(org.evosuite.testcase.VariableReferenceImpl) GenericField(org.evosuite.utils.GenericField)

Example 5 with AssignmentStatement

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

Aggregations

AssignmentStatement (org.evosuite.testcase.AssignmentStatement)6 VariableReference (org.evosuite.testcase.VariableReference)6 ArrayIndex (org.evosuite.testcase.ArrayIndex)3 ArrayList (java.util.ArrayList)2 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)2 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)2 SuperMethodInvocation (org.eclipse.jdt.core.dom.SuperMethodInvocation)2 ArrayReference (org.evosuite.testcase.ArrayReference)2 ArrayStatement (org.evosuite.testcase.ArrayStatement)2 FieldReference (org.evosuite.testcase.FieldReference)2 PrimitiveExpression (org.evosuite.testcase.PrimitiveExpression)2 GenericField (org.evosuite.utils.GenericField)2 AbstractList (java.util.AbstractList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 ArrayCreation (org.eclipse.jdt.core.dom.ArrayCreation)1 ArrayInitializer (org.eclipse.jdt.core.dom.ArrayInitializer)1 CastExpression (org.eclipse.jdt.core.dom.CastExpression)1 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)1