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