use of org.eclipse.jdt.core.dom.CharacterLiteral in project evosuite by EvoSuite.
the class JUnitCodeGenerator method createPlainInitStmt.
@SuppressWarnings("unchecked")
@Override
public void createPlainInitStmt(CaptureLog log, int logRecNo) {
PostProcessor.notifyRecentlyProcessedLogRecNo(logRecNo);
// NOTE: PLAIN INIT: has always one non-null param
// TODO: use primitives
final int oid = log.objectIds.get(logRecNo);
if (this.oidToVarMapping.containsKey(oid)) {
// TODO this might happen because of Integer.valueOf o.รค. . Is this approach ok?
return;
}
final String type = log.oidClassNames.get(log.oidRecMapping.get(oid));
final Object value = log.params.get(logRecNo)[0];
final VariableDeclarationFragment vd = ast.newVariableDeclarationFragment();
vd.setName(ast.newSimpleName(this.createNewVarName(oid, value.getClass().getName())));
final VariableDeclarationStatement stmt = ast.newVariableDeclarationStatement(vd);
if (value instanceof Class) {
stmt.setType(ast.newSimpleType(ast.newSimpleName("Class")));
} else {
stmt.setType(this.createAstType(type, ast));
}
if (value instanceof Number) {
if (value instanceof Long) {
vd.setInitializer(ast.newNumberLiteral(String.valueOf(value) + 'l'));
} else if (value instanceof Double) {
vd.setInitializer(ast.newNumberLiteral(String.valueOf(value) + 'd'));
} else {
vd.setInitializer(ast.newNumberLiteral(String.valueOf(value)));
}
} else if (value instanceof String) {
final StringLiteral literal = ast.newStringLiteral();
literal.setLiteralValue((String) value);
vd.setInitializer(literal);
} else if (value instanceof Character) {
final CharacterLiteral literal = ast.newCharacterLiteral();
literal.setCharValue((Character) value);
vd.setInitializer(literal);
} else if (value instanceof Boolean) {
final BooleanLiteral literal = ast.newBooleanLiteral((Boolean) value);
vd.setInitializer(literal);
} else if (value instanceof Class) {
final TypeLiteral clazz = ast.newTypeLiteral();
clazz.setType(ast.newSimpleType(ast.newName(((Class<?>) value).getName())));
vd.setInitializer(clazz);
} else {
throw new IllegalStateException("An error occurred while creating a plain statement: unsupported type: " + value.getClass().getName());
}
methodBlock.statements().add(stmt);
}
Aggregations