Search in sources :

Example 6 with CatchClause

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

the class FlowAnalyzer method endVisit.

@Override
public void endVisit(TryStatement node) {
    if (skipNode(node)) {
        return;
    }
    TryFlowInfo info = createTry();
    setFlowInfo(node, info);
    for (Iterator<VariableDeclarationExpression> iterator = node.resources().iterator(); iterator.hasNext(); ) {
        info.mergeResources(getFlowInfo(iterator.next()), fFlowContext);
    }
    info.mergeTry(getFlowInfo(node.getBody()), fFlowContext);
    for (Iterator<CatchClause> iter = node.catchClauses().iterator(); iter.hasNext(); ) {
        CatchClause element = iter.next();
        info.mergeCatch(getFlowInfo(element), fFlowContext);
    }
    info.mergeFinally(getFlowInfo(node.getFinally()), fFlowContext);
}
Also used : VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) CatchClause(org.eclipse.jdt.core.dom.CatchClause)

Example 7 with CatchClause

use of org.eclipse.jdt.core.dom.CatchClause 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 8 with CatchClause

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

Example 9 with CatchClause

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

the class StatementAnalyzer method endVisit.

/* (non-Javadoc)
	 * Method declared in ASTVisitor
	 */
@Override
public void endVisit(TryStatement node) {
    ASTNode firstSelectedNode = getFirstSelectedNode();
    if (getSelection().getEndVisitSelectionMode(node) == Selection.AFTER) {
        if (firstSelectedNode == node.getBody() || firstSelectedNode == node.getFinally()) {
            invalidSelection(RefactoringCoreMessages.StatementAnalyzer_try_statement);
        } else {
            List<CatchClause> catchClauses = node.catchClauses();
            for (Iterator<CatchClause> iterator = catchClauses.iterator(); iterator.hasNext(); ) {
                CatchClause element = iterator.next();
                if (element == firstSelectedNode || element.getBody() == firstSelectedNode) {
                    invalidSelection(RefactoringCoreMessages.StatementAnalyzer_try_statement);
                } else if (element.getException() == firstSelectedNode) {
                    invalidSelection(RefactoringCoreMessages.StatementAnalyzer_catch_argument);
                }
            }
        }
    }
    super.endVisit(node);
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) CatchClause(org.eclipse.jdt.core.dom.CatchClause)

Example 10 with CatchClause

use of org.eclipse.jdt.core.dom.CatchClause in project xtext-xtend by eclipse.

the class JavaASTFlattener method visit.

@Override
public boolean visit(final SingleVariableDeclaration it) {
    if ((((it.getParent() instanceof MethodDeclaration) || (it.getParent() instanceof CatchClause)) || (it.getParent() instanceof EnhancedForStatement))) {
        final Function1<IExtendedModifier, Boolean> _function = (IExtendedModifier it_1) -> {
            return Boolean.valueOf(it_1.isAnnotation());
        };
        this.appendModifiers(it, IterableExtensions.<IExtendedModifier>filter(Iterables.<IExtendedModifier>filter(it.modifiers(), IExtendedModifier.class), _function));
    } else {
        this.appendModifiers(it, it.modifiers());
    }
    it.getType().accept(this);
    this.appendExtraDimensions(it.getExtraDimensions());
    boolean _isVarargs = it.isVarargs();
    if (_isVarargs) {
        this.appendToBuffer("...");
    }
    this.appendSpaceToBuffer();
    it.getName().accept(this);
    Expression _initializer = it.getInitializer();
    boolean _tripleNotEquals = (_initializer != null);
    if (_tripleNotEquals) {
        this.appendToBuffer("=");
        it.getInitializer().accept(this);
    }
    return false;
}
Also used : InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) CatchClause(org.eclipse.jdt.core.dom.CatchClause) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier)

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