Search in sources :

Example 21 with VariableReference

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

the class TestCaseCodeGenerator method createFieldReadAccessStmt.

private void createFieldReadAccessStmt(final int logRecNo, final TestCase testCase) {
    // assumption: all necessary statements are created and there is one variable for reach referenced object
    final int oid = this.log.objectIds.get(logRecNo);
    final int captureId = this.log.captureIds.get(logRecNo);
    final Object returnValue = this.log.returnValues.get(logRecNo);
    if (// TODO necessary?
    !CaptureLog.RETURN_TYPE_VOID.equals(returnValue)) {
        Integer returnValueOID = (Integer) returnValue;
        final String descriptor = this.log.descList.get(logRecNo);
        final org.objectweb.asm.Type fieldTypeType = org.objectweb.asm.Type.getType(descriptor);
        final String typeName = this.log.oidClassNames.get(this.log.oidRecMapping.get(oid));
        final String fieldName = this.log.namesOfAccessedFields.get(captureId);
        try {
            // Class.forName(fieldTypeName, true, StaticTestCluster.classLoader);
            final Class<?> fieldType = getClassFromType(fieldTypeType);
            // Class.forName(typeName, true, StaticTestCluster.classLoader);
            final Class<?> type = getClassForName(typeName);
            final FieldReference valueRef = new FieldReference(testCase, new GenericField(type.getField(fieldName), type));
            final VariableReference targetVar = new VariableReferenceImpl(testCase, fieldType);
            final AssignmentStatement assignment = new AssignmentStatement(testCase, targetVar, valueRef);
            VariableReference varRef = testCase.addStatement(assignment);
            this.oidToVarRefMap.put(returnValueOID, varRef);
        } catch (final Exception e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : FieldReference(org.evosuite.testcase.FieldReference) VariableReference(org.evosuite.testcase.VariableReference) AssignmentStatement(org.evosuite.testcase.AssignmentStatement) VariableReferenceImpl(org.evosuite.testcase.VariableReferenceImpl) GenericField(org.evosuite.utils.GenericField)

Example 22 with VariableReference

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

the class CompoundTestCase method getVariableReferenceInternally.

private VariableReference getVariableReferenceInternally(IVariableBinding varBinding) {
    VariableReference varRef = currentMethodVars.get(varBinding.toString());
    if (varRef != null) {
        return varRef;
    }
    varRef = fieldVars.get(varBinding.toString());
    if (varRef != null) {
        return varRef;
    }
    if (parent != null) {
        return parent.getVariableReferenceInternally(varBinding);
    }
    return null;
}
Also used : VariableReference(org.evosuite.testcase.VariableReference)

Example 23 with VariableReference

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

the class TestExtractingVisitor method endVisit.

/**
 * {@inheritDoc}
 */
@Override
public void endVisit(ClassInstanceCreation instanceCreation) {
    if (instanceCreation.getParent() instanceof ThrowStatement) {
        logger.warn("Ignoring throw statements!");
        return;
    }
    List<?> paramTypes = Arrays.asList(instanceCreation.resolveConstructorBinding().getParameterTypes());
    List<?> paramValues = instanceCreation.arguments();
    Constructor<?> constructor = retrieveConstructor(instanceCreation.getType(), paramTypes, paramValues);
    List<VariableReference> params = convertParams(instanceCreation.arguments(), paramTypes);
    VariableReference retVal = retrieveVariableReference(instanceCreation, null);
    retVal.setOriginalCode(instanceCreation.toString());
    ConstructorStatement statement = new ValidConstructorStatement(testCase.getReference(), constructor, retVal, params);
    testCase.addStatement(statement);
}
Also used : ConstructorStatement(org.evosuite.testcase.ConstructorStatement) VariableReference(org.evosuite.testcase.VariableReference) ThrowStatement(org.eclipse.jdt.core.dom.ThrowStatement)

Example 24 with VariableReference

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

the class TestExtractingVisitor method endVisit.

/**
 * {@inheritDoc}
 */
@Override
public void endVisit(Assignment assignment) {
    if ((assignment.getRightHandSide() instanceof MethodInvocation) || (assignment.getRightHandSide() instanceof ClassInstanceCreation)) {
        // treated in respective endVisit methods
        return;
    }
    VariableReference varRef = retrieveVariableReference(assignment.getLeftHandSide(), null);
    varRef.setOriginalCode(assignment.getLeftHandSide().toString());
    VariableReference newAssignment = retrieveVariableReference(assignment.getRightHandSide(), null);
    newAssignment.setOriginalCode(assignment.getRightHandSide().toString());
    if (varRef instanceof ArrayIndex) {
        AssignmentStatement assignmentStatement = new AssignmentStatement(testCase.getReference(), varRef, newAssignment);
        testCase.addStatement(assignmentStatement);
        return;
    }
    testCase.variableAssignment(varRef, newAssignment);
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) VariableReference(org.evosuite.testcase.VariableReference) AssignmentStatement(org.evosuite.testcase.AssignmentStatement) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) ArrayIndex(org.evosuite.testcase.ArrayIndex)

Example 25 with VariableReference

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

the class TestExtractingVisitor method visit.

/**
 * {@inheritDoc}
 */
@Override
public boolean visit(FieldDeclaration fieldDeclaration) {
    if (Modifier.isStatic(fieldDeclaration.getModifiers())) {
        testCase.setCurrentScope(TestScope.STATICFIELDS);
    }
    VariableDeclarationFragment varDeclFrgmnt = (VariableDeclarationFragment) fieldDeclaration.fragments().get(0);
    Expression expression = varDeclFrgmnt.getInitializer();
    VariableReference varRef;
    if (expression == null) {
        varRef = retrieveDefaultValueAssignment(retrieveTypeClass(varDeclFrgmnt));
    } else {
        varRef = retrieveVariableReference(expression, null);
    }
    varRef.setOriginalCode(fieldDeclaration.toString());
    // TODO Use the name here as well?
    // String name = varDeclFrgmt.getName().getIdentifier();
    // new BoundVariableReferenceImpl(testCase, varType, name);
    testCase.addVariable(varDeclFrgmnt.resolveBinding(), varRef);
    testCase.setCurrentScope(TestScope.FIELDS);
    return true;
}
Also used : 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) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment)

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