Search in sources :

Example 1 with PrivateFieldStatement

use of org.evosuite.testcase.statements.reflection.PrivateFieldStatement 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 2 with PrivateFieldStatement

use of org.evosuite.testcase.statements.reflection.PrivateFieldStatement in project evosuite by EvoSuite.

the class TestFactory method insertRandomReflectionCallOnObject.

private boolean insertRandomReflectionCallOnObject(TestCase test, VariableReference callee, int position, int recursionDepth) throws ConstructionFailedException {
    logger.debug("Recursion depth: " + recursionDepth);
    if (recursionDepth > Properties.MAX_RECURSION) {
        logger.debug("Max recursion depth reached");
        throw new ConstructionFailedException("Max recursion depth reached");
    }
    if (!reflectionFactory.getReflectedClass().isAssignableFrom(callee.getVariableClass())) {
        logger.debug("Reflection not performed on class {}", callee.getVariableClass());
        return false;
    }
    int length = test.size();
    List<VariableReference> parameters = null;
    Statement st = null;
    if (reflectionFactory.nextUseField()) {
        Field field = reflectionFactory.nextField();
        /*
				In theory, there might be cases in which using null in PA might help increasing
				coverage. However, likely most of the time we ll end up in useless tests throwing
				NPE on the private fields. As we maximize the number of methods throwing exceptions,
				we could end up with a lot of useless tests
			 */
        boolean allowNull = false;
        // Added 'null' as additional parameter - fix for @NotNull annotations issue on evo mailing list
        parameters = satisfyParameters(test, callee, // we need a reference to the SUT, and one to a variable of same type of chosen field
        Arrays.asList((Type) field.getType()), null, position, recursionDepth + 1, allowNull, false, true);
        try {
            st = new PrivateFieldStatement(test, reflectionFactory.getReflectedClass(), field.getName(), callee, parameters.get(0));
        } catch (NoSuchFieldException e) {
            logger.error("Reflection problem: " + e, e);
            throw new ConstructionFailedException("Reflection problem");
        }
    } else {
        // method
        Method method = reflectionFactory.nextMethod();
        List<Type> list = new ArrayList<>();
        list.addAll(Arrays.asList(method.getParameterTypes()));
        // Added 'null' as additional parameter - fix for @NotNull annotations issue on evo mailing list
        parameters = satisfyParameters(test, callee, list, null, position, recursionDepth + 1, true, false, true);
        st = new PrivateMethodStatement(test, reflectionFactory.getReflectedClass(), method, callee, parameters, Modifier.isStatic(method.getModifiers()));
    }
    int newLength = test.size();
    position += (newLength - length);
    test.addStatement(st, position);
    return true;
}
Also used : PrivateMethodStatement(org.evosuite.testcase.statements.reflection.PrivateMethodStatement) PrivateMethodStatement(org.evosuite.testcase.statements.reflection.PrivateMethodStatement) PrivateFieldStatement(org.evosuite.testcase.statements.reflection.PrivateFieldStatement) ArrayList(java.util.ArrayList) PrivateFieldStatement(org.evosuite.testcase.statements.reflection.PrivateFieldStatement) GenericMethod(org.evosuite.utils.generic.GenericMethod) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException) GenericField(org.evosuite.utils.generic.GenericField) CaptureType(com.googlecode.gentyref.CaptureType)

Example 3 with PrivateFieldStatement

use of org.evosuite.testcase.statements.reflection.PrivateFieldStatement in project evosuite by EvoSuite.

the class TestFactory method insertRandomReflectionCall.

private boolean insertRandomReflectionCall(TestCase test, int position, int recursionDepth) throws ConstructionFailedException {
    logger.debug("Recursion depth: " + recursionDepth);
    if (recursionDepth > Properties.MAX_RECURSION) {
        logger.debug("Max recursion depth reached");
        throw new ConstructionFailedException("Max recursion depth reached");
    }
    int length = test.size();
    List<VariableReference> parameters = null;
    Statement st = null;
    if (reflectionFactory.nextUseField()) {
        Field field = reflectionFactory.nextField();
        parameters = satisfyParameters(test, null, // we need a reference to the SUT, and one to a variable of same type of chosen field
        Arrays.asList((Type) reflectionFactory.getReflectedClass(), (Type) field.getType()), null, position, recursionDepth + 1, true, false, true);
        try {
            st = new PrivateFieldStatement(test, reflectionFactory.getReflectedClass(), field.getName(), parameters.get(0), parameters.get(1));
        } catch (NoSuchFieldException e) {
            logger.error("Reflection problem: " + e, e);
            throw new ConstructionFailedException("Reflection problem");
        }
    } else {
        // method
        Method method = reflectionFactory.nextMethod();
        List<Type> list = new ArrayList<>();
        list.add(reflectionFactory.getReflectedClass());
        list.addAll(Arrays.asList(method.getGenericParameterTypes()));
        // Added 'null' as additional parameter - fix for @NotNull annotations issue on evo mailing list
        parameters = satisfyParameters(test, null, list, null, position, recursionDepth + 1, true, false, true);
        VariableReference callee = parameters.remove(0);
        st = new PrivateMethodStatement(test, reflectionFactory.getReflectedClass(), method, callee, parameters, Modifier.isStatic(method.getModifiers()));
    }
    int newLength = test.size();
    position += (newLength - length);
    test.addStatement(st, position);
    return true;
}
Also used : PrivateMethodStatement(org.evosuite.testcase.statements.reflection.PrivateMethodStatement) PrivateMethodStatement(org.evosuite.testcase.statements.reflection.PrivateMethodStatement) PrivateFieldStatement(org.evosuite.testcase.statements.reflection.PrivateFieldStatement) ArrayList(java.util.ArrayList) PrivateFieldStatement(org.evosuite.testcase.statements.reflection.PrivateFieldStatement) GenericMethod(org.evosuite.utils.generic.GenericMethod) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException) GenericField(org.evosuite.utils.generic.GenericField) CaptureType(com.googlecode.gentyref.CaptureType)

Aggregations

PrivateFieldStatement (org.evosuite.testcase.statements.reflection.PrivateFieldStatement)3 CaptureType (com.googlecode.gentyref.CaptureType)2 ArrayList (java.util.ArrayList)2 ConstructionFailedException (org.evosuite.ga.ConstructionFailedException)2 PrivateMethodStatement (org.evosuite.testcase.statements.reflection.PrivateMethodStatement)2 GenericField (org.evosuite.utils.generic.GenericField)2 GenericMethod (org.evosuite.utils.generic.GenericMethod)2 HashSet (java.util.HashSet)1 FieldStatement (org.evosuite.testcase.statements.FieldStatement)1 MethodStatement (org.evosuite.testcase.statements.MethodStatement)1 Statement (org.evosuite.testcase.statements.Statement)1 FieldReference (org.evosuite.testcase.variable.FieldReference)1 VariableReference (org.evosuite.testcase.variable.VariableReference)1