use of org.eclipse.jdt.core.dom.TypeLiteral in project evosuite by EvoSuite.
the class CodeGenerator method createCallMethodOrNewInstanceCallStmt.
@SuppressWarnings("unchecked")
private MethodInvocation createCallMethodOrNewInstanceCallStmt(final boolean isConstructorCall, final AST ast, final String varName, final String targetTypeName, final String methodName, final Object[] methodArgs, final org.objectweb.asm.Type[] paramTypes) {
// -- construct getField() call
final MethodInvocation callMethodCall = ast.newMethodInvocation();
if (isConstructorCall) {
callMethodCall.setName(ast.newSimpleName("newInstance"));
} else {
callMethodCall.setName(ast.newSimpleName("callMethod"));
}
StringLiteral stringLiteral = ast.newStringLiteral();
stringLiteral.setLiteralValue(targetTypeName);
// class name
callMethodCall.arguments().add(stringLiteral);
if (!isConstructorCall) {
stringLiteral = ast.newStringLiteral();
stringLiteral.setLiteralValue(methodName);
// method name
callMethodCall.arguments().add(stringLiteral);
if (varName == null) {
// static call -> no receiver
callMethodCall.arguments().add(ast.newNullLiteral());
} else {
// receiver
callMethodCall.arguments().add(ast.newSimpleName(varName));
}
}
// method arguments
ArrayCreation arrCreation = ast.newArrayCreation();
arrCreation.setType(ast.newArrayType(ast.newSimpleType(ast.newSimpleName("Object"))));
ArrayInitializer arrInit = ast.newArrayInitializer();
arrCreation.setInitializer(arrInit);
callMethodCall.arguments().add(arrCreation);
// is either an oid or null
Integer arg;
for (int i = 0; i < methodArgs.length; i++) {
arg = (Integer) methodArgs[i];
if (arg == null) {
arrInit.expressions().add(ast.newNullLiteral());
} else {
arrInit.expressions().add(ast.newSimpleName(this.oidToVarMapping.get(arg)));
}
}
// paramTypes
arrCreation = ast.newArrayCreation();
arrCreation.setType(ast.newArrayType(ast.newSimpleType(ast.newSimpleName("Class"))));
arrInit = ast.newArrayInitializer();
arrCreation.setInitializer(arrInit);
callMethodCall.arguments().add(arrCreation);
org.objectweb.asm.Type type;
for (int i = 0; i < paramTypes.length; i++) {
type = paramTypes[i];
if (type.equals(org.objectweb.asm.Type.BOOLEAN_TYPE)) {
FieldAccess facc = ast.newFieldAccess();
facc.setName(ast.newSimpleName("TYPE"));
facc.setExpression(ast.newSimpleName("Boolean"));
arrInit.expressions().add(facc);
} else if (type.equals(org.objectweb.asm.Type.BYTE_TYPE)) {
FieldAccess facc = ast.newFieldAccess();
facc.setName(ast.newSimpleName("TYPE"));
facc.setExpression(ast.newSimpleName("Byte"));
arrInit.expressions().add(facc);
} else if (type.equals(org.objectweb.asm.Type.CHAR_TYPE)) {
FieldAccess facc = ast.newFieldAccess();
facc.setName(ast.newSimpleName("TYPE"));
facc.setExpression(ast.newSimpleName("Character"));
arrInit.expressions().add(facc);
} else if (type.equals(org.objectweb.asm.Type.DOUBLE_TYPE)) {
FieldAccess facc = ast.newFieldAccess();
facc.setName(ast.newSimpleName("TYPE"));
facc.setExpression(ast.newSimpleName("Double"));
arrInit.expressions().add(facc);
} else if (type.equals(org.objectweb.asm.Type.FLOAT_TYPE)) {
FieldAccess facc = ast.newFieldAccess();
facc.setName(ast.newSimpleName("TYPE"));
facc.setExpression(ast.newSimpleName("Float"));
arrInit.expressions().add(facc);
} else if (type.equals(org.objectweb.asm.Type.INT_TYPE)) {
FieldAccess facc = ast.newFieldAccess();
facc.setName(ast.newSimpleName("TYPE"));
facc.setExpression(ast.newSimpleName("Integer"));
arrInit.expressions().add(facc);
} else if (type.equals(org.objectweb.asm.Type.LONG_TYPE)) {
FieldAccess facc = ast.newFieldAccess();
facc.setName(ast.newSimpleName("TYPE"));
facc.setExpression(ast.newSimpleName("Long"));
arrInit.expressions().add(facc);
} else if (type.equals(org.objectweb.asm.Type.SHORT_TYPE)) {
FieldAccess facc = ast.newFieldAccess();
facc.setName(ast.newSimpleName("TYPE"));
facc.setExpression(ast.newSimpleName("Short"));
arrInit.expressions().add(facc);
} else {
final TypeLiteral clazz = ast.newTypeLiteral();
clazz.setType(ast.newSimpleType(ast.newName(type.getClassName())));
arrInit.expressions().add(clazz);
}
}
return callMethodCall;
}
use of org.eclipse.jdt.core.dom.TypeLiteral in project evosuite by EvoSuite.
the class CodeGenerator method createPlainInitStmt.
@SuppressWarnings("unchecked")
private void createPlainInitStmt(final int logRecNo, final Block methodBlock, final AST ast) {
// NOTE: PLAIN INIT: has always one non-null param
// TODO: use primitives
final int oid = this.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 = this.log.oidClassNames.get(this.log.oidRecMapping.get(oid));
final Object value = this.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);
}
use of org.eclipse.jdt.core.dom.TypeLiteral in project evosuite by EvoSuite.
the class JUnitCodeGenerator method createCallMethodOrNewInstanceCallStmt.
@SuppressWarnings("unchecked")
private MethodInvocation createCallMethodOrNewInstanceCallStmt(final boolean isConstructorCall, final AST ast, final String varName, final String targetTypeName, final String methodName, final Object[] methodArgs, final org.objectweb.asm.Type[] paramTypes) {
// -- construct getField() call
final MethodInvocation callMethodCall = ast.newMethodInvocation();
if (isConstructorCall) {
callMethodCall.setName(ast.newSimpleName("newInstance"));
} else {
callMethodCall.setName(ast.newSimpleName("callMethod"));
}
StringLiteral stringLiteral = ast.newStringLiteral();
stringLiteral.setLiteralValue(targetTypeName);
// class name
callMethodCall.arguments().add(stringLiteral);
if (!isConstructorCall) {
stringLiteral = ast.newStringLiteral();
stringLiteral.setLiteralValue(methodName);
// method name
callMethodCall.arguments().add(stringLiteral);
if (varName == null) {
// static call -> no receiver
callMethodCall.arguments().add(ast.newNullLiteral());
} else {
// receiver
callMethodCall.arguments().add(ast.newSimpleName(varName));
}
}
// method arguments
ArrayCreation arrCreation = ast.newArrayCreation();
arrCreation.setType(ast.newArrayType(ast.newSimpleType(ast.newSimpleName("Object"))));
ArrayInitializer arrInit = ast.newArrayInitializer();
arrCreation.setInitializer(arrInit);
callMethodCall.arguments().add(arrCreation);
// is either an oid or null
Integer arg;
for (int i = 0; i < methodArgs.length; i++) {
arg = (Integer) methodArgs[i];
if (arg == null) {
arrInit.expressions().add(ast.newNullLiteral());
} else {
arrInit.expressions().add(ast.newSimpleName(this.oidToVarMapping.get(arg)));
}
}
// paramTypes
arrCreation = ast.newArrayCreation();
arrCreation.setType(ast.newArrayType(ast.newSimpleType(ast.newSimpleName("Class"))));
arrInit = ast.newArrayInitializer();
arrCreation.setInitializer(arrInit);
callMethodCall.arguments().add(arrCreation);
org.objectweb.asm.Type type;
for (int i = 0; i < paramTypes.length; i++) {
type = paramTypes[i];
if (type.equals(org.objectweb.asm.Type.BOOLEAN_TYPE)) {
FieldAccess facc = ast.newFieldAccess();
facc.setName(ast.newSimpleName("TYPE"));
facc.setExpression(ast.newSimpleName("Boolean"));
arrInit.expressions().add(facc);
} else if (type.equals(org.objectweb.asm.Type.BYTE_TYPE)) {
FieldAccess facc = ast.newFieldAccess();
facc.setName(ast.newSimpleName("TYPE"));
facc.setExpression(ast.newSimpleName("Byte"));
arrInit.expressions().add(facc);
} else if (type.equals(org.objectweb.asm.Type.CHAR_TYPE)) {
FieldAccess facc = ast.newFieldAccess();
facc.setName(ast.newSimpleName("TYPE"));
facc.setExpression(ast.newSimpleName("Character"));
arrInit.expressions().add(facc);
} else if (type.equals(org.objectweb.asm.Type.DOUBLE_TYPE)) {
FieldAccess facc = ast.newFieldAccess();
facc.setName(ast.newSimpleName("TYPE"));
facc.setExpression(ast.newSimpleName("Double"));
arrInit.expressions().add(facc);
} else if (type.equals(org.objectweb.asm.Type.FLOAT_TYPE)) {
FieldAccess facc = ast.newFieldAccess();
facc.setName(ast.newSimpleName("TYPE"));
facc.setExpression(ast.newSimpleName("Float"));
arrInit.expressions().add(facc);
} else if (type.equals(org.objectweb.asm.Type.INT_TYPE)) {
FieldAccess facc = ast.newFieldAccess();
facc.setName(ast.newSimpleName("TYPE"));
facc.setExpression(ast.newSimpleName("Integer"));
arrInit.expressions().add(facc);
} else if (type.equals(org.objectweb.asm.Type.LONG_TYPE)) {
FieldAccess facc = ast.newFieldAccess();
facc.setName(ast.newSimpleName("TYPE"));
facc.setExpression(ast.newSimpleName("Long"));
arrInit.expressions().add(facc);
} else if (type.equals(org.objectweb.asm.Type.SHORT_TYPE)) {
FieldAccess facc = ast.newFieldAccess();
facc.setName(ast.newSimpleName("TYPE"));
facc.setExpression(ast.newSimpleName("Short"));
arrInit.expressions().add(facc);
} else {
final TypeLiteral clazz = ast.newTypeLiteral();
clazz.setType(ast.newSimpleType(ast.newName(type.getClassName())));
arrInit.expressions().add(clazz);
}
}
return callMethodCall;
}
use of org.eclipse.jdt.core.dom.TypeLiteral 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);
}
use of org.eclipse.jdt.core.dom.TypeLiteral in project AutoRefactor by JnRouvignac.
the class EnumMapRatherThanHashMapCleanUp method resolveParameter.
/**
* Map parameter for HashMap constructor to EnumMap constructor. HashMap(Map) ->
* EnumMap(EnumMap) <br/>
* other HashMap constructors -> EnumMap(Class) <br>
*
* @return correct parameter for EnumMap constructor
*/
private Expression resolveParameter(final Type keyType, final List<Expression> originalArgs) {
ASTNodeFactory ast = cuRewrite.getASTBuilder();
if (!originalArgs.isEmpty() && ASTNodes.instanceOf(originalArgs.get(0), EnumMap.class.getCanonicalName())) {
return ast.createCopyTarget(originalArgs.get(0));
}
TypeLiteral keyTypeLiteral = keyType.getAST().newTypeLiteral();
keyTypeLiteral.setType(ast.createCopyTarget(keyType));
return keyTypeLiteral;
}
Aggregations