Search in sources :

Example 11 with VariableReference

use of org.evosuite.testcase.VariableReference in project evosuite by EvoSuite.

the class TestExtractingVisitor method endVisit.

/**
 * {@inheritDoc}
 */
@Override
public void endVisit(SuperMethodInvocation superMethodInvocation) {
    List<?> paramTypes = Arrays.asList(superMethodInvocation.resolveMethodBinding().getParameterTypes());
    List<VariableReference> params = convertParams(superMethodInvocation.arguments(), paramTypes);
    String name = superMethodInvocation.getName().getIdentifier();
    MethodDef methodDef = testCase.getParent().getMethod(name);
    VariableReference retVal = retrieveResultReference(superMethodInvocation);
    retVal.setOriginalCode(superMethodInvocation.toString());
    testCase.convertMethod(methodDef, params, retVal);
}
Also used : VariableReference(org.evosuite.testcase.VariableReference) MethodDef(org.evosuite.junit.CompoundTestCase.MethodDef)

Example 12 with VariableReference

use of org.evosuite.testcase.VariableReference in project evosuite by EvoSuite.

the class TestExtractingVisitor method retrieveMethod.

private Method retrieveMethod(MethodInvocation methodInvocation, IMethodBinding methodBinding, List<VariableReference> params) {
    assert methodBinding != null : "Sources missing!";
    String methodName = methodInvocation.getName().getIdentifier();
    Class<?> clazz = retrieveTypeClass(methodBinding.getDeclaringClass());
    Set<Method> methods = retrieveMethods(clazz, methodName, params);
    if (methods.size() == 1) {
        return methods.iterator().next();
    }
    // TODO Implement varargs methods
    try {
        Class<?>[] paramClasses = new Class<?>[methodInvocation.arguments().size()];
        assert methods.size() > 0;
        for (int idx = 0; idx < methodInvocation.arguments().size(); idx++) {
            ITypeBinding paramTypeBinding = methodBinding.getParameterTypes()[idx];
            paramClasses[idx] = retrieveTypeClass(paramTypeBinding);
            VariableReference param = params.get(idx);
            if (!param.isAssignableFrom(paramClasses[idx])) {
                if (methods.size() == 0) {
                    throw new IllegalStateException("Param class and argument do not match!");
                }
                throw new WrongMethodBindingException();
            }
        }
        try {
            return clazz.getMethod(methodName, paramClasses);
        } catch (Exception exc) {
            try {
                return clazz.getDeclaredMethod(methodName, paramClasses);
            } catch (Exception exc2) {
                throw new RuntimeException(exc);
            }
        }
    } catch (WrongMethodBindingException exc) {
        logger.debug("The resolved method binding is wrong. Will manually correct it...");
        if (methods.size() > 1) {
            logger.warn("Cannot unambiguously determine the method '{}'.", methodName);
        }
        return methods.iterator().next();
    }
}
Also used : VariableReference(org.evosuite.testcase.VariableReference) GenericMethod(org.evosuite.utils.GenericMethod) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) GenericClass(org.evosuite.utils.GenericClass)

Example 13 with VariableReference

use of org.evosuite.testcase.VariableReference in project evosuite by EvoSuite.

the class TestExtractingVisitor method convertParams.

