Search in sources :

Example 66 with StringLiteral

use of org.eclipse.jdt.core.dom.StringLiteral in project evosuite-plus-plus by llmhyy.

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 67 with StringLiteral

use of org.eclipse.jdt.core.dom.StringLiteral in project evosuite-plus-plus by llmhyy.

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);
}
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 68 with StringLiteral

use of org.eclipse.jdt.core.dom.StringLiteral in project evosuite-plus-plus by llmhyy.

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 69 with StringLiteral

use of org.eclipse.jdt.core.dom.StringLiteral in project evosuite-plus-plus by llmhyy.

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 70 with StringLiteral

use of org.eclipse.jdt.core.dom.StringLiteral in project evosuite-plus-plus by llmhyy.

the class TestExtractingVisitor method retrieveTypeClass.

/**
 * <p>
 * retrieveTypeClass
 * </p>
 *
 * @param argument
 *            a {@link java.lang.Object} object.
 * @return a {@link java.lang.Class} object.
 */
protected Class<?> retrieveTypeClass(Object argument) {
    assert argument != null;
    if (argument instanceof SimpleType) {
        SimpleType simpleType = (SimpleType) argument;
        return retrieveTypeClass(simpleType);
    }
    if (argument instanceof ITypeBinding) {
        ITypeBinding binding = (ITypeBinding) argument;
        return retrieveTypeClass(binding);
    }
    if (argument instanceof IVariableBinding) {
        IVariableBinding variableBinding = (IVariableBinding) argument;
        return retrieveTypeClass(variableBinding.getType());
    }
    if (argument instanceof SimpleName) {
        SimpleName simpleName = (SimpleName) argument;
        return retrieveTypeClass(simpleName.resolveBinding());
    }
    if (argument instanceof StringLiteral) {
        return String.class;
    }
    if (argument instanceof NumberLiteral) {
        return retrieveTypeClass((NumberLiteral) argument);
    }
    if (argument instanceof PrimitiveType) {
        PrimitiveType primitiveType = (PrimitiveType) argument;
        String typeCode = primitiveType.getPrimitiveTypeCode().toString();
        Class<?> result = PRIMITIVE_TYPECODE_MAPPING.get(typeCode);
        assert result != null : "Could not resolve typecode " + typeCode + ".";
        return result;
    }
    if (argument instanceof ArrayType) {
        ArrayType arrayType = (ArrayType) argument;
        return retrieveTypeClass(arrayType);
    }
    if (argument instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) argument;
        return retrieveTypeClass(parameterizedType.getType());
    }
    if (argument instanceof VariableDeclarationFragment) {
        VariableDeclarationFragment varDeclFrgmnt = (VariableDeclarationFragment) argument;
        return retrieveTypeClass(varDeclFrgmnt.resolveBinding());
    }
    if (argument instanceof InfixExpression) {
        InfixExpression infixExpr = (InfixExpression) argument;
        ITypeBinding refTypeBinding = infixExpr.resolveTypeBinding();
        if (refTypeBinding != null) {
            return retrieveTypeClass(refTypeBinding);
        } else {
            throw new RuntimeException("Could not determine type class of infix expression '" + infixExpr + "'.");
        }
    }
    if (argument instanceof MethodInvocation) {
        MethodInvocation methodInvocation = (MethodInvocation) argument;
        ITypeBinding typeBinding = methodInvocation.resolveTypeBinding();
        if (typeBinding != null) {
            return retrieveTypeClass(typeBinding);
        }
        Expression typeExpression = methodInvocation.getExpression();
        if (typeExpression instanceof MethodInvocation) {
            MethodInvocation parentMethodInvocation = (MethodInvocation) typeExpression;
            IMethodBinding parentMethodBinding = parentMethodInvocation.resolveMethodBinding();
            return retrieveTypeClass(parentMethodBinding.getDeclaringClass());
        } else {
            return retrieveTypeClass(typeExpression);
        }
    }
    if (argument instanceof ArrayAccess) {
        ArrayAccess arrayAccess = (ArrayAccess) argument;
        return retrieveTypeClass(arrayAccess.getArray());
    }
    if (argument instanceof Class<?>) {
        return (Class<?>) argument;
    }
    if (argument instanceof ClassInstanceCreation) {
        return retrieveTypeClass(((ClassInstanceCreation) argument).resolveTypeBinding());
    }
    if (argument instanceof BooleanLiteral) {
        return Boolean.TYPE;
    }
    throw new UnsupportedOperationException("Retrieval of type " + argument.getClass() + " not implemented yet!");
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) BooleanLiteral(org.eclipse.jdt.core.dom.BooleanLiteral) SimpleName(org.eclipse.jdt.core.dom.SimpleName) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) PrimitiveExpression(org.evosuite.testcase.PrimitiveExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) GenericClass(org.evosuite.utils.GenericClass) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral)

Aggregations

StringLiteral (org.eclipse.jdt.core.dom.StringLiteral)74 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)31 Expression (org.eclipse.jdt.core.dom.Expression)22 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)20 CastExpression (org.eclipse.jdt.core.dom.CastExpression)19 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)18 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)16 ASTNode (org.eclipse.jdt.core.dom.ASTNode)12 SimpleName (org.eclipse.jdt.core.dom.SimpleName)11 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)11 CharacterLiteral (org.eclipse.jdt.core.dom.CharacterLiteral)10 ArrayList (java.util.ArrayList)9 AST (org.eclipse.jdt.core.dom.AST)9 BooleanLiteral (org.eclipse.jdt.core.dom.BooleanLiteral)9 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)9 MemberValuePair (org.eclipse.jdt.core.dom.MemberValuePair)9 NumberLiteral (org.eclipse.jdt.core.dom.NumberLiteral)9 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)9 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)8 NormalAnnotation (org.eclipse.jdt.core.dom.NormalAnnotation)8