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;
}
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;
}
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);
}
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!");
}
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);
}
Aggregations