private List<VariableReference> convertParams(List<?> params, List<?> argumentTypes) {
    List<VariableReference> result = new ArrayList<VariableReference>();
    if ((params.size() == 0) && (argumentTypes.size() == 0)) {
        return result;
    }
    if ((argumentTypes.size() > params.size()) && (argumentTypes.size() - 1 != params.size())) {
        throw new RuntimeException("Number of declared and actual params do not match!");
    }
    int limit = argumentTypes.size();
    Class<?> lastDeclaredParamType = retrieveTypeClass(argumentTypes.get(argumentTypes.size() - 1));
    if (lastDeclaredParamType.isArray()) {
        limit = argumentTypes.size() - 1;
    }
    for (int idx = 0; idx < limit; idx++) {
        if (idx >= params.size()) {
            break;
        }
        Object argument = params.get(idx);
        if ((argument instanceof MethodInvocation) || (argument instanceof ClassInstanceCreation)) {
            assert !nestedCallResults.isEmpty();
            result.add(nestedCallResults.pop());
            continue;
        }
        Class<?> argClass = retrieveTypeClass(argumentTypes.get(idx));
        VariableReference argRef = retrieveVariableReference(argument, argClass);
        argRef.setOriginalCode(argument.toString());
        result.add(argRef);
    }
    if (limit == argumentTypes.size()) {
        return result;
    }
    assert lastDeclaredParamType.isArray();
    if (argumentTypes.size() == params.size()) {
        Object lastParam = params.get(params.size() - 1);
        Class<?> lastActualParamType = retrieveTypeClass(lastParam);
        if (lastParam instanceof MethodInvocation) {
            assert !nestedCallResults.isEmpty();
            lastActualParamType = nestedCallResults.peek().getVariableClass();
        }
        if (lastActualParamType.isArray()) {
            if ((lastParam instanceof MethodInvocation) || (lastParam instanceof ClassInstanceCreation)) {
                assert !nestedCallResults.isEmpty();
                result.add(nestedCallResults.pop());
            } else {
                result.add(retrieveVariableReference(lastParam, null));
            }
            return result;
        }
    }
    ArrayReference arrayReference = new ValidArrayReference(testCase.getReference(), lastDeclaredParamType);
    arrayReference.setOriginalCode(params.toString());
    ArrayStatement arrayStatement = new ArrayStatement(testCase.getReference(), arrayReference);
    testCase.addStatement(arrayStatement);
    result.add(arrayStatement.getReturnValue());
    arrayStatement.setSize(params.size() - (argumentTypes.size() - 1));
    int arrayIdx = 0;
    for (int idx = argumentTypes.size() - 1; idx < params.size(); idx++) {
        ArrayIndex arrayIndex = new ArrayIndex(testCase.getReference(), arrayReference, arrayIdx);
        Object param = params.get(idx);
        VariableReference paramRef = null;
        if ((param instanceof MethodInvocation) || (param instanceof ClassInstanceCreation)) {
            assert !nestedCallResults.isEmpty();
            paramRef = nestedCallResults.pop();
        } else {
            paramRef = retrieveVariableReference(param, lastDeclaredParamType.getComponentType());
        }
        paramRef.setOriginalCode(param.toString());
        AssignmentStatement assignment = new AssignmentStatement(testCase.getReference(), arrayIndex, paramRef);
        testCase.addStatement(assignment);
        arrayIdx++;
    }
    return result;
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) ArrayReference(org.evosuite.testcase.ArrayReference) VariableReference(org.evosuite.testcase.VariableReference) ArrayList(java.util.ArrayList) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) ArrayIndex(org.evosuite.testcase.ArrayIndex) AssignmentStatement(org.evosuite.testcase.AssignmentStatement) ArrayStatement(org.evosuite.testcase.ArrayStatement)

Example 14 with VariableReference

use of org.evosuite.testcase.VariableReference in project evosuite by EvoSuite.

the class TestExtractingVisitor method endVisit.

/**
 * {@inheritDoc}
 */
