use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.
the class EvoTestCaseCodeGenerator method createMethodCallStmt.
@Override
public void createMethodCallStmt(final CaptureLog log, final int logRecNo) {
if (log == null)
throw new IllegalArgumentException("captured log must not be null");
if (logRecNo <= -1)
throw new IllegalArgumentException("log record number is invalid: " + logRecNo);
if (isMaximumLengthReached())
return;
// assumption: all necessary statements are created and there is one variable for each referenced object
final int oid = log.objectIds.get(logRecNo);
final Object[] methodArgs = log.params.get(logRecNo);
final String methodName = log.methodNames.get(logRecNo);
Class<?> type;
try {
final String typeName = log.getTypeName(oid);
type = getClassForName(typeName);
logger.debug("Creating method call statement for call to method {}.{}", typeName, methodName);
final Class<?>[] methodParamTypeClasses = getMethodParamTypeClasses(log, logRecNo);
final ArrayList<VariableReference> args = getArguments(methodArgs, methodParamTypeClasses);
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 = log.returnValues.get(logRecNo);
if (CaptureLog.RETURN_TYPE_VOID.equals(returnValue)) {
GenericMethod genericMethod = new GenericMethod(this.getDeclaredMethod(type, methodName, methodParamTypeClasses), type);
MethodStatement m = new MethodStatement(testCase, genericMethod, this.oidToVarRefMap.get(oid), args);
testCase.addStatement(m);
} else {
// final org.objectweb.asm.Type returnType = org.objectweb.asm.Type.getReturnType(methodDesc);
logger.debug("Callee: {} ({})", this.oidToVarRefMap.get(oid), this.oidToVarRefMap.keySet());
// Person var0 = var.getPerson();
final MethodStatement m = new MethodStatement(testCase, new GenericMethod(this.getDeclaredMethod(type, methodName, methodParamTypeClasses), type), this.oidToVarRefMap.get(oid), args);
final Integer returnValueOID = (Integer) returnValue;
this.oidToVarRefMap.put(returnValueOID, testCase.addStatement(m));
}
}
} catch (NoSuchMethodException e) {
logger.info("Method not found; this may happen e.g. if an exception is thrown in the constructor");
return;
} catch (final Exception e) {
logger.info("Error at log record number {}: {}", logRecNo, e.toString());
logger.info("Test case so far: " + testCase.toCode());
logger.info(log.toString());
CodeGeneratorException.propagateError(e, "[logRecNo = %s] - an unexpected error occurred while creating method call stmt for %s.", logRecNo, methodName);
}
}
use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.
the class EvoTestCaseCodeGenerator method createFieldWriteAccessStmt.
@Override
public void createFieldWriteAccessStmt(CaptureLog log, int logRecNo) {
// assumption: all necessary statements are created and there is one variable for each referenced object
final Object[] methodArgs = log.params.get(logRecNo);
final int oid = log.objectIds.get(logRecNo);
final int captureId = log.captureIds.get(logRecNo);
final String fieldName = log.getNameOfAccessedFields(captureId);
final String typeName = log.getTypeName(oid);
try {
final Class<?> type = getClassForName(typeName);
final String fieldDesc = log.descList.get(logRecNo);
final Class<?> fieldType = CaptureUtil.getClassFromDesc(fieldDesc);
final FieldReference targetFieldRef = new FieldReference(testCase, new GenericField(this.getDeclaredField(type, fieldName), type), this.oidToVarRefMap.get(oid));
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);
logger.debug("Adding assignment statement: " + assignment.getCode());
if (arg != null) {
this.oidToVarRefMap.put(arg, varRef);
}
} catch (final Exception e) {
CodeGeneratorException.propagateError(e, "[logRecNo = %s] - an unexpected error occurred while creating field write access stmt. Log: %s", logRecNo, log);
}
}
use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.
the class EvoTestCaseCodeGenerator method createMapInitStmt.
@Override
public void createMapInitStmt(final CaptureLog log, final int logRecNo) {
try {
final int oid = log.objectIds.get(logRecNo);
final Object[] params = log.params.get(logRecNo);
String collTypeName = log.getTypeName(oid);
Class<?> collType = getClassForName(collTypeName);
// -- determine if an alternative collection must be used for code generation
final boolean isPublic = java.lang.reflect.Modifier.isPublic(collType.getModifiers());
if (!isPublic || !hasDefaultConstructor(collType)) {
collType = HashMap.class;
}
// -- create code for instantiating collection
final List<VariableReference> noParams = Collections.emptyList();
final ConstructorStatement constrStmt = new ConstructorStatement(testCase, new GenericConstructor(collType.getConstructor(new Class<?>[0]), collType), noParams);
final VariableReference collRef = testCase.addStatement(constrStmt);
this.oidToVarRefMap.put(oid, collRef);
// --- fill collection
MethodStatement methodStmt;
// is either an oid or null
Integer argOID;
ArrayList<VariableReference> paramList = new ArrayList<VariableReference>();
for (int i = 0; i < params.length; i++) {
argOID = (Integer) params[i];
if (argOID == null) {
paramList.add(testCase.addStatement(new NullStatement(testCase, Object.class)));
} else {
paramList.add(this.oidToVarRefMap.get(argOID));
}
if (i % 2 == 1) {
final Method method = collType.getMethod("put", Object.class, Object.class);
replaceNullWithNullReferences(paramList, Object.class, Object.class);
methodStmt = new MethodStatement(testCase, new GenericMethod(method, collType), collRef, paramList);
testCase.addStatement(methodStmt);
paramList = new ArrayList<VariableReference>(2);
}
}
} catch (final Exception e) {
CodeGeneratorException.propagateError(e, "[logRecNo = %s] - an unexpected error occurred while creating map init stmt", logRecNo);
}
}
use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.
the class TestCaseExpander method visitConstructorStatement.
/*
* (non-Javadoc)
*
* @see
* org.evosuite.testcase.TestVisitor#visitConstructorStatement(org.evosuite
* .testcase.ConstructorStatement)
*/
public void visitConstructorStatement(TestCase test, ConstructorStatement statement) {
int i = 0;
for (VariableReference var : statement.getParameterReferences()) {
if (var.isPrimitive() || var.isString()) {
if (usedVariables.contains(var) && test.getStatement(var.getStPosition()) instanceof PrimitiveStatement<?>) {
// Duplicate and replace
VariableReference varCopy = duplicateStatement(test, var);
statement.replaceParameterReference(varCopy, i);
usedVariables.add(varCopy);
}
usedVariables.add(var);
}
i++;
}
addUnchangedMapping(test, statement.getReturnValue());
}
use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.
the class TestCaseExpander method duplicateStatement.
private VariableReference duplicateStatement(TestCase test, VariableReference owner) {
Statement statement = test.getStatement(owner.getStPosition());
currentPosition++;
VariableReference copy = test.addStatement(statement.clone(test), owner.getStPosition() + 1);
if (!variableMapping.containsKey(owner.getStPosition())) {
variableMapping.put(owner.getStPosition(), new HashSet<VariableReference>());
// variableMapping.get(owner.getStPosition()).add(owner);
}
variableMapping.get(owner.getStPosition()).add(copy);
return copy;
}
Aggregations