use of org.evosuite.testcase.statements.FieldStatement in project evosuite by EvoSuite.
the class Contract method isTargetStatement.
/**
* Check if this statement is related to the unit under test
*
* @param statement
* a {@link org.evosuite.testcase.statements.Statement} object.
* @return a boolean.
*/
protected boolean isTargetStatement(Statement statement) {
// return true;
if (statement instanceof MethodStatement) {
MethodStatement ms = (MethodStatement) statement;
final Class<?> targetClass = Properties.getTargetClassAndDontInitialise();
if (targetClass.equals(ms.getMethod().getDeclaringClass()))
return true;
} else if (statement instanceof ConstructorStatement) {
ConstructorStatement cs = (ConstructorStatement) statement;
final Class<?> targetClass = Properties.getTargetClassAndDontInitialise();
if (targetClass.equals(cs.getConstructor().getDeclaringClass()))
return true;
} else if (statement instanceof FieldStatement) {
FieldStatement fs = (FieldStatement) statement;
final Class<?> targetClass = Properties.getTargetClassAndDontInitialise();
if (targetClass.equals(fs.getField().getDeclaringClass()))
return true;
}
return false;
}
use of org.evosuite.testcase.statements.FieldStatement in project evosuite by EvoSuite.
the class EvoTestCaseCodeGenerator method createFieldReadAccessStmt.
@Override
public void createFieldReadAccessStmt(CaptureLog log, int logRecNo) {
// assumption: all necessary statements are created and there is one variable for reach referenced object
final int oid = log.objectIds.get(logRecNo);
final int captureId = log.captureIds.get(logRecNo);
final Object returnValue = log.returnValues.get(logRecNo);
if (// TODO necessary?
!CaptureLog.RETURN_TYPE_VOID.equals(returnValue)) {
Integer returnValueOID = (Integer) returnValue;
// final String descriptor = log.descList.get(logRecNo);
// final org.objectweb.asm.Type fieldTypeType = org.objectweb.asm.Type.getType(descriptor);
final String typeName = log.getTypeName(oid);
final String fieldName = log.getNameOfAccessedFields(captureId);
try {
// final Class<?> fieldType = getClassFromType(fieldTypeType);
final Class<?> type = getClassForName(typeName);
// final FieldReference valueRef = new FieldReference(testCase,
// new GenericField(getDeclaredField(type, fieldName), type));
// final VariableReference targetVar = new VariableReferenceImpl(testCase,
// fieldType);
final FieldStatement fieldStatement = new FieldStatement(testCase, new GenericField(FieldUtils.getField(type, fieldName, true), type), this.oidToVarRefMap.get(oid));
// final AssignmentStatement assignment = new AssignmentStatement(testCase,
// targetVar, valueRef);
// VariableReference varRef = testCase.addStatement(assignment);
VariableReference varRef = testCase.addStatement(fieldStatement);
this.oidToVarRefMap.put(returnValueOID, varRef);
} catch (final Exception e) {
logger.debug("Error while trying to get field " + fieldName + " of class " + getClassForName(typeName) + ": " + e);
CodeGeneratorException.propagateError(e, "[logRecNo = %s] - an unexpected error occurred while creating field read access stmt. Log: %s", logRecNo, log);
}
}
}
use of org.evosuite.testcase.statements.FieldStatement in project evosuite by EvoSuite.
the class ReferenceLocalSearch method changeParameters.
/**
* Switch parameter/callee variables with other available objects
*
* @param test
* @param statement
* @return
*/
private boolean changeParameters(TestChromosome test, int statement) {
logger.debug("Changing parameters");
Statement stmt = test.getTestCase().getStatement(statement);
if (stmt instanceof MethodStatement) {
return replaceMethodParameter(test, (MethodStatement) stmt);
} else if (stmt instanceof ConstructorStatement) {
return replaceConstructorParameter(test, (ConstructorStatement) stmt);
} else if (stmt instanceof FieldStatement) {
return replaceFieldSource(test, (FieldStatement) stmt);
} else {
return false;
}
}
Aggregations