@SuppressWarnings("unchecked")
@Override
public void endVisit(MethodInvocation methodInvocation) {
    // TODO If in constructor, treat calls to this() and super().
    String methodName = methodInvocation.getName().toString();
    if (methodName.equals("fail") || methodName.startsWith("assert")) {
        logger.warn("We are ignoring fail and assert statements for now.");
        for (Expression expression : (List<Expression>) methodInvocation.arguments()) {
            if ((expression instanceof MethodInvocation) || (expression instanceof ClassInstanceCreation)) {
                assert !nestedCallResults.isEmpty();
                nestedCallResults.pop();
            }
        }
        return;
    }
    IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
    if (methodBinding == null) {
        throw new RuntimeException("JDT was unable to resolve method for " + methodInvocation);
    }
    List<?> paramTypes = Arrays.asList(methodBinding.getParameterTypes());
    List<VariableReference> params = convertParams(methodInvocation.arguments(), paramTypes);
    Method method = retrieveMethod(methodInvocation, methodBinding, params);
    /*
		Class<?> declaringClass = method.getDeclaringClass();
		if (false) {
			// TODO Methods can be declared in an order such that the called
			// method is not yet read
			if (testCase.getClassName().equals(declaringClass.getName())
			        || testCase.isDescendantOf(declaringClass)) {
				MethodDef methodDef = testCase.getMethod(method.getName());
				VariableReference retVal = retrieveResultReference(methodInvocation);
				retVal.setOriginalCode(methodInvocation.toString());
				testCase.convertMethod(methodDef, params, retVal);
				return;
			}
		}
		*/
    VariableReference callee = null;
    if (!Modifier.isStatic(method.getModifiers())) {
        callee = retrieveCalleeReference(methodInvocation);
    }
    MethodStatement methodStatement = null;
    ASTNode parent = methodInvocation.getParent();
    if (parent instanceof ExpressionStatement) {
        VariableReference retVal = new ValidVariableReference(testCase.getReference(), method.getReturnType());
        retVal.setOriginalCode(methodInvocation.toString());
        methodStatement = new ValidMethodStatement(testCase.getReference(), method, callee, retVal, params);
        testCase.addStatement(methodStatement);
        return;
    }
    VariableReference retVal = retrieveResultReference(methodInvocation);
    retVal.setOriginalCode(methodInvocation.toString());
    methodStatement = new ValidMethodStatement(testCase.getReference(), method, callee, retVal, params);
    if (!(parent instanceof Block)) {
        nestedCallResults.push(retVal);
    }
    testCase.addStatement(methodStatement);
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) MethodStatement(org.evosuite.testcase.MethodStatement) VariableReference(org.evosuite.testcase.VariableReference) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) GenericMethod(org.evosuite.utils.GenericMethod) Method(java.lang.reflect.Method) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) PrimitiveExpression(org.evosuite.testcase.PrimitiveExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block) ArrayList(java.util.ArrayList) AbstractList(java.util.AbstractList) List(java.util.List)

Example 15 with VariableReference

use of org.evosuite.testcase.VariableReference in project evosuite by EvoSuite.

the class TestExtractingVisitor method retrieveResultReference.

private VariableReference retrieveResultReference(MethodInvocation methodInvocation) {
    VariableReference result = calleeResultMap.get(methodInvocation.toString());
    if (result != null) {
        return result;
    }
    ASTNode parent = methodInvocation.getParent();
    if (parent instanceof VariableDeclarationFragment) {
        return retrieveVariableReference(parent, null);
    }
    if (parent instanceof Assignment) {
        Assignment assignment = (Assignment) parent;
        return retrieveVariableReference(assignment.getLeftHandSide(), null);
    }
    IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
    ITypeBinding returnType = methodBinding.getReturnType();
    Class<?> resultClass = null;
    try {
        resultClass = retrieveTypeClass(returnType);
    } catch (Exception exc) {
        String localClass = methodBinding.getDeclaringClass().getQualifiedName() + "." + returnType.getName();
        resultClass = loadClass(localClass);
    }
    result = retrieveVariableReference(returnType, resultClass);
    calleeResultMap.put(methodInvocation.toString(), result);
    return result;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) VariableReference(org.evosuite.testcase.VariableReference) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

VariableReference (org.evosuite.testcase.VariableReference)27 PrimitiveExpression (org.evosuite.testcase.PrimitiveExpression)7 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)6 SuperMethodInvocation (org.eclipse.jdt.core.dom.SuperMethodInvocation)6 AssignmentStatement (org.evosuite.testcase.AssignmentStatement)6 ArrayList (java.util.ArrayList)5 CastExpression (org.eclipse.jdt.core.dom.CastExpression)5 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)5 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)5 Expression (org.eclipse.jdt.core.dom.Expression)5 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)5 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)5 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)5 ArrayIndex (org.evosuite.testcase.ArrayIndex)4 ArrayReference (org.evosuite.testcase.ArrayReference)4 GenericMethod (org.evosuite.utils.GenericMethod)4 ASTNode (org.eclipse.jdt.core.dom.ASTNode)3 Assignment (org.eclipse.jdt.core.dom.Assignment)3 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)3 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)3