Search in sources :

Example 26 with CatchClause

use of org.eclipse.jdt.core.dom.CatchClause in project AutoRefactor by JnRouvignac.

the class DoWhileRatherThanWhileCleanUp method getPrecedingCode.

@SuppressWarnings({ "deprecation" })
private List<ASTNode> getPrecedingCode(final ASTNode node) {
    Statement statement = null;
    if (node instanceof Statement) {
        statement = (Statement) node;
    } else {
        statement = ASTNodes.getTypedAncestor(node, Statement.class);
    }
    if (statement == null) {
        return new ArrayList<>();
    }
    List<ASTNode> precedingStatements = new ArrayList<>(ASTNodes.getPreviousSiblings(statement));
    ASTNode parent = statement.getParent();
    if (parent instanceof Block) {
        precedingStatements.addAll(0, getPrecedingCode(parent));
        return precedingStatements;
    }
    if (parent instanceof IfStatement) {
        precedingStatements.add(0, ((IfStatement) parent).getExpression());
        precedingStatements.addAll(0, getPrecedingCode(parent));
    }
    if (parent instanceof CatchClause) {
        TryStatement tryStatement = (TryStatement) parent.getParent();
        precedingStatements.addAll(0, ASTNodes.asList(tryStatement.getBody()));
        if (statement.getParent().getLocationInParent() != TryStatement.RESOURCES_PROPERTY) {
            precedingStatements.addAll(0, tryStatement.resources());
        }
        precedingStatements.addAll(0, getPrecedingCode(tryStatement));
    }
    if (parent instanceof TryStatement) {
        if (statement.getLocationInParent() == TryStatement.FINALLY_PROPERTY) {
            return precedingStatements;
        }
        if (statement.getLocationInParent() != TryStatement.RESOURCES_PROPERTY) {
            precedingStatements.addAll(0, ((TryStatement) parent).resources());
        }
        precedingStatements.addAll(0, getPrecedingCode(parent));
    }
    return precedingStatements;
}
Also used : IfStatement(org.eclipse.jdt.core.dom.IfStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) DoStatement(org.eclipse.jdt.core.dom.DoStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ArrayList(java.util.ArrayList) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block) CatchClause(org.eclipse.jdt.core.dom.CatchClause)

Example 27 with CatchClause

use of org.eclipse.jdt.core.dom.CatchClause in project AutoRefactor by JnRouvignac.

the class ASTNodes method fallsThrough.

/**
 * Return true if the statement falls through.
 *
 * @param statement the statement
 * @return true if the statement falls through.
 */
public static boolean fallsThrough(final Statement statement) {
    List<Statement> statements = asList(statement);
    if (statements.isEmpty()) {
        return false;
    }
    Statement lastStatement = statements.get(statements.size() - 1);
    switch(lastStatement.getNodeType()) {
        case ASTNode.RETURN_STATEMENT:
        case ASTNode.THROW_STATEMENT:
        case ASTNode.BREAK_STATEMENT:
        case ASTNode.CONTINUE_STATEMENT:
            return true;
        case ASTNode.BLOCK:
            Block block = (Block) lastStatement;
            return fallsThrough(block);
        case ASTNode.IF_STATEMENT:
            IfStatement ifStatement = (IfStatement) lastStatement;
            Statement thenStatement = ifStatement.getThenStatement();
            Statement elseStatement = ifStatement.getElseStatement();
            return fallsThrough(thenStatement) && fallsThrough(elseStatement);
        case ASTNode.TRY_STATEMENT:
            TryStatement tryStatement = (TryStatement) lastStatement;
            if (!fallsThrough(tryStatement.getBody()) || tryStatement.getFinally() != null && !fallsThrough(tryStatement.getFinally())) {
                return false;
            }
            if (tryStatement.catchClauses() != null) {
                for (Object catchClause : tryStatement.catchClauses()) {
                    if (!fallsThrough(((CatchClause) catchClause).getBody())) {
                        return false;
                    }
                }
            }
            return true;
        default:
            return false;
    }
}
Also used : IfStatement(org.eclipse.jdt.core.dom.IfStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) ThrowStatement(org.eclipse.jdt.core.dom.ThrowStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) LabeledStatement(org.eclipse.jdt.core.dom.LabeledStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) Block(org.eclipse.jdt.core.dom.Block) CatchClause(org.eclipse.jdt.core.dom.CatchClause)

Example 28 with CatchClause

use of org.eclipse.jdt.core.dom.CatchClause in project AutoRefactor by JnRouvignac.

the class ASTNodeFactory method newCatchClause.

/**
 * Builds a new {@link CatchClause} instance.
 *
 * @param exceptionTypeName   the exception type name
 * @param caughtExceptionName the local name for the caught exception
 * @param statements          the statements to add to the catch clause
 * @return a new catch clause
 */
public CatchClause newCatchClause(final String exceptionTypeName, final String caughtExceptionName, final Statement... statements) {
    CatchClause cc = ast.newCatchClause();
    SingleVariableDeclaration svd = ast.newSingleVariableDeclaration();
    svd.setType(simpleType(exceptionTypeName));
    svd.setName(ast.newSimpleName(caughtExceptionName));
    cc.setException(svd);
    Block block = ast.newBlock();
    addAll(block.statements(), statements);
    cc.setBody(block);
    return cc;
}
Also used : SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) Block(org.eclipse.jdt.core.dom.Block) CatchClause(org.eclipse.jdt.core.dom.CatchClause)

Example 29 with CatchClause

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

the class CodeGenerator 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[] { "de", "unisb", "cs", "st", "testcarver", "capture", "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)

Example 30 with CatchClause

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

the class CodeGenerator 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)

Aggregations

CatchClause (org.eclipse.jdt.core.dom.CatchClause)32 TryStatement (org.eclipse.jdt.core.dom.TryStatement)15 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)14 ASTNode (org.eclipse.jdt.core.dom.ASTNode)13 ArrayList (java.util.ArrayList)12 Statement (org.eclipse.jdt.core.dom.Statement)10 AST (org.eclipse.jdt.core.dom.AST)9 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)9 Type (org.eclipse.jdt.core.dom.Type)9 UnionType (org.eclipse.jdt.core.dom.UnionType)9 Block (org.eclipse.jdt.core.dom.Block)8 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)8 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)6 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)6 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)6 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)6 SwitchStatement (org.eclipse.jdt.core.dom.SwitchStatement)6 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)6 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)6 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)6