Search in sources :

Example 1 with NullStatement

use of org.evosuite.testcase.NullStatement 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 2 with NullStatement

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

the class TestCaseCodeGenerator method createMethodCallStmt.

private void createMethodCallStmt(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 Object[] methodArgs = this.log.params.get(logRecNo);
    final String methodName = this.log.methodNames.get(logRecNo);
    final String methodDesc = this.log.descList.get(logRecNo);
    final org.objectweb.asm.Type[] methodParamTypes = org.objectweb.asm.Type.getArgumentTypes(methodDesc);
    final Class<?>[] methodParamTypeClasses = new Class[methodParamTypes.length];
    for (int i = 0; i < methodParamTypes.length; i++) {
        methodParamTypeClasses[i] = getClassFromType(methodParamTypes[i]);
    }
    final String typeName = this.log.oidClassNames.get(this.log.oidRecMapping.get(oid));
    Class<?> type;
    try {
        // Class.forName(typeName, true, StaticTestCluster.classLoader);
        type = getClassForName(typeName);
        final ArrayList<VariableReference> args = new ArrayList<VariableReference>();
        // is either an oid or null
        Integer argOID;
        for (int i = 0; i < methodArgs.length; i++) {
            argOID = (Integer) methodArgs[i];
            if (argOID == null) {
                args.add(testCase.addStatement(new NullStatement(testCase, methodParamTypeClasses[i])));
            } else {
                args.add(this.oidToVarRefMap.get(argOID));
            }
        }
        if (CaptureLog.OBSERVED_INIT.equals(methodName)) {
            // Person var0 = new Person();
            final ConstructorStatement constStmt = new ConstructorStatement(testCase, new GenericConstructor(type.getDeclaredConstructor(methodParamTypeClasses), type), args);
            this.oidToVarRefMap.put(oid, testCase.addStatement(constStmt));
        } else // ------------------ handling for ordinary method calls e.g. var1 = var0.doSth();
        {
            final Object returnValue = this.log.returnValues.get(logRecNo);
            if (CaptureLog.RETURN_TYPE_VOID.equals(returnValue)) {
                final MethodStatement m = new MethodStatement(testCase, new GenericMethod(this.getDeclaredMethod(type, methodName, methodParamTypeClasses), type.getMethod(methodName, methodParamTypeClasses).getReturnType()), this.oidToVarRefMap.get(oid), args);
                testCase.addStatement(m);
            } else {
                final org.objectweb.asm.Type returnType = org.objectweb.asm.Type.getReturnType(methodDesc);
                // Person var0 = var.getPerson();
                final MethodStatement m = new MethodStatement(testCase, new GenericMethod(this.getDeclaredMethod(type, methodName, methodParamTypeClasses), getClassFromType(returnType)), this.oidToVarRefMap.get(oid), args);
                final Integer returnValueOID = (Integer) returnValue;
                this.oidToVarRefMap.put(returnValueOID, testCase.addStatement(m));
            }
        }
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : ConstructorStatement(org.evosuite.testcase.ConstructorStatement) MethodStatement(org.evosuite.testcase.MethodStatement) VariableReference(org.evosuite.testcase.VariableReference) TIntArrayList(gnu.trove.list.array.TIntArrayList) ArrayList(java.util.ArrayList) GenericConstructor(org.evosuite.utils.GenericConstructor) GenericMethod(org.evosuite.utils.GenericMethod) NullStatement(org.evosuite.testcase.NullStatement)

Aggregations

NullStatement (org.evosuite.testcase.NullStatement)2 VariableReference (org.evosuite.testcase.VariableReference)2 TIntArrayList (gnu.trove.list.array.TIntArrayList)1 ArrayList (java.util.ArrayList)1 AssignmentStatement (org.evosuite.testcase.AssignmentStatement)1 ConstructorStatement (org.evosuite.testcase.ConstructorStatement)1 FieldReference (org.evosuite.testcase.FieldReference)1 MethodStatement (org.evosuite.testcase.MethodStatement)1 GenericConstructor (org.evosuite.utils.GenericConstructor)1 GenericField (org.evosuite.utils.GenericField)1 GenericMethod (org.evosuite.utils.GenericMethod)1