Search in sources :

Example 6 with VariableReference

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

the class TestExtractingVisitor method retrieveVariableReference.

private VariableReference retrieveVariableReference(ArrayAccess arrayAccess) {
    Expression expr = arrayAccess.getArray();
    List<Integer> indices = new ArrayList<Integer>();
    // TODO This is a shortcut
    // we need a variable reference for the index value
    indices.add(getArrayIndex(arrayAccess.getIndex()));
    while (expr instanceof ArrayAccess) {
        ArrayAccess current = (ArrayAccess) expr;
        expr = current.getArray();
        indices.add(getArrayIndex(current.getIndex()));
    }
    Collections.reverse(indices);
    VariableReference varRef = retrieveVariableReference(expr, null);
    ArrayReference arrayReference = (ArrayReference) varRef;
    assert indices.size() == arrayReference.getArrayDimensions();
    ArrayIndex arrayIndex = new ArrayIndex(testCase.getReference(), arrayReference, indices);
    return arrayIndex;
}
Also used : ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess) ArrayReference(org.evosuite.testcase.ArrayReference) VariableReference(org.evosuite.testcase.VariableReference) 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) ArrayList(java.util.ArrayList) ArrayIndex(org.evosuite.testcase.ArrayIndex)

Example 7 with VariableReference

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

the class TestExtractingVisitor method retrieveVariableReference.

private VariableReference retrieveVariableReference(VariableDeclarationFragment varDeclFrgmnt) {
    IVariableBinding variableBinding = varDeclFrgmnt.resolveBinding();
    Class<?> clazz = retrieveTypeClass(variableBinding.getType());
    if (clazz.isArray()) {
        ArrayReference arrayReference = new ValidArrayReference(testCase.getReference(), clazz);
        arrayReference.setOriginalCode(varDeclFrgmnt.toString());
        testCase.addVariable(varDeclFrgmnt.resolveBinding(), arrayReference);
        return arrayReference;
    }
    VariableReference result = new BoundVariableReferenceImpl(testCase, clazz, variableBinding.getName());
    testCase.addVariable(variableBinding, result);
    return result;
}
Also used : ArrayReference(org.evosuite.testcase.ArrayReference) VariableReference(org.evosuite.testcase.VariableReference) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 8 with VariableReference

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

the class TestExtractingVisitor method retrieveVariableReference.

private VariableReference retrieveVariableReference(ClassInstanceCreation instanceCreation, Class<?> varType) {
    if ((instanceCreation.getParent() instanceof MethodInvocation) || (instanceCreation.getParent() instanceof ClassInstanceCreation)) {
        VariableReference result = new ValidVariableReference(testCase.getReference(), retrieveTypeClass(instanceCreation.getType()));
        nestedCallResults.push(result);
        return result;
    }
    if ((instanceCreation.getParent() instanceof ExpressionStatement) && (instanceCreation.getParent().getParent() instanceof Block)) {
        if (varType == null) {
            varType = retrieveTypeClass(instanceCreation);
        }
        VariableReference varRef = new ValidVariableReference(testCase.getReference(), varType);
        return varRef;
    }
    return retrieveVariableReference(instanceCreation.getParent(), varType);
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) VariableReference(org.evosuite.testcase.VariableReference) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) Block(org.eclipse.jdt.core.dom.Block) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation)

Example 9 with VariableReference

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

the class TestExtractingVisitor method retrieveVariableReference.

/**
 * <p>
 * retrieveVariableReference
 * </p>
 *
 * @param argument
 *            a {@link java.lang.Object} object.
 * @param varType
 *            a {@link java.lang.Class} object.
 * @return a {@link org.evosuite.testcase.VariableReference} object.
 */
