/**
   * 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);
    }
  }
 /**
  * create the update code block with dao invoked.
  *
  * @param ast the ast tree. fc the configuration.
  */
 @SuppressWarnings("unchecked")
 public Block createBlock(AST ast, FastIbatisConfig fc) {
   Block block = ast.newBlock();
   MethodInvocation methodInvocation = ast.newMethodInvocation();
   methodInvocation.setExpression(ast.newName(Utils.lowFirstChar(fc.getModelName()) + "DAO"));
   methodInvocation.setName(ast.newSimpleName(fc.getMethodName()));
   methodInvocation.arguments().add(ast.newSimpleName("param"));
   ReturnStatement rs = ast.newReturnStatement();
   rs.setExpression(methodInvocation);
   block.statements().add(rs);
   return block;
 }
  /**
   * Builds a new {@link CatchClause} instance.
   *
   * @param exceptionTypeName the exception type name
   * @param caughtExceptionName the local name for the caught exception
   * @param stmts the statements to add to the catch clause
   * @return a new catch clause
   */
  public CatchClause catch0(
      String exceptionTypeName, String caughtExceptionName, Statement... stmts) {
    final CatchClause cc = ast.newCatchClause();
    final SingleVariableDeclaration svd = ast.newSingleVariableDeclaration();
    svd.setType(newSimpleType(exceptionTypeName));
    svd.setName(ast.newSimpleName(caughtExceptionName));
    cc.setException(svd);

    final Block block = ast.newBlock();
    addAll(statements(block), stmts);
    cc.setBody(block);
    return cc;
  }
Example #4
0
 @SuppressWarnings("unchecked")
 public Block newBlock(Statement... stmts) {
   Block newBlock = ast.newBlock();
   newBlock.statements().addAll(Arrays.asList(stmts));
   return newBlock;
 }
 /**
  * Builds a new {@link Block} instance.
  *
  * @param stmts the statements to add to the block
  * @return a new Block
  */
 public Block block(final Statement... stmts) {
   final Block block = ast.newBlock();
   addAll(statements(block), stmts);
   return block;
 }