Esempio n. 1
0
 @SuppressWarnings("unchecked")
 public TryStatement newTryCatch(Block tryBlock, CatchClause... clauses) {
   TryStatement tryStmt = ast.newTryStatement();
   tryStmt.setBody(tryBlock);
   tryStmt.catchClauses().addAll(Arrays.asList(clauses));
   return tryStmt;
 }
  /**
   * Inserts the provided {@code statement} after the the node this inserter was created for. Tries
   * to put the {@code statement} as close to the {@code marker} as possible.
   *
   * @param statement The statement to insert after the insertion node.
   */
  void insertAfter(final Statement statement) {
    Validate.notNull(statement);
    Validate.validState(
        this.insertionList != null,
        "Insertion is only possible after the inserter has been set up.");
    Validate.validState(
        this.insertionList.isPresent(),
        "Insertion is only possible if setting up the inserter succeeded.");

    if (this.differentAfterList) {
      this.afterList.add(0, statement);
    } else if (this.breakDetector.containsControlFlowBreakingStatement(this.markedStatement)) {
      // we are trying to insert after a control flow breaking statement. That’s
      // dangerous, better surround with try…finally

      final AST factory = this.markedStatement.getAST();
      final TryStatement tryStatement = factory.newTryStatement();
      tryStatement.setFinally(factory.newBlock());

      @SuppressWarnings("unchecked")
      final List<Statement> tryBodyStatements = tryStatement.getBody().statements();
      @SuppressWarnings("unchecked")
      final List<Statement> finallyStatements = tryStatement.getFinally().statements();
      final Statement copy = (Statement) ASTNode.copySubtree(factory, this.markedStatement);
      tryBodyStatements.add(copy);
      finallyStatements.add(statement);
      this.insertionList.get().set(this.markerIndex, tryStatement);
      this.markedStatement = tryStatement;
      this.differentAfterList = true;
      this.afterList = finallyStatements;
    } else {
      this.insertionList.get().add(this.markerIndex, statement);
    }
  }
Esempio n. 3
0
 /**
  * Builds a new {@link TryStatement} instance.
  *
  * @param body the try body
  * @param catchClauses the catch clauses for the try
  * @return a new try statement
  */
 public TryStatement try0(final Block body, CatchClause... catchClauses) {
   final TryStatement tryS = ast.newTryStatement();
   tryS.setBody(body);
   addAll(catchClauses(tryS), catchClauses);
   return tryS;
 }