com.github.javaparser.ast.stmt.TryStmt Java Examples

The following examples show how to use com.github.javaparser.ast.stmt.TryStmt. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: QueryEndpointGenerator.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
private BlockStmt wrapBodyAddingExceptionLogging(BlockStmt body, String nameURL) {
    TryStmt ts = new TryStmt();
    ts.setTryBlock(body);
    CatchClause cc = new CatchClause();
    String exceptionName = "e";
    cc.setParameter(new Parameter().setName(exceptionName).setType(Exception.class));
    BlockStmt cb = new BlockStmt();
    cb.addStatement(parseStatement(
            String.format(
                    "SystemMetricsCollector.registerException(\"%s\", %s.getStackTrace()[0].toString());",
                    nameURL,
                    exceptionName)
    ));
    cb.addStatement(new ThrowStmt(new NameExpr(exceptionName)));
    cc.setBody(cb);
    ts.setCatchClauses(new NodeList<>(cc));
    return new BlockStmt(new NodeList<>(ts));
}
 
Example #2
Source File: PrettyPrintVisitor.java    From stategen with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void visit(final TryStmt n, final Void 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(") ");
    }
    if (n.getTryBlock().isPresent()) {
        n.getTryBlock().get().accept(this, arg);
    }
    for (final CatchClause c : n.getCatchClauses()) {
        c.accept(this, arg);
    }
    if (n.getFinallyBlock().isPresent()) {
        printer.print(" finally ");
        n.getFinallyBlock().get().accept(this, arg);
    }
}
 
Example #3
Source File: TryStmtMerger.java    From dolphin with Apache License 2.0 5 votes vote down vote up
@Override public TryStmt doMerge(TryStmt first, TryStmt second) {
  TryStmt ts = new TryStmt();
  ts.setResources(mergeCollectionsInOrder(first.getResources(),second.getResources()));
  ts.setCatchs(mergeCollectionsInOrder(first.getCatchs(),second.getCatchs()));
  ts.setTryBlock(mergeSingle(first.getTryBlock(),second.getTryBlock()));
  ts.setFinallyBlock(mergeSingle(first.getFinallyBlock(),second.getFinallyBlock()));
  return ts;
}
 
Example #4
Source File: FlywayTask.java    From enkan with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public void execute(PathResolver pathResolver) throws Exception {
    CompilationUnit cu = new CompilationUnit();
    cu.setPackage(new PackageDeclaration(ASTHelper.createNameExpr("db.migration")));
    List<ImportDeclaration> imports = new ArrayList<>();
    imports.add(new ImportDeclaration(ASTHelper.createNameExpr("org.flywaydb.core.api.migration.BaseJavaMigration"), false, false));
    imports.add(new ImportDeclaration(ASTHelper.createNameExpr("org.flywaydb.core.api.migration.Context"), false, false));
    imports.add(new ImportDeclaration(ASTHelper.createNameExpr("java.sql.Statement"), false, false));
    cu.setImports(imports);

    String version = LocalDateTime.now().format(VERSION_FMT);
    ClassOrInterfaceDeclaration migrationClass = new ClassOrInterfaceDeclaration(
            ModifierSet.PUBLIC, false, "V" + version + "__Create"
            + CaseConverter.pascalCase(tableName));
    ASTHelper.addTypeDeclaration(cu, migrationClass);

    List<ClassOrInterfaceType> extendsList = new ArrayList<>();
    extendsList.add(new ClassOrInterfaceType("BaseJavaMigration"));
    migrationClass.setExtends(extendsList);

    MethodDeclaration migrateMethod = new MethodDeclaration(ModifierSet.PUBLIC, ASTHelper.VOID_TYPE, "migrate");
    ASTHelper.addMember(migrationClass, migrateMethod);
    ASTHelper.addParameter(migrateMethod, ASTHelper.createParameter(
            ASTHelper.createReferenceType("Context", 0), "context"
    ));
    migrateMethod.setThrows(Collections.singletonList(ASTHelper.createNameExpr("Exception")));

    List<AnnotationExpr> annotations = new ArrayList<>();
    annotations.add(new MarkerAnnotationExpr(ASTHelper.createNameExpr("Override")));
    migrateMethod.setAnnotations(annotations);

    BlockStmt block = new BlockStmt();
    migrateMethod.setBody(block);

    TryStmt tryStmt = new TryStmt(createStatementExecuteBlock(), null, null);
    tryStmt.setResources(Collections.singletonList(createAssignStatement()));
    ASTHelper.addStmt(block, tryStmt);

    try (Writer writer = new OutputStreamWriter(
            pathResolver.destinationAsStream(
                    "src/main/java/db/migration/" + migrationClass.getName() + ".java"))) {
        writer.write(cu.toString());
    }
}
 
Example #5
Source File: TryStmtMerger.java    From dolphin with Apache License 2.0 4 votes vote down vote up
@Override public boolean doIsEquals(TryStmt first, TryStmt second) {
  if(!isEqualsUseMerger(first.getResources(),second.getResources())) return false;
  if(!isEqualsUseMerger(first.getCatchs(),second.getCatchs())) return false;

  return true;
}
 
Example #6
Source File: TraceVisitor.java    From JCTools with Apache License 2.0 4 votes vote down vote up
@Override
public void visit(TryStmt n, Void arg) {
    out.println("TryStmt: " + (extended ? n : ""));
    super.visit(n, arg);
}