Search in sources :

Example 1 with FieldReference

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

use of org.evosuite.testcase.FieldReference 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 3 with FieldReference

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

the class TestExtractingVisitor method retrieveVariableReference.

private VariableReference retrieveVariableReference(QualifiedName qualifiedName) {
    try {
        Class<?> referencedClass = retrieveTypeClass(qualifiedName.getQualifier().resolveTypeBinding());
        Field field = referencedClass.getField(qualifiedName.getName().getIdentifier());
        FieldReference fieldReference = new FieldReference(testCase.getReference(), new GenericField(field, referencedClass));
        Class<?> resultClass = retrieveTypeClass(qualifiedName.resolveTypeBinding());
        FieldStatement fieldStatement = new FieldStatement(testCase.getReference(), new GenericField(field, resultClass), fieldReference);
        testCase.addStatement(fieldStatement);
        return fieldStatement.getReturnValue();
    } catch (Exception exc) {
        throw new RuntimeException(exc);
    }
}
Also used : Field(java.lang.reflect.Field) GenericField(org.evosuite.utils.GenericField) FieldReference(org.evosuite.testcase.FieldReference) FieldStatement(org.evosuite.testcase.FieldStatement) GenericField(org.evosuite.utils.GenericField) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

FieldReference (org.evosuite.testcase.FieldReference)3 GenericField (org.evosuite.utils.GenericField)3 AssignmentStatement (org.evosuite.testcase.AssignmentStatement)2 VariableReference (org.evosuite.testcase.VariableReference)2 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 FieldStatement (org.evosuite.testcase.FieldStatement)1 NullStatement (org.evosuite.testcase.NullStatement)1 VariableReferenceImpl (org.evosuite.testcase.VariableReferenceImpl)1