Search in sources :

Example 1 with TryStmt

use of com.github.javaparser.ast.stmt.TryStmt in project checker-framework by typetools.

the class JointJavacJavaParserVisitor method visitTry.

@Override
public Void visitTry(TryTree javacTree, Node javaParserNode) {
    TryStmt node = castNode(TryStmt.class, javaParserNode, javacTree);
    processTry(javacTree, node);
    Iterator<? extends Tree> javacResources = javacTree.getResources().iterator();
    for (Expression resource : node.getResources()) {
        if (resource.isVariableDeclarationExpr()) {
            for (VariableDeclarator declarator : resource.asVariableDeclarationExpr().getVariables()) {
                assert javacResources.hasNext();
                javacResources.next().accept(this, declarator);
            }
        } else {
            assert javacResources.hasNext();
            javacResources.next().accept(this, resource);
        }
    }
    javacTree.getBlock().accept(this, node.getTryBlock());
    visitLists(javacTree.getCatches(), node.getCatchClauses());
    visitOptional(javacTree.getFinallyBlock(), node.getFinallyBlock());
    return null;
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) TryStmt(com.github.javaparser.ast.stmt.TryStmt) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator)

Example 2 with TryStmt

use of com.github.javaparser.ast.stmt.TryStmt in project checker-framework by typetools.

the class DoubleJavaParserVisitor method visit.

@Override
public void visit(final TryStmt node1, final Node other) {
    TryStmt node2 = (TryStmt) other;
    defaultAction(node1, node2);
    visitLists(node1.getCatchClauses(), node2.getCatchClauses());
    node1.getFinallyBlock().ifPresent(l -> l.accept(this, node2.getFinallyBlock().get()));
    visitLists(node1.getResources(), node2.getResources());
    node1.getTryBlock().accept(this, node2.getTryBlock());
}
Also used : TryStmt(com.github.javaparser.ast.stmt.TryStmt)

Example 3 with TryStmt

use of com.github.javaparser.ast.stmt.TryStmt in project drools by kiegroup.

the class FunctionGenerator method toFunction.

public static MethodDeclaration toFunction(FunctionDescr desc) {
    List<Parameter> parameters = new ArrayList<>();
    List<String> parameterTypes = desc.getParameterTypes();
    for (int i = 0; i < parameterTypes.size(); i++) {
        String type = parameterTypes.get(i);
        String name = desc.getParameterNames().get(i);
        parameters.add(new Parameter(toClassOrInterfaceType(type), name));
    }
    NodeList<Modifier> modifiers = NodeList.nodeList(Modifier.publicModifier(), Modifier.staticModifier());
    MethodDeclaration methodDeclaration = new MethodDeclaration(modifiers, desc.getName(), toClassOrInterfaceType(desc.getReturnType()), nodeList(parameters));
    BlockStmt block = DrlxParseUtil.parseBlock("try {} catch (Exception e) { throw new RuntimeException(e); }");
    TryStmt tryStmt = (TryStmt) block.getStatement(0);
    tryStmt.setTryBlock(DrlxParseUtil.parseBlock(desc.getBody()));
    methodDeclaration.setBody(block);
    return methodDeclaration;
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) TryStmt(com.github.javaparser.ast.stmt.TryStmt) ArrayList(java.util.ArrayList) Parameter(com.github.javaparser.ast.body.Parameter) Modifier(com.github.javaparser.ast.Modifier)

Aggregations

TryStmt (com.github.javaparser.ast.stmt.TryStmt)3 Modifier (com.github.javaparser.ast.Modifier)1 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)1 Parameter (com.github.javaparser.ast.body.Parameter)1 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)1 Expression (com.github.javaparser.ast.expr.Expression)1 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)1 ArrayList (java.util.ArrayList)1