protected VariableReference retrieveVariableReference(Object argument, Class<?> varType) {
    if (argument instanceof ClassInstanceCreation) {
        return retrieveVariableReference((ClassInstanceCreation) argument, varType);
    }
    if (argument instanceof VariableDeclarationFragment) {
        return retrieveVariableReference((VariableDeclarationFragment) argument);
    }
    if (argument instanceof SimpleName) {
        SimpleName simpleName = (SimpleName) argument;
        lineNumber = testReader.getLineNumber(simpleName.getStartPosition());
        return retrieveVariableReference(simpleName.resolveBinding(), varType);
    }
    if (argument instanceof IVariableBinding) {
        return retrieveVariableReference((IVariableBinding) argument, varType);
    }
    if (argument instanceof PrefixExpression) {
        return retrieveVariableReference((PrefixExpression) argument);
    }
    if (argument instanceof InfixExpression) {
        return retrieveVariableReference((InfixExpression) argument, varType);
    }
    if (argument instanceof ExpressionStatement) {
        ExpressionStatement exprStmt = (ExpressionStatement) argument;
        Expression expression = exprStmt.getExpression();
        return retrieveVariableReference(expression, varType);
    }
    if (argument instanceof NullLiteral) {
        return retrieveVariableReference((NullLiteral) argument, varType);
    }
    if (argument instanceof StringLiteral) {
        return retrieveVariableReference((StringLiteral) argument);
    }
    if (argument instanceof NumberLiteral) {
        return retrieveVariableReference((NumberLiteral) argument);
    }
    if (argument instanceof CharacterLiteral) {
        return retrieveVariableReference((CharacterLiteral) argument);
    }
    if (argument instanceof BooleanLiteral) {
        return retrieveVariableReference((BooleanLiteral) argument);
    }
    if (argument instanceof ITypeBinding) {
        if (varType != null) {
            return new ValidVariableReference(testCase.getReference(), varType);
        }
        return new ValidVariableReference(testCase.getReference(), retrieveTypeClass(argument));
    }
    if (argument instanceof QualifiedName) {
        return retrieveVariableReference((QualifiedName) argument);
    }
    if (argument instanceof MethodInvocation) {
        MethodInvocation methodInvocation = (MethodInvocation) argument;
        VariableReference result = retrieveResultReference(methodInvocation);
        nestedCallResults.push(result);
        return result;
    }
    if (argument instanceof ArrayCreation) {
        return retrieveVariableReference((ArrayCreation) argument);
    }
    if (argument instanceof VariableDeclaration) {
        return retrieveVariableReference((VariableDeclaration) argument);
    }
    if (argument instanceof ArrayAccess) {
        // argument).getArray(), null);
        return retrieveVariableReference((ArrayAccess) argument);
    }
    if (argument instanceof Assignment) {
        return retrieveVariableReference(((Assignment) argument).getLeftHandSide(), null);
    }
    if (argument instanceof CastExpression) {
        CastExpression castExpression = (CastExpression) argument;
        VariableReference result = retrieveVariableReference(castExpression.getExpression(), null);
        Class<?> castClass = retrieveTypeClass(castExpression.resolveTypeBinding());
        assert castClass.isAssignableFrom(toClass(result.getType()));
        result.setType(castClass);
        return result;
    }
    throw new UnsupportedOperationException("Argument type " + argument.getClass() + " not implemented!");
}
Also used : BooleanLiteral(org.eclipse.jdt.core.dom.BooleanLiteral) SimpleName(org.eclipse.jdt.core.dom.SimpleName) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) Assignment(org.eclipse.jdt.core.dom.Assignment) ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) VariableReference(org.evosuite.testcase.VariableReference) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) 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) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ArrayCreation(org.eclipse.jdt.core.dom.ArrayCreation) CastExpression(org.eclipse.jdt.core.dom.CastExpression) NullLiteral(org.eclipse.jdt.core.dom.NullLiteral) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral) CharacterLiteral(org.eclipse.jdt.core.dom.CharacterLiteral)

Example 10 with VariableReference

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

the class TestExtractingVisitor method endVisit.

/**
 * {@inheritDoc}
 */
@Override
public void endVisit(ReturnStatement returnStatement) {
    VariableReference returnValue = null;
    if (returnStatement.getExpression() == null) {
        logger.warn("Since we do not represent control structures, we ignore explicit empty return statements ('return;').");
        return;
    }
    if (returnStatement.getExpression() instanceof MethodInvocation) {
        returnValue = testCase.getLastStatement().getReturnValue();
    } else {
        returnValue = retrieveVariableReference(returnStatement.getExpression(), null);
    }
    returnValue.setOriginalCode(returnStatement.toString());
    ReturnStatementPlaceholder returnStmt = new ReturnStatementPlaceholder(testCase.getReference(), returnValue);
    testCase.addStatement(returnStmt);
}
Also used : ReturnStatementPlaceholder(org.evosuite.junit.CompoundTestCase.ReturnStatementPlaceholder) VariableReference(org.evosuite.testcase.VariableReference) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation)

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