Search in sources :

Example 6 with FieldStatement

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;
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) FieldStatement(org.evosuite.testcase.statements.FieldStatement)

Example 7 with FieldStatement

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);
        }
    }
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) FieldStatement(org.evosuite.testcase.statements.FieldStatement) GenericField(org.evosuite.utils.generic.GenericField)

Example 8 with FieldStatement

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;
    }
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) NullStatement(org.evosuite.testcase.statements.NullStatement) FieldStatement(org.evosuite.testcase.statements.FieldStatement) ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) FieldStatement(org.evosuite.testcase.statements.FieldStatement)

Aggregations

FieldStatement (org.evosuite.testcase.statements.FieldStatement)8 MethodStatement (org.evosuite.testcase.statements.MethodStatement)5 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)3 Statement (org.evosuite.testcase.statements.Statement)3 VariableReference (org.evosuite.testcase.variable.VariableReference)3 GenericField (org.evosuite.utils.generic.GenericField)3 HashSet (java.util.HashSet)1 CodeUnderTestException (org.evosuite.testcase.execution.CodeUnderTestException)1 NullStatement (org.evosuite.testcase.statements.NullStatement)1 PrivateFieldStatement (org.evosuite.testcase.statements.reflection.PrivateFieldStatement)1 ConstantValue (org.evosuite.testcase.variable.ConstantValue)1 FieldReference (org.evosuite.testcase.variable.FieldReference)1