Search in sources :

Example 6 with TryStatement

use of org.eclipse.jdt.core.dom.TryStatement in project whole by wholeplatform.

the class CompilationUnitBuilder method newTryStatement.

public TryStatement newTryStatement(Statement stm, SingleVariableDeclaration exc, Statement body) {
    TryStatement tryStm = newTryStatement();
    tryStm.getBody().statements().add(stm);
    tryStm.catchClauses().add(newCatchClause(exc, body));
    return tryStm;
}
Also used : TryStatement(org.eclipse.jdt.core.dom.TryStatement)

Example 7 with TryStatement

use of org.eclipse.jdt.core.dom.TryStatement 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)

Example 8 with TryStatement

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

the class JUnitCodeGenerator method createTryStmtWithEmptyCatch.

/*
	 * Needed to preserve program flow: 
	 * 
	 * try
	 * {
	 *    var0.doSth();
	 * }
	 * catch(Throwable t) {}
	 */
@SuppressWarnings("unchecked")
private TryStatement createTryStmtWithEmptyCatch(final AST ast, Statement stmt) {
    final TryStatement tryStmt = ast.newTryStatement();
    tryStmt.getBody().statements().add(stmt);
    final CatchClause cc = ast.newCatchClause();
    SingleVariableDeclaration excDecl = ast.newSingleVariableDeclaration();
    excDecl.setType(ast.newSimpleType(ast.newSimpleName("Throwable")));
    excDecl.setName(ast.newSimpleName("t"));
    cc.setException(excDecl);
    tryStmt.catchClauses().add(cc);
    return tryStmt;
}
Also used : TryStatement(org.eclipse.jdt.core.dom.TryStatement) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) CatchClause(org.eclipse.jdt.core.dom.CatchClause)

Example 9 with TryStatement

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

the class JUnitCodeGenerator method createMethodCallStmt.

/* (non-Javadoc)
	 * @see org.evosuite.testcarver.codegen.ICodeGenerator#createMethodCallStmt(org.evosuite.testcarver.capture.CaptureLog, int)
	 */
