Search in sources :

Example 1 with CatchClause

use of com.github.javaparser.ast.stmt.CatchClause in project javaparser by javaparser.

the class JavaParserFacadeResolutionTest method solveMultiCatchType.

@Test
public void solveMultiCatchType() {
    String code = "class A {\n" + "        public void foo() {\n" + "            try {\n" + "                \n" + "            } catch (IllegalStateException | IllegalArgumentException e) {\n" + "                \n" + "            }\n" + "        }\n" + "    }";
    CompilationUnit cu = parseWithTypeSolver(code);
    CatchClause catchClause = Navigator.findNodeOfGivenClass(cu, CatchClause.class);
    Type jpType = catchClause.getParameter().getType();
    ResolvedType jssType = jpType.resolve();
    assertEquals(true, jssType instanceof ResolvedUnionType);
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ResolvedUnionType(com.github.javaparser.resolution.types.ResolvedUnionType) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) Type(com.github.javaparser.ast.type.Type) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) CatchClause(com.github.javaparser.ast.stmt.CatchClause) ResolvedUnionType(com.github.javaparser.resolution.types.ResolvedUnionType) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) Test(org.junit.Test)

Example 2 with CatchClause

use of com.github.javaparser.ast.stmt.CatchClause in project javaparser by javaparser.

the class DumpVisitor method visit.

@Override
public void visit(final TryStmt n, final Object arg) {
    printJavaComment(n.getComment(), arg);
    printer.print("try ");
    if (!n.getResources().isEmpty()) {
        printer.print("(");
        Iterator<VariableDeclarationExpr> resources = n.getResources().iterator();
        boolean first = true;
        while (resources.hasNext()) {
            visit(resources.next(), arg);
            if (resources.hasNext()) {
                printer.print(";");
                printer.printLn();
                if (first) {
                    printer.indent();
                }
            }
            first = false;
        }
        if (n.getResources().size() > 1) {
            printer.unindent();
        }
        printer.print(") ");
    }
    n.getTryBlock().accept(this, arg);
    if (n.getCatchs() != null) {
        for (final CatchClause c : n.getCatchs()) {
            c.accept(this, arg);
        }
    }
    if (n.getFinallyBlock() != null) {
        printer.print(" finally ");
        n.getFinallyBlock().accept(this, arg);
    }
}
Also used : CatchClause(com.github.javaparser.ast.stmt.CatchClause)

Example 3 with CatchClause

use of com.github.javaparser.ast.stmt.CatchClause in project javaparser by javaparser.

the class VoidVisitorAdapter method visit.

@Override
public void visit(final TryStmt n, final A arg) {
    visitComment(n.getComment(), arg);
    if (n.getResources() != null) {
        for (final VariableDeclarationExpr v : n.getResources()) {
            v.accept(this, arg);
        }
    }
    n.getTryBlock().accept(this, arg);
    if (n.getCatchs() != null) {
        for (final CatchClause c : n.getCatchs()) {
            c.accept(this, arg);
        }
    }
    if (n.getFinallyBlock() != null) {
        n.getFinallyBlock().accept(this, arg);
    }
}
Also used : CatchClause(com.github.javaparser.ast.stmt.CatchClause)

Aggregations

CatchClause (com.github.javaparser.ast.stmt.CatchClause)3 CompilationUnit (com.github.javaparser.ast.CompilationUnit)1 Type (com.github.javaparser.ast.type.Type)1 ResolvedReferenceType (com.github.javaparser.resolution.types.ResolvedReferenceType)1 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)1 ResolvedUnionType (com.github.javaparser.resolution.types.ResolvedUnionType)1 Test (org.junit.Test)1