Search in sources :

Example 1 with FieldStatement

use of org.evosuite.testcase.statements.FieldStatement in project evosuite by EvoSuite.

the class ConstantInliner method afterStatement.

/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.evosuite.testcase.ExecutionObserver#statement(org.evosuite.testcase.
	 * StatementInterface, org.evosuite.testcase.Scope, java.lang.Throwable)
	 */
/**
 * {@inheritDoc}
 */
@Override
public void afterStatement(Statement statement, Scope scope, Throwable exception) {
    try {
        for (VariableReference var : statement.getVariableReferences()) {
            if (var.equals(statement.getReturnValue()) || var.equals(statement.getReturnValue().getAdditionalVariableReference()))
                continue;
            Object object = var.getObject(scope);
            if (var.isPrimitive()) {
                ConstantValue value = new ConstantValue(test, var.getGenericClass());
                value.setValue(object);
                // logger.info("Statement before inlining: " +
                // statement.getCode());
                statement.replace(var, value);
            // logger.info("Statement after inlining: " +
            // statement.getCode());
            } else if (var.isString() && object != null) {
                ConstantValue value = new ConstantValue(test, var.getGenericClass());
                try {
                    String val = StringEscapeUtils.unescapeJava(new String(object.toString()));
                    if (val.length() < Properties.MAX_STRING) {
                        value.setValue(val);
                        statement.replace(var, value);
                    }
                } catch (IllegalArgumentException e) {
                    // Exceptions may happen if strings are not valid
                    // unicode
                    logger.info("Cannot escape invalid string: " + object);
                }
            // logger.info("Statement after inlining: " +
            // statement.getCode());
            } else if (var.isArrayIndex()) {
                // then replace the array index with that object
                for (VariableReference otherVar : scope.getElements(var.getType())) {
                    Object otherObject = otherVar.getObject(scope);
                    if (otherObject == object && !otherVar.isArrayIndex() && otherVar.getStPosition() < statement.getPosition()) {
                        statement.replace(var, otherVar);
                        break;
                    }
                }
            } else {
                // the assertion for now
                if (object == null) {
                    if (statement instanceof MethodStatement) {
                        MethodStatement ms = (MethodStatement) statement;
                        if (var.equals(ms.getCallee())) {
                            // Don't put null in callee's, the compiler will not accept it
                            continue;
                        }
                    } else if (statement instanceof FieldStatement) {
                        FieldStatement fs = (FieldStatement) statement;
                        if (var.equals(fs.getSource())) {
                            // Don't put null in source, the compiler will not accept it
                            continue;
                        }
                    }
                    ConstantValue value = new ConstantValue(test, var.getGenericClass());
                    value.setValue(null);
                    // logger.info("Statement before inlining: " +
                    // statement.getCode());
                    statement.replace(var, value);
                // logger.info("Statement after inlining: " +
                // statement.getCode());
                }
            }
        }
    } catch (CodeUnderTestException e) {
        logger.warn("Not inlining test: " + e.getCause());
    // throw new AssertionError("This case isn't handled yet: " +
    // e.getCause()
    // + ", " + Arrays.asList(e.getStackTrace()));
    }
}
Also used : MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) FieldStatement(org.evosuite.testcase.statements.FieldStatement) CodeUnderTestException(org.evosuite.testcase.execution.CodeUnderTestException) ConstantValue(org.evosuite.testcase.variable.ConstantValue)

Example 2 with FieldStatement

use of org.evosuite.testcase.statements.FieldStatement in project evosuite by EvoSuite.

the class ClassReInitializer method getMoreClassesToReset.

/*
	 * TODO: I think this would be nicer if each type of statement registered
	 * the classes to reset as part of their execute() method
	 */
private static HashSet<String> getMoreClassesToReset(TestCase tc, ExecutionResult result) {
    HashSet<String> moreClassesForStaticReset = new HashSet<String>();
    for (int position = 0; position < result.getExecutedStatements(); position++) {
        Statement statement = tc.getStatement(position);
        // If we reset also after reads, get all fields
        if (Properties.RESET_STATIC_FIELD_GETS) {
            for (VariableReference var : statement.getVariableReferences()) {
                if (var.isFieldReference()) {
                    FieldReference fieldReference = (FieldReference) var;
                    moreClassesForStaticReset.add(fieldReference.getField().getOwnerClass().getClassName());
                }
            }
        }
        // Check for explicit assignments to static fields
        if (statement.isAssignmentStatement()) {
            if (statement.getReturnValue() instanceof FieldReference) {
                FieldReference fieldReference = (FieldReference) statement.getReturnValue();
                if (fieldReference.getField().isStatic()) {
                    moreClassesForStaticReset.add(fieldReference.getField().getOwnerClass().getClassName());
                }
            }
        } else if (statement instanceof FieldStatement) {
            // Check if we are invoking a non-pure method on a static field
            // variable
            FieldStatement fieldStatement = (FieldStatement) statement;
            if (fieldStatement.getField().isStatic()) {
                VariableReference fieldReference = fieldStatement.getReturnValue();
                if (Properties.RESET_STATIC_FIELD_GETS) {
                    moreClassesForStaticReset.add(fieldStatement.getField().getOwnerClass().getClassName());
                } else {
                    // Check if the field was passed to a non-pure method
                    for (int i = fieldStatement.getPosition() + 1; i < result.getExecutedStatements(); i++) {
                        Statement invokedStatement = tc.getStatement(i);
                        if (invokedStatement.references(fieldReference)) {
                            if (invokedStatement instanceof MethodStatement) {
                                if (fieldReference.equals(((MethodStatement) invokedStatement).getCallee())) {
                                    if (!CheapPurityAnalyzer.getInstance().isPure(((MethodStatement) invokedStatement).getMethod().getMethod())) {
                                        moreClassesForStaticReset.add(fieldStatement.getField().getOwnerClass().getClassName());
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } else if (statement instanceof PrivateFieldStatement) {
            PrivateFieldStatement fieldStatement = (PrivateFieldStatement) statement;
            if (fieldStatement.isStaticField()) {
                moreClassesForStaticReset.add(fieldStatement.getOwnerClassName());
            }
        }
    }
    return moreClassesForStaticReset;
}
Also used : MethodStatement(org.evosuite.testcase.statements.MethodStatement) FieldReference(org.evosuite.testcase.variable.FieldReference) VariableReference(org.evosuite.testcase.variable.VariableReference) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) FieldStatement(org.evosuite.testcase.statements.FieldStatement) PrivateFieldStatement(org.evosuite.testcase.statements.reflection.PrivateFieldStatement) FieldStatement(org.evosuite.testcase.statements.FieldStatement) PrivateFieldStatement(org.evosuite.testcase.statements.reflection.PrivateFieldStatement) PrivateFieldStatement(org.evosuite.testcase.statements.reflection.PrivateFieldStatement) HashSet(java.util.HashSet)

Example 3 with FieldStatement

use of org.evosuite.testcase.statements.FieldStatement in project evosuite by EvoSuite.

the class TestCaseBuilder method appendFieldStmt.

public VariableReference appendFieldStmt(VariableReference receiver, Field field) {
    if (receiver == null) {
        throw new NullPointerException("Receiver object for a non-static field cannot be null");
    }
    FieldStatement stmt = new FieldStatement(tc, new GenericField(field, receiver.getType()), receiver);
    tc.addStatement(stmt);
    return stmt.getReturnValue();
}
Also used : FieldStatement(org.evosuite.testcase.statements.FieldStatement) GenericField(org.evosuite.utils.generic.GenericField)

Example 4 with FieldStatement

use of org.evosuite.testcase.statements.FieldStatement in project evosuite by EvoSuite.

the class ParameterLocalSearch method doSearch.

/* (non-Javadoc)
	 * @see org.evosuite.testcase.LocalSearch#doSearch(org.evosuite.testcase.TestChromosome, int, org.evosuite.ga.LocalSearchObjective)
	 */
/**
 * {@inheritDoc}
 */
@Override
public boolean doSearch(TestChromosome test, int statement, LocalSearchObjective<TestChromosome> objective) {
    Statement stmt = test.getTestCase().getStatement(statement);
    backup(test, stmt);
    if (stmt instanceof MethodStatement) {
        return doSearch(test, (MethodStatement) stmt, objective);
    } else if (stmt instanceof ConstructorStatement) {
        return doSearch(test, (ConstructorStatement) stmt, objective);
    } else if (stmt instanceof FieldStatement) {
        return doSearch(test, (FieldStatement) stmt, objective);
    } 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) FieldStatement(org.evosuite.testcase.statements.FieldStatement) ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) FieldStatement(org.evosuite.testcase.statements.FieldStatement)

Example 5 with FieldStatement

use of org.evosuite.testcase.statements.FieldStatement in project evosuite by EvoSuite.

the class TestCaseBuilder method appendStaticFieldStmt.

public VariableReference appendStaticFieldStmt(Field field) {
    Class<?> declaringClass = field.getDeclaringClass();
    final GenericField genericField = new GenericField(field, declaringClass);
    // static field (receiver is null)
    FieldStatement stmt = new FieldStatement(tc, genericField, null);
    tc.addStatement(stmt);
    return stmt.getReturnValue();
}
Also used : FieldStatement(org.evosuite.testcase.statements.FieldStatement) GenericField(org.evosuite.utils.generic.GenericField)

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