Search in sources :

Example 86 with Assignment

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

the class TestExtractingVisitor method retrieveResultReference.

private VariableReference retrieveResultReference(SuperMethodInvocation superMethodInvocation) {
    // TODO Duplicate code from retrieveResultReference(MethodInvocation)
    // too bad they don't have a common matching interface
    VariableReference result = calleeResultMap.get(superMethodInvocation.toString());
    if (result != null) {
        return result;
    }
    ASTNode parent = superMethodInvocation.getParent();
    if (parent instanceof VariableDeclarationFragment) {
        return retrieveVariableReference(parent, null);
    }
    if (parent instanceof Assignment) {
        Assignment assignment = (Assignment) parent;
        return retrieveVariableReference(assignment.getLeftHandSide(), null);
    }
    IMethodBinding methodBinding = superMethodInvocation.resolveMethodBinding();
    result = retrieveVariableReference(methodBinding.getReturnType(), null);
    calleeResultMap.put(superMethodInvocation.toString(), result);
    return result;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) VariableReference(org.evosuite.testcase.VariableReference) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Example 87 with Assignment

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!");
}
Also used : 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) Assignment(org.eclipse.jdt.core.dom.Assignment) ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) VariableReference(org.evosuite.testcase.VariableReference) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) 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) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ArrayCreation(org.eclipse.jdt.core.dom.ArrayCreation) CastExpression(org.eclipse.jdt.core.dom.CastExpression) NullLiteral(org.eclipse.jdt.core.dom.NullLiteral) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral) CharacterLiteral(org.eclipse.jdt.core.dom.CharacterLiteral)

Example 88 with Assignment

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;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) VariableReference(org.evosuite.testcase.VariableReference) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 89 with Assignment

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));
    }
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) CastExpression(org.eclipse.jdt.core.dom.CastExpression) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess)

Example 90 with 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);
                }
            }
        }
    }
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) TryStatement(org.eclipse.jdt.core.dom.TryStatement) Statement(org.eclipse.jdt.core.dom.Statement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) Assignment(org.eclipse.jdt.core.dom.Assignment) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) TryStatement(org.eclipse.jdt.core.dom.TryStatement) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) TIntArrayList(gnu.trove.list.array.TIntArrayList) List(java.util.List) CastExpression(org.eclipse.jdt.core.dom.CastExpression)

Aggregations

Assignment (org.eclipse.jdt.core.dom.Assignment)229 Expression (org.eclipse.jdt.core.dom.Expression)115 SimpleName (org.eclipse.jdt.core.dom.SimpleName)94 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)91 ASTNode (org.eclipse.jdt.core.dom.ASTNode)84 AST (org.eclipse.jdt.core.dom.AST)69 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)64 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)63 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)63 CastExpression (org.eclipse.jdt.core.dom.CastExpression)62 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)61 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)60 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)59 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)58 Statement (org.eclipse.jdt.core.dom.Statement)55 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)49 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)48 Block (org.eclipse.jdt.core.dom.Block)43 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)43 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)41