@SuppressWarnings("unchecked")
@Override
public void createMethodCallStmt(CaptureLog log, int logRecNo) {
    PostProcessor.notifyRecentlyProcessedLogRecNo(logRecNo);
    // assumption: all necessary statements are created and there is one variable for reach referenced object
    final int oid = log.objectIds.get(logRecNo);
    Object[] methodArgs = log.params.get(logRecNo);
    final String methodName = log.methodNames.get(logRecNo);
    final String methodDesc = log.descList.get(logRecNo);
    org.objectweb.asm.Type[] methodParamTypes = org.objectweb.asm.Type.getArgumentTypes(methodDesc);
    Class<?>[] methodParamTypeClasses = new Class[methodParamTypes.length];
    for (int i = 0; i < methodParamTypes.length; i++) {
        methodParamTypeClasses[i] = getClassForName(methodParamTypes[i].getClassName());
    }
    final String typeName = log.oidClassNames.get(log.oidRecMapping.get(oid));
    Class<?> type = getClassForName(typeName);
    // 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);
        // FIXME this does not make any sense
        try {
            this.getConstructorModifiers(type, methodParamTypeClasses);
        } catch (Exception e) {
            logger.error("" + e, e);
        }
        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();
            final int recNo = log.oidRecMapping.get(oid);
            final int dependencyOID = log.dependencies.getQuick(recNo);
            if (dependencyOID == CaptureLog.NO_DEPENDENCY) {
                ci.setType(this.createAstType(typeName, ast));
            } else {
                // final String varTypeName = oidToVarMapping.get(dependencyOID) + "." + typeName.substring(typeName.indexOf('$') + 1);
                // ci.setType(this.createAstType(varTypeName, ast));
                /*
					 * e.g.
					 * OuterClass.InnerClass innerObject = outerObject.new InnerClass();
					 */
                ci.setType(this.createAstType(typeName.substring(typeName.indexOf('$') + 1), ast));
                ci.setExpression(ast.newSimpleName(oidToVarMapping.get(dependencyOID)));
                final int index = Arrays.binarySearch(methodArgs, dependencyOID);
                if (index > -1) {
                    logger.debug(varName + " xxxx3 " + index);
                    final Object[] newArgs = new Object[methodArgs.length - 1];
                    System.arraycopy(methodArgs, 0, newArgs, 0, index);
                    System.arraycopy(methodArgs, index + 1, newArgs, index, methodArgs.length - index - 1);
                    methodArgs = newArgs;
                    final Class<?>[] newParamTypeClasses = new Class<?>[methodParamTypeClasses.length - 1];
                    System.arraycopy(methodParamTypeClasses, 0, newParamTypeClasses, 0, index);
                    System.arraycopy(methodParamTypeClasses, index + 1, newParamTypeClasses, index, methodParamTypeClasses.length - index - 1);
                    methodParamTypeClasses = newParamTypeClasses;
                    final org.objectweb.asm.Type[] newParamTypes = new org.objectweb.asm.Type[methodParamTypes.length - 1];
                    System.arraycopy(methodParamTypes, 0, newParamTypes, 0, index);
                    System.arraycopy(methodParamTypes, index + 1, newParamTypes, index, methodParamTypes.length - index - 1);
                    methodParamTypes = newParamTypes;
                }
            }
            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 = log.descList.get(logRecNo);
        final String returnType = org.objectweb.asm.Type.getReturnType(desc).getClassName();
        final Object returnValue = 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 (log.isStaticCallList.get(logRecNo)) {
                // can only happen, if this is a static method call (because constructor statement has been reported)
                final String tmpType = log.oidClassNames.get(log.oidRecMapping.get(oid));
                mi.setExpression(ast.newName(tmpType.split("\\.")));
            } else {
                try {
                    mi.setExpression(ast.newSimpleName(varName));
                } catch (final IllegalArgumentException ex) {
                    String msg = "";
                    msg += "--recno-- " + logRecNo + "\n";
                    msg += "--oid-- " + oid + "\n";
                    msg += "--method-- " + methodName + "\n";
                    msg += "--varName-- " + varName + "\n";
                    msg += "--oidToVarMap-- " + this.oidToVarMapping + "\n";
                    msg += (log) + "\n";
                    logger.error(msg);
                    throw new RuntimeException(msg);
                }
            }
            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) {
        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);
                // TODO: Warten was Florian und Gordon dazu sagen. Siehe Mail 04.08.2012
                if (argType == null) {
                    logger.error("Call within constructor needs instance of enclosing object as parameter -> ignored: " + arg);
                    methodBlock.statements().remove(methodBlock.statements().size() - 1);
                    return;
                }
                final CastExpression cast = ast.newCastExpression();
                if (methodParamType.isPrimitive()) {
                    if (methodParamType.equals(boolean.class)) {
                        cast.setType(ast.newPrimitiveType(PrimitiveType.BOOLEAN));
                    } else if (methodParamType.equals(byte.class)) {
                        cast.setType(ast.newPrimitiveType(PrimitiveType.BYTE));
                    } else if (methodParamType.equals(char.class)) {
                        cast.setType(ast.newPrimitiveType(PrimitiveType.CHAR));
                    } else if (methodParamType.equals(double.class)) {
                        cast.setType(ast.newPrimitiveType(PrimitiveType.DOUBLE));
                    } else if (methodParamType.equals(float.class)) {
                        cast.setType(ast.newPrimitiveType(PrimitiveType.FLOAT));
                    } else if (methodParamType.equals(int.class)) {
                        cast.setType(ast.newPrimitiveType(PrimitiveType.INT));
                    } else if (methodParamType.equals(long.class)) {
                        cast.setType(ast.newPrimitiveType(PrimitiveType.LONG));
                    } else if (methodParamType.equals(short.class)) {
                        cast.setType(ast.newPrimitiveType(PrimitiveType.SHORT));
                    } else {
                        throw new RuntimeException("unknown primitive type: " + methodParamType);
                    }
                } else {
                    // we need an up-cast
                    if (methodParamType.getName().contains(".")) {
                        cast.setType(this.createAstType(methodParamType.getName(), ast));
                    } else {
                        cast.setType(createAstType(methodParamType.getName(), ast));
                    }
                }
                cast.setExpression(ast.newSimpleName(this.oidToVarMapping.get(arg)));
                arguments.add(cast);
            }
        }
    }
}
Also used : MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) Assignment(org.eclipse.jdt.core.dom.Assignment) 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) ArrayList(java.util.ArrayList) ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) Statement(org.eclipse.jdt.core.dom.Statement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ArrayType(org.eclipse.jdt.core.dom.ArrayType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) CastExpression(org.eclipse.jdt.core.dom.CastExpression)

Example 10 with TryStatement

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

the class JUnitCodeGenerator method createTryStatementForPostProcessing.

/*
	 * try
	 * {
	 *    var0.doSth();
	 * }
	 * catch(Throwable t)
	 * {
	 *    org.uni.saarland.sw.prototype.capture.PostProcessor.captureException(<logRecNo>);
	 * }
	 */
@SuppressWarnings("unchecked")
private TryStatement createTryStatementForPostProcessing(final AST ast, final Statement stmt, final int logRecNo) {
    final TryStatement tryStmt = this.createTryStmtWithEmptyCatch(ast, stmt);
    final CatchClause cc = (CatchClause) tryStmt.catchClauses().get(0);
    final MethodInvocation m = ast.newMethodInvocation();
    m.setExpression(ast.newName(new String[] { "org", "evosuite", "testcarver", "codegen", "PostProcessor" }));
    m.setName(ast.newSimpleName("captureException"));
    m.arguments().add(ast.newNumberLiteral(String.valueOf(logRecNo)));
    cc.getBody().statements().add(ast.newExpressionStatement(m));
    MethodInvocation m2 = ast.newMethodInvocation();
    m2.setExpression(ast.newSimpleName("t"));
    m2.setName(ast.newSimpleName("printStackTrace"));
    cc.getBody().statements().add(ast.newExpressionStatement(m2));
    return tryStmt;
}
Also used : TryStatement(org.eclipse.jdt.core.dom.TryStatement) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) CatchClause(org.eclipse.jdt.core.dom.CatchClause)

Aggregations

TryStatement (org.eclipse.jdt.core.dom.TryStatement)25 Statement (org.eclipse.jdt.core.dom.Statement)12 CatchClause (org.eclipse.jdt.core.dom.CatchClause)11 ASTNode (org.eclipse.jdt.core.dom.ASTNode)10 Block (org.eclipse.jdt.core.dom.Block)8 ArrayList (java.util.ArrayList)7 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)7 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)7 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)7 IfStatement (org.eclipse.jdt.core.dom.IfStatement)6 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)6 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)6 Type (org.eclipse.jdt.core.dom.Type)6 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)6 ForStatement (org.eclipse.jdt.core.dom.ForStatement)5 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)5 List (java.util.List)4 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)4 AST (org.eclipse.jdt.core.dom.AST)4 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)4