use of org.evosuite.utils.GenericField 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);
}
}
use of org.evosuite.utils.GenericField 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);
}
}
}
use of org.evosuite.utils.GenericField 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);
}
}
Aggregations