Search in sources :

Example 36 with StringLiteral

use of org.eclipse.jdt.core.dom.StringLiteral 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;
}
Also used : StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) TypeLiteral(org.eclipse.jdt.core.dom.TypeLiteral) ArrayCreation(org.eclipse.jdt.core.dom.ArrayCreation) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) ArrayInitializer(org.eclipse.jdt.core.dom.ArrayInitializer)

Example 37 with StringLiteral

use of org.eclipse.jdt.core.dom.StringLiteral in project evosuite by EvoSuite.

the class CodeGenerator method createFieldReadAccessStmt.

@SuppressWarnings("unchecked")
private void createFieldReadAccessStmt(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 String methodName = this.log.methodNames.get(logRecNo);
    final int oid = this.log.objectIds.get(logRecNo);
    final int captureId = this.log.captureIds.get(logRecNo);
    String returnVarName = null;
    final Object returnValue = this.log.returnValues.get(logRecNo);
    if (!CaptureLog.RETURN_TYPE_VOID.equals(returnValue)) {
        Integer returnValueOID = (Integer) returnValue;
        final String descriptor = this.log.descList.get(logRecNo);
        final String fieldTypeName = org.objectweb.asm.Type.getType(descriptor).getClassName();
        final String typeName = this.log.oidClassNames.get(this.log.oidRecMapping.get(oid));
        final String fieldName = this.log.namesOfAccessedFields.get(captureId);
        final String receiverVarName = this.oidToVarMapping.get(oid);
        final 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;
        // e.g. Person var0 = Person.BEN;
        returnVarName = this.createNewVarName(returnValueOID, fieldTypeName);
        VariableDeclarationFragment vd = ast.newVariableDeclarationFragment();
        vd.setName(ast.newSimpleName(returnVarName));
        if (isReflectionAccessNeeded) {
            this.isGetFieldMethodNeeded = true;
            // -- construct getField() call
            final MethodInvocation getFieldCall = ast.newMethodInvocation();
            getFieldCall.setName(ast.newSimpleName("getField"));
            StringLiteral stringLiteral = ast.newStringLiteral();
            stringLiteral.setLiteralValue(fieldTypeName);
            // class name
            getFieldCall.arguments().add(stringLiteral);
            stringLiteral = ast.newStringLiteral();
            stringLiteral.setLiteralValue(fieldName);
            // field name
            getFieldCall.arguments().add(stringLiteral);
            if (receiverVarName == null) {
                // static call -> no receiver
                getFieldCall.arguments().add(ast.newNullLiteral());
            } else {
                // receiver
                getFieldCall.arguments().add(ast.newSimpleName(receiverVarName));
            }
            // cast from object to field type
            final CastExpression cast = ast.newCastExpression();
            cast.setType(ast.newSimpleType(ast.newName(fieldTypeName)));
            cast.setExpression(getFieldCall);
            vd.setInitializer(cast);
        } else {
            FieldAccess fa = ast.newFieldAccess();
            if (CaptureLog.GETSTATIC.equals(methodName)) {
                final String classType = this.log.oidClassNames.get(this.log.oidRecMapping.get(oid));
                fa.setExpression(ast.newName(classType.split("\\.")));
            } else {
                fa.setExpression(ast.newSimpleName(receiverVarName));
            }
            fa.setName(ast.newSimpleName(fieldName));
            vd.setInitializer(fa);
        }
        VariableDeclarationStatement stmt = ast.newVariableDeclarationStatement(vd);
        stmt.setType(this.createAstType(fieldTypeName, ast));
        methodBlock.statements().add(stmt);
    }
}
Also used : StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) CastExpression(org.eclipse.jdt.core.dom.CastExpression) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess)

Example 38 with StringLiteral

use of org.eclipse.jdt.core.dom.StringLiteral 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);
}
Also used : BooleanLiteral(org.eclipse.jdt.core.dom.BooleanLiteral) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) TypeLiteral(org.eclipse.jdt.core.dom.TypeLiteral) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) CharacterLiteral(org.eclipse.jdt.core.dom.CharacterLiteral)

Example 39 with StringLiteral

use of org.eclipse.jdt.core.dom.StringLiteral in project evosuite by EvoSuite.

the class CodeGenerator method createUnobservedInitStmt.

@SuppressWarnings("unchecked")
private void createUnobservedInitStmt(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);
    final String type = this.log.oidClassNames.get(this.log.oidRecMapping.get(oid));
    final Object value = this.log.params.get(logRecNo)[0];
    this.isXStreamNeeded = true;
    final VariableDeclarationFragment vd = ast.newVariableDeclarationFragment();
    // handling because there must always be a new instantiation statement for pseudo inits
    this.oidToVarMapping.remove(oid);
    vd.setName(ast.newSimpleName(this.createNewVarName(oid, type)));
    final MethodInvocation methodInvocation = ast.newMethodInvocation();
    final Name name = ast.newSimpleName("XSTREAM");
    methodInvocation.setExpression(name);
    methodInvocation.setName(ast.newSimpleName("fromXML"));
    final StringLiteral xmlParam = ast.newStringLiteral();
    xmlParam.setLiteralValue((String) value);
    methodInvocation.arguments().add(xmlParam);
    final CastExpression castExpr = ast.newCastExpression();
    castExpr.setType(this.createAstType(type, ast));
    castExpr.setExpression(methodInvocation);
    vd.setInitializer(castExpr);
    final VariableDeclarationStatement vs = ast.newVariableDeclarationStatement(vd);
    vs.setType(this.createAstType(type, ast));
    methodBlock.statements().add(vs);
}
Also used : StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) CastExpression(org.eclipse.jdt.core.dom.CastExpression) Name(org.eclipse.jdt.core.dom.Name)

Example 40 with StringLiteral

use of org.eclipse.jdt.core.dom.StringLiteral 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;
}
Also used : StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) TypeLiteral(org.eclipse.jdt.core.dom.TypeLiteral) ArrayCreation(org.eclipse.jdt.core.dom.ArrayCreation) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) ArrayInitializer(org.eclipse.jdt.core.dom.ArrayInitializer)

Aggregations

StringLiteral (org.eclipse.jdt.core.dom.StringLiteral)54 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)20 Expression (org.eclipse.jdt.core.dom.Expression)16 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)16 ASTNode (org.eclipse.jdt.core.dom.ASTNode)11 CastExpression (org.eclipse.jdt.core.dom.CastExpression)11 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)10 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)8 AST (org.eclipse.jdt.core.dom.AST)7 MemberValuePair (org.eclipse.jdt.core.dom.MemberValuePair)7 SimpleName (org.eclipse.jdt.core.dom.SimpleName)7 CharacterLiteral (org.eclipse.jdt.core.dom.CharacterLiteral)6 NumberLiteral (org.eclipse.jdt.core.dom.NumberLiteral)6 SuperMethodInvocation (org.eclipse.jdt.core.dom.SuperMethodInvocation)6 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)6 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)6 ArrayList (java.util.ArrayList)5 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)5 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)5 Name (org.eclipse.jdt.core.dom.Name)5