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;
}
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;
}
}
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;
}
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;
}
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;
}
Aggregations