use of org.eclipse.jdt.core.dom.Assignment 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.eclipse.jdt.core.dom.Assignment 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;
}
use of org.eclipse.jdt.core.dom.Assignment in project evosuite by EvoSuite.
the class CodeGenerator method createFieldWriteAccessStmt.
@SuppressWarnings("unchecked")
private void createFieldWriteAccessStmt(final String packageName, final int logRecNo, final Block methodBlock, final AST ast) {
// assumption: all necessary statements are created and there is one variable for reach referenced object
final Object[] methodArgs = this.log.params.get(logRecNo);
final String methodName = this.log.methodNames.get(logRecNo);
final int oid = this.log.objectIds.get(logRecNo);
final int captureId = this.log.captureIds.get(logRecNo);
final String fieldName = this.log.namesOfAccessedFields.get(captureId);
final String typeName = this.log.oidClassNames.get(this.log.oidRecMapping.get(oid));
Class<?> type = getClassForName(typeName);
// try {
// type = Class.forName(typeName);
// } catch (ClassNotFoundException e) {
// throw new RuntimeException(e);
// }
final int fieldTypeModifiers = this.getFieldModifiers(type, fieldName);
final boolean isPublic = java.lang.reflect.Modifier.isPublic(fieldTypeModifiers);
// TODO might be nicer...
final boolean haveSamePackage = type.getPackage().getName().equals(packageName);
final boolean isReflectionAccessNeeded = !isPublic && !haveSamePackage;
if (isReflectionAccessNeeded) {
this.isSetFieldMethodNeeded = true;
final String varName = this.oidToVarMapping.get(oid);
final MethodInvocation setFieldCall = ast.newMethodInvocation();
setFieldCall.setName(ast.newSimpleName("setField"));
StringLiteral stringLiteral = ast.newStringLiteral();
stringLiteral.setLiteralValue(typeName);
// class name
setFieldCall.arguments().add(stringLiteral);
stringLiteral = ast.newStringLiteral();
stringLiteral.setLiteralValue(fieldName);
// field name
setFieldCall.arguments().add(stringLiteral);
// receiver
setFieldCall.arguments().add(ast.newSimpleName(varName));
final Integer arg = (Integer) methodArgs[0];
if (arg == null) {
// value
setFieldCall.arguments().add(ast.newNullLiteral());
} else {
// value
setFieldCall.arguments().add(ast.newSimpleName(this.oidToVarMapping.get(arg)));
}
methodBlock.statements().add(ast.newExpressionStatement(setFieldCall));
} else {
FieldAccess fa = ast.newFieldAccess();
if (CaptureLog.PUTSTATIC.equals(methodName)) {
// final String type = this.log.oidClassNames.get(this.log.oidRecMapping.get(oid));
// .split("\\.")));
fa.setExpression(ast.newName(typeName));
} else {
final String varName = this.oidToVarMapping.get(oid);
fa.setExpression(ast.newSimpleName(varName));
}
fa.setName(ast.newSimpleName(fieldName));
final Assignment assignment = ast.newAssignment();
assignment.setLeftHandSide(fa);
final Integer arg = (Integer) methodArgs[0];
if (arg == null) {
assignment.setRightHandSide(ast.newNullLiteral());
} else {
final Class<?> argType = this.oidToTypeMapping.get(arg);
final String fieldDesc = this.log.descList.get(logRecNo);
final Class<?> fieldType = CaptureUtil.getClassFromDesc(fieldDesc);
if (fieldType.isAssignableFrom(argType)) {
assignment.setRightHandSide(ast.newSimpleName(this.oidToVarMapping.get(arg)));
} else {
// we need an up-cast
final CastExpression cast = ast.newCastExpression();
cast.setType(ast.newSimpleType(ast.newName(fieldType.getName())));
cast.setExpression(ast.newSimpleName(this.oidToVarMapping.get(arg)));
assignment.setRightHandSide(cast);
}
}
methodBlock.statements().add(ast.newExpressionStatement(assignment));
}
}
use of org.eclipse.jdt.core.dom.Assignment in project evosuite by EvoSuite.
the class CodeGenerator method createMethodCallStmt.
@SuppressWarnings("unchecked")
private void createMethodCallStmt(final String packageName, final int logRecNo, final boolean postprocessing, final Block methodBlock, final AST ast) {
// assumption: all necessary statements are created and there is one variable for reach referenced object
final int oid = this.log.objectIds.get(logRecNo);
final Object[] methodArgs = this.log.params.get(logRecNo);
final String methodName = this.log.methodNames.get(logRecNo);
final String methodDesc = this.log.descList.get(logRecNo);
final org.objectweb.asm.Type[] methodParamTypes = org.objectweb.asm.Type.getArgumentTypes(methodDesc);
final Class<?>[] methodParamTypeClasses = new Class[methodParamTypes.length];
for (int i = 0; i < methodParamTypes.length; i++) {
methodParamTypeClasses[i] = getClassForName(methodParamTypes[i].getClassName());
}
final String typeName = this.log.oidClassNames.get(this.log.oidRecMapping.get(oid));
Class<?> type = getClassForName(typeName);
// Class<?> type;
// try {
// type = Class.forName(typeName);
// } catch (ClassNotFoundException e) {
// throw new RuntimeException(e);
// }
// TODO might be nicer...
final boolean haveSamePackage = type.getPackage().getName().equals(packageName);
final Statement finalStmt;
@SuppressWarnings("rawtypes") final List arguments;
if (CaptureLog.OBSERVED_INIT.equals(methodName)) {
/*
* Person var0 = null;
* {
* var0 = new Person();
* }
* catch(Throwable t)
* {
* org.uni.saarland.sw.prototype.capture.PostProcessor.captureException(<logRecNo>);
* }
*/
// e.g. Person var0 = null;
final String varName = this.createNewVarName(oid, typeName);
VariableDeclarationFragment vd = ast.newVariableDeclarationFragment();
vd.setName(ast.newSimpleName(varName));
VariableDeclarationStatement stmt = ast.newVariableDeclarationStatement(vd);
stmt.setType(this.createAstType(typeName, ast));
vd.setInitializer(ast.newNullLiteral());
methodBlock.statements().add(stmt);
try {
this.getConstructorModifiers(type, methodParamTypeClasses);
} catch (Exception e) {
e.printStackTrace();
}
final int constructorTypeModifiers = this.getConstructorModifiers(type, methodParamTypeClasses);
final boolean isPublic = java.lang.reflect.Modifier.isPublic(constructorTypeModifiers);
final boolean isReflectionAccessNeeded = !isPublic && !haveSamePackage;
if (isReflectionAccessNeeded) {
this.isNewInstanceMethodNeeded = true;
final MethodInvocation mi = this.createCallMethodOrNewInstanceCallStmt(true, ast, varName, typeName, methodName, methodArgs, methodParamTypes);
arguments = null;
final Assignment assignment = ast.newAssignment();
assignment.setLeftHandSide(ast.newSimpleName(varName));
assignment.setOperator(Operator.ASSIGN);
final CastExpression cast = ast.newCastExpression();
cast.setType(this.createAstType(typeName, ast));
cast.setExpression(mi);
assignment.setRightHandSide(cast);
finalStmt = ast.newExpressionStatement(assignment);
} else {
// e.g. var0 = new Person();
final ClassInstanceCreation ci = ast.newClassInstanceCreation();
ci.setType(this.createAstType(typeName, ast));
final Assignment assignment = ast.newAssignment();
assignment.setLeftHandSide(ast.newSimpleName(varName));
assignment.setOperator(Operator.ASSIGN);
assignment.setRightHandSide(ci);
finalStmt = ast.newExpressionStatement(assignment);
arguments = ci.arguments();
}
} else // ------------------ handling for ordinary method calls e.g. var1 = var0.doSth();
{
String returnVarName = null;
final String desc = this.log.descList.get(logRecNo);
final String returnType = org.objectweb.asm.Type.getReturnType(desc).getClassName();
final Object returnValue = this.log.returnValues.get(logRecNo);
if (!CaptureLog.RETURN_TYPE_VOID.equals(returnValue)) {
Integer returnValueOID = (Integer) returnValue;
// e.g. Person var0 = null;
returnVarName = this.createNewVarName(returnValueOID, returnType);
VariableDeclarationFragment vd = ast.newVariableDeclarationFragment();
vd.setName(ast.newSimpleName(returnVarName));
VariableDeclarationStatement stmt = ast.newVariableDeclarationStatement(vd);
stmt.setType(this.createAstType(returnType, ast));
vd.setInitializer(ast.newNullLiteral());
methodBlock.statements().add(stmt);
}
final String varName = this.oidToVarMapping.get(oid);
final int methodTypeModifiers = this.getMethodModifiers(type, methodName, methodParamTypeClasses);
final boolean isPublic = java.lang.reflect.Modifier.isPublic(methodTypeModifiers);
final boolean isReflectionAccessNeeded = !isPublic && !haveSamePackage;
// e.g. Person var0 = var1.getPerson("Ben");
final MethodInvocation mi;
if (isReflectionAccessNeeded) {
this.isCallMethodMethodNeeded = true;
mi = this.createCallMethodOrNewInstanceCallStmt(false, ast, varName, typeName, methodName, methodArgs, methodParamTypes);
arguments = null;
if (returnVarName != null) {
final Assignment assignment = ast.newAssignment();
assignment.setLeftHandSide(ast.newSimpleName(returnVarName));
assignment.setOperator(Operator.ASSIGN);
final CastExpression cast = ast.newCastExpression();
cast.setType(this.createAstType(returnType, ast));
cast.setExpression(mi);
assignment.setRightHandSide(cast);
finalStmt = ast.newExpressionStatement(assignment);
} else {
finalStmt = ast.newExpressionStatement(mi);
}
} else {
mi = ast.newMethodInvocation();
if (this.log.isStaticCallList.get(logRecNo)) {
// can only happen, if this is a static method call (because constructor statement has been reported)
final String tmpType = this.log.oidClassNames.get(this.log.oidRecMapping.get(oid));
mi.setExpression(ast.newName(tmpType.split("\\.")));
} else {
mi.setExpression(ast.newSimpleName(varName));
}
mi.setName(ast.newSimpleName(methodName));
arguments = mi.arguments();
if (returnVarName != null) {
final Assignment assignment = ast.newAssignment();
assignment.setLeftHandSide(ast.newSimpleName(returnVarName));
assignment.setOperator(Operator.ASSIGN);
assignment.setRightHandSide(mi);
finalStmt = ast.newExpressionStatement(assignment);
} else {
finalStmt = ast.newExpressionStatement(mi);
}
}
}
if (postprocessing) {
final TryStatement tryStmt = this.createTryStatementForPostProcessing(ast, finalStmt, logRecNo);
methodBlock.statements().add(tryStmt);
} else {
if (this.failedRecords.contains(logRecNo)) {
// we just need an empty catch block to preserve program flow
final TryStatement tryStmt = this.createTryStmtWithEmptyCatch(ast, finalStmt);
methodBlock.statements().add(tryStmt);
} else {
methodBlock.statements().add(finalStmt);
}
}
if (arguments != null) {
// final String methodDesc = this.log.descList.get(logRecNo);
// final org.objectweb.asm.Type[] methodParamTypes = org.objectweb.asm.Type.getArgumentTypes(methodDesc);
Class<?> methodParamType;
Class<?> argType;
// is either an oid or null
Integer arg;
for (int i = 0; i < methodArgs.length; i++) {
arg = (Integer) methodArgs[i];
if (arg == null) {
arguments.add(ast.newNullLiteral());
} else {
methodParamType = CaptureUtil.getClassFromDesc(methodParamTypes[i].getDescriptor());
argType = this.oidToTypeMapping.get(arg);
if (methodParamType.isAssignableFrom(argType)) {
arguments.add(ast.newSimpleName(this.oidToVarMapping.get(arg)));
} else {
// we need an up-cast
final CastExpression cast = ast.newCastExpression();
cast.setType(ast.newSimpleType(ast.newName(methodParamType.getName())));
cast.setExpression(ast.newSimpleName(this.oidToVarMapping.get(arg)));
arguments.add(cast);
}
}
}
}
}
use of org.eclipse.jdt.core.dom.Assignment in project evosuite by EvoSuite.
the class JUnitCodeGenerator method createArrayInitStmt.
@Override
public void createArrayInitStmt(final CaptureLog log, final int logRecNo) {
final int oid = log.objectIds.get(logRecNo);
final Object[] params = log.params.get(logRecNo);
final String arrTypeName = log.oidClassNames.get(log.oidRecMapping.get(oid));
final Class<?> arrType = getClassForName(arrTypeName);
// --- create array instance creation e.g. int[] var = new int[10];
final ArrayType arrAstType = (ArrayType) createAstArrayType(arrTypeName, ast);
final ArrayCreation arrCreationExpr = ast.newArrayCreation();
arrCreationExpr.setType(arrAstType);
arrCreationExpr.dimensions().add(ast.newNumberLiteral(String.valueOf(params.length)));
final String arrVarName = this.createNewVarName(oid, arrTypeName);
final VariableDeclarationFragment vd = ast.newVariableDeclarationFragment();
final SimpleName arrVarNameExpr = ast.newSimpleName(arrVarName);
vd.setName(arrVarNameExpr);
vd.setInitializer(arrCreationExpr);
final VariableDeclarationStatement varDeclStmt = ast.newVariableDeclarationStatement(vd);
varDeclStmt.setType(this.createAstType(arrTypeName, ast));
methodBlock.statements().add(varDeclStmt);
// create array access statements var[0] = var1;
Integer paramOID;
Assignment assign;
ArrayAccess arrAccessExpr;
for (int i = 0; i < params.length; i++) {
assign = ast.newAssignment();
arrAccessExpr = ast.newArrayAccess();
arrAccessExpr.setIndex(ast.newNumberLiteral(String.valueOf(i)));
arrAccessExpr.setArray(arrVarNameExpr);
assign.setLeftHandSide(arrAccessExpr);
paramOID = (Integer) params[i];
if (paramOID == null) {
assign.setRightHandSide(ast.newNullLiteral());
} else {
assign.setRightHandSide(ast.newSimpleName(this.oidToVarMapping.get(paramOID)));
}
methodBlock.statements().add(assign);
}
}
Aggregations