Example #1
0
  /**
   * Create a new block node with two children.
   *
   * @param node The child.
   * @return The new block.
   */
  private Block createBlockWithNode(AstNode node) {
    Block b = new Block();

    b.addChild(node);

    return b;
  }
Example #2
0
  public void testLinenoTry() {
    AstRoot root =
        parse(
            "\ntry {\n"
                + "    var x = 1;\n"
                + "} catch\n"
                + "    (err)\n"
                + "{\n"
                + "} finally {\n"
                + "    var y = 2;\n"
                + "}\n");

    TryStatement tryStmt = (TryStatement) root.getFirstChild();
    AstNode tryBlock = tryStmt.getTryBlock();
    List<CatchClause> catchBlocks = tryStmt.getCatchClauses();
    CatchClause catchClause = catchBlocks.get(0);
    Block catchVarBlock = catchClause.getBody();
    Name catchVar = catchClause.getVarName();
    AstNode finallyBlock = tryStmt.getFinallyBlock();
    AstNode finallyStmt = (AstNode) finallyBlock.getFirstChild();

    assertEquals(1, tryStmt.getLineno());
    assertEquals(1, tryBlock.getLineno());
    assertEquals(5, catchVarBlock.getLineno());
    assertEquals(4, catchVar.getLineno());
    assertEquals(3, catchClause.getLineno());
    assertEquals(6, finallyBlock.getLineno());
    assertEquals(7, finallyStmt.getLineno());
  }
 @Override
 public AstNode block(Iterable<AstNode> statements) {
   Block block = new Block();
   for (AstNode stmt : statements) {
     if (stmt != null) {
       block.addStatement(stmt);
     }
   }
   return block;
 }
 @Override
 public AstNode catchClause(AstNode varName, AstNode body) {
   CatchClause c = new CatchClause();
   c.setVarName(cast(varName, Name.class));
   if (body instanceof Block) {
     c.setBody((Block) body);
   } else {
     Block b = new Block();
     b.addStatement(body);
     c.setBody(b);
   }
   return c;
 }
 @Override
 public AstNode addStatement(AstNode blockOrStatement, AstNode statement) {
   if (blockOrStatement instanceof Block) {
     ((Block) blockOrStatement).addStatement(statement);
     return blockOrStatement;
   }
   Block block = new Block();
   if (blockOrStatement != null) {
     block.addStatement(blockOrStatement);
   }
   if (statement != null) {
     block.addStatement(statement);
   }
   return block;
 }
 @Override
 public AstNode addStatementBeginning(AstNode blockOrStatement, AstNode statement) {
   if (blockOrStatement instanceof Block) {
     if (statement != null) {
       blockOrStatement.addChildrenToFront(statement);
       statement.setParent(blockOrStatement);
     }
     return blockOrStatement;
   }
   Block block = new Block();
   if (statement != null) {
     block.addStatement(statement);
   }
   if (blockOrStatement != null) {
     block.addStatement(blockOrStatement);
   }
   return block;
 }
Example #7
0
  /**
   * Actual visiting method.
   *
   * @param node The node that is currently visited.
   * @return Whether to visit the children.
   */
  @Override
  public boolean visit(AstNode node) {

    if (node instanceof FunctionNode) {
      FunctionNode func = (FunctionNode) node;
      String functionName = getFunctionName(func);

      if (functionName.equals(funcName)) {
        Node domNode = nodeProp.getNode();
        String xpath = domNode.xpath;
        String line = nodeProp.getLine();
        String value = nodeProp.getValue();
        String typeofAccess = nodeProp.getTypeOfAccess();
        String property = nodeProp.getProperty();

        AstNode newNode =
            createMutationNode(
                func, xpath, ProgramPoint.ENTERPOSTFIX, typeofAccess, property, shouldDeleteNode);
        func.getBody().addChildToFront(newNode);

        /* get last line of the function */
        AstNode lastnode = (AstNode) func.getBody().getLastChild();
        /* if this is not a return statement, we need to add logging here also */
        if (!(lastnode instanceof ReturnStatement)) {
          newNode =
              createMutationNode(
                  func, xpath, ProgramPoint.EXITPOSTFIX, typeofAccess, property, shouldDeleteNode);
          /* add as last statement */
          func.getBody().addChildToBack(newNode);
        }
      }

    } else if (node instanceof SwitchCase) {
      // Add block around all statements in the switch case
      SwitchCase sc = (SwitchCase) node;
      List<AstNode> statements = sc.getStatements();
      List<AstNode> blockStatement = new ArrayList<AstNode>();
      Block b = new Block();

      if (statements != null) {
        Iterator<AstNode> it = statements.iterator();
        while (it.hasNext()) {
          AstNode stmnt = it.next();
          b.addChild(stmnt);
        }

        blockStatement.add(b);
        sc.setStatements(blockStatement);
      }
    } else if (node instanceof ReturnStatement) {

      FunctionNode func = node.getEnclosingFunction();
      String functionName = getFunctionName(func);

      if (functionName.equals(funcName)) {
        Node domNode = nodeProp.getNode();
        String xpath = domNode.xpath;
        String line = nodeProp.getLine();
        String value = nodeProp.getValue();
        String typeofAccess = nodeProp.getTypeOfAccess();
        String property = nodeProp.getProperty();
        AstNode newNode =
            createMutationNode(
                func, xpath, ProgramPoint.EXITPOSTFIX, typeofAccess, property, shouldDeleteNode);

        AstNode parent = makeSureBlockExistsAround(node);

        /* the parent is something we can prepend to */
        parent.addChildBefore(newNode, node);
      }
    }

    return true;
  }