Search in sources :

Example 1 with PrivateMethodStatement

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

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

CaptureType (com.googlecode.gentyref.CaptureType)2 ArrayList (java.util.ArrayList)2 ConstructionFailedException (org.evosuite.ga.ConstructionFailedException)2 PrivateFieldStatement (org.evosuite.testcase.statements.reflection.PrivateFieldStatement)2 PrivateMethodStatement (org.evosuite.testcase.statements.reflection.PrivateMethodStatement)2 GenericField (org.evosuite.utils.generic.GenericField)2 GenericMethod (org.evosuite.utils.generic.GenericMethod)2