use of org.evosuite.utils.generic.GenericField in project evosuite by EvoSuite.
the class TestFactory method addCallFor.
/**
* Append given call to the test case at given position
*
* @param test
* @param call
* @param position
*/
private boolean addCallFor(TestCase test, VariableReference callee, GenericAccessibleObject<?> call, int position) {
logger.trace("addCallFor {}", callee.getName());
int previousLength = test.size();
currentRecursion.clear();
try {
if (call.isMethod()) {
GenericMethod method = (GenericMethod) call;
if (call.isStatic() || !method.getDeclaringClass().isAssignableFrom(callee.getVariableClass())) {
// Static methods / methods in other classes can be modifiers of the SUT if the SUT depends on static fields
addMethod(test, method, position, 0);
} else {
addMethodFor(test, callee, (GenericMethod) call.copyWithNewOwner(callee.getGenericClass()), position);
}
} else if (call.isField()) {
// A modifier for the SUT could also be a static field in another class
if (call.isStatic()) {
addFieldAssignment(test, (GenericField) call, position, 0);
} else {
addFieldFor(test, callee, (GenericField) call.copyWithNewOwner(callee.getGenericClass()), position);
}
}
return true;
} catch (ConstructionFailedException e) {
// TODO: Check this!
logger.debug("Inserting call {} has failed: {} Removing statements", call, e);
// TODO: Doesn't work if position != test.size()
int lengthDifference = test.size() - previousLength;
for (int i = lengthDifference - 1; i >= 0; i--) {
// we need to remove them in order, so that the testcase is at all time consistent
if (logger.isDebugEnabled()) {
logger.debug(" Removing statement: " + test.getStatement(position + i).getCode());
}
test.remove(position + i);
}
if (logger.isDebugEnabled()) {
logger.debug("Test after removal: " + test.toCode());
}
return false;
}
}
use of org.evosuite.utils.generic.GenericField in project evosuite by EvoSuite.
the class TestFactory method getPossibleCalls.
/**
* Retrieve all the replacement calls that can be inserted at this position
* without changing the length
*
* @param returnType
* @param objects
* @return
*/
private List<GenericAccessibleObject<?>> getPossibleCalls(Type returnType, List<VariableReference> objects) {
List<GenericAccessibleObject<?>> calls = new ArrayList<GenericAccessibleObject<?>>();
Set<GenericAccessibleObject<?>> allCalls;
try {
allCalls = TestCluster.getInstance().getGenerators(new GenericClass(returnType), true);
} catch (ConstructionFailedException e) {
return calls;
}
for (GenericAccessibleObject<?> call : allCalls) {
Set<Type> dependencies = null;
if (call.isMethod()) {
GenericMethod method = (GenericMethod) call;
if (method.hasTypeParameters()) {
try {
call = method.getGenericInstantiation(new GenericClass(returnType));
} catch (ConstructionFailedException e) {
continue;
}
}
if (!((GenericMethod) call).getReturnType().equals(returnType))
continue;
dependencies = getDependencies((GenericMethod) call);
} else if (call.isConstructor()) {
dependencies = getDependencies((GenericConstructor) call);
} else if (call.isField()) {
if (!((GenericField) call).getFieldType().equals(returnType))
continue;
dependencies = getDependencies((GenericField) call);
} else {
assert (false);
}
if (dependenciesSatisfied(dependencies, objects)) {
calls.add(call);
}
}
return calls;
}
use of org.evosuite.utils.generic.GenericField 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.utils.generic.GenericField in project evosuite by EvoSuite.
the class DefaultTestCase method addFields.
private void addFields(List<VariableReference> variables, VariableReference var, Type type) {
if (!var.isPrimitive() && !(var instanceof NullReference)) {
// add fields of this object to list
for (Field field : TestClusterUtils.getAccessibleFields(var.getVariableClass())) {
Type fieldType = field.getType();
try {
fieldType = field.getGenericType();
} catch (java.lang.reflect.GenericSignatureFormatError e) {
// Ignore
fieldType = field.getType();
}
FieldReference f = new FieldReference(this, new GenericField(field, var.getGenericClass()), fieldType, var);
if (f.getDepth() <= 2) {
if (type != null) {
if (f.isAssignableTo(type) && !variables.contains(f)) {
variables.add(f);
}
} else if (!variables.contains(f)) {
variables.add(f);
}
}
}
}
}
use of org.evosuite.utils.generic.GenericField in project evosuite by EvoSuite.
the class FactoryTestSystemTest method testAddField.
@Test
public void testAddField() throws ConstructionFailedException, NoSuchMethodException, SecurityException, NoSuchFieldException, ClassNotFoundException {
TestFactory testFactory = TestFactory.getInstance();
Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(FactoryExample.class.getCanonicalName());
GenericField field = new GenericField(sut.getField("setMe"), sut);
DefaultTestCase test = new DefaultTestCase();
testFactory.addField(test, field, 0, 0);
assertEquals(2, test.size());
String code = test.toCode();
System.out.println(code);
assertTrue(code.contains("factoryExample0.setMe"));
assertFalse(code.contains("factoryExample1"));
}
Aggregations