当前位置: 首页>>代码示例>>Java>>正文


Java Node.replaceChild方法代码示例

本文整理汇总了Java中com.google.javascript.rhino.Node.replaceChild方法的典型用法代码示例。如果您正苦于以下问题:Java Node.replaceChild方法的具体用法?Java Node.replaceChild怎么用?Java Node.replaceChild使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.google.javascript.rhino.Node的用法示例。


在下文中一共展示了Node.replaceChild方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: rewriteCallSites

import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
 * Rewrites object method call sites as calls to global functions
 * that take "this" as their first argument.
 *
 * Before:
 *   o.foo(a, b, c)
 *
 * After:
 *   foo(o, a, b, c)
 */
private void rewriteCallSites(SimpleDefinitionFinder defFinder,
                              Definition definition,
                              String newMethodName) {
  Collection<UseSite> useSites = defFinder.getUseSites(definition);
  for (UseSite site : useSites) {
    Node node = site.node;
    Node parent = node.getParent();

    Node objectNode = node.getFirstChild();
    node.removeChild(objectNode);
    parent.replaceChild(node, objectNode);
    parent.addChildToFront(Node.newString(Token.NAME, newMethodName));
    compiler.reportCodeChange();
  }
}
 
开发者ID:andyjko,项目名称:feedlack,代码行数:26,代码来源:DevirtualizePrototypeMethods.java

示例2: flattenNameRef

import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
 * Replaces a GETPROP a.b.c with a NAME a$b$c.
 *
 * @param alias A flattened prefix name (e.g. "a$b")
 * @param n The GETPROP node corresponding to the original name (e.g. "a.b")
 * @param parent {@code n}'s parent
 * @param originalName String version of the property name.
 */
private void flattenNameRef(String alias, Node n, Node parent,
    String originalName) {
  // BEFORE:
  //   getprop
  //     getprop
  //       name a
  //       string b
  //     string c
  // AFTER:
  //   name a$b$c
  Node ref = NodeUtil.newName(alias, n, originalName);
  NodeUtil.copyNameAnnotations(n.getLastChild(), ref);
  parent.replaceChild(n, ref);
  compiler.reportCodeChange();
}
 
开发者ID:andyjko,项目名称:feedlack,代码行数:24,代码来源:CollapseProperties.java

示例3: inject

import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
 * With the map provided, replace the names with expression trees.
 * @param node The root of the node tree within which to perform the
 *     substitutions.
 * @param parent The parent root node.
 * @param replacements The map of names to template node trees with which
 *     to replace the name Nodes.
 * @returns The root node or its replacement.
 */
static Node inject(Node node, Node parent,
    Map<String, Node> replacements) {
  if (node.getType() == Token.NAME) {
    Node replacementTemplate = replacements.get(node.getString());
    if (replacementTemplate != null) {
      // This should not be replacing declared names.
      Preconditions.checkState(parent.getType() != Token.FUNCTION
          || parent.getType() != Token.VAR
          || parent.getType() != Token.CATCH);
      // The name may need to be replaced more than once,
      // so we need to clone the node.
      Node replacement = replacementTemplate.cloneTree();
      parent.replaceChild(node, replacement);
      return replacement;
    }
  }

  for (Node c = node.getFirstChild(); c != null; c = c.getNext()) {
    // We have to reassign c in case it was replaced, because the removed c's
    // getNext() would no longer be correct.
    c = inject(c, node, replacements);
  }

  return node;
}
 
开发者ID:andyjko,项目名称:feedlack,代码行数:35,代码来源:FunctionArgumentInjector.java

示例4: tryFoldAdd

import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
 * Try to fold a ADD node
 */
void tryFoldAdd(NodeTraversal t, Node n, Node left, Node right,
                Node parent) {
  if (left.getType() == Token.STRING ||
      right.getType() == Token.STRING) {

    // Add strings.
    String leftString = NodeUtil.getStringValue(left);
    String rightString = NodeUtil.getStringValue(right);
    if (leftString != null && rightString != null) {
      parent.replaceChild(n, Node.newString(leftString + rightString));
      t.getCompiler().reportCodeChange();
    }
  } else {
    // Try arithmetic add
    tryFoldArithmetic(t, n, left, right, parent);
  }
}
 
开发者ID:andyjko,项目名称:feedlack,代码行数:21,代码来源:FoldConstants.java

示例5: rewriteDefinition

import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
 * Rewrites method definitions as global functions that take "this"
 * as their first argument.
 *
 * Before:
 *   a.prototype.b = function(a, b, c) {...}
 *
 * After:
 *   var b = function(self, a, b, c) {...}
 */
private void rewriteDefinition(Node node, String newMethodName) {
  Node parent = node.getParent();
  Node functionNode = parent.getLastChild();
  Node expr = parent.getParent();
  Node block = expr.getParent();

  Node newNameNode = Node.newString(Token.NAME, newMethodName);
  parent.removeChild(functionNode);
  newNameNode.addChildToFront(functionNode);
  block.replaceChild(expr, new Node(Token.VAR, newNameNode));

  // add extra argument
  String self = newMethodName + "$self";
  Node argList = functionNode.getFirstChild().getNext();
  argList.addChildToFront(Node.newString(Token.NAME, self));

  // rewrite body
  Node body = functionNode.getLastChild();
  replaceReferencesToThis(body, self);

  // fix type
  fixFunctionType(functionNode);

  compiler.reportCodeChange();
}
 
开发者ID:andyjko,项目名称:feedlack,代码行数:36,代码来源:DevirtualizePrototypeMethods.java

示例6: visit

import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  if (parent == null) {
    return;
  }
  if (n.getType() == Token.FUNCTION || n.getType() == Token.SCRIPT) {
    return;
  }
  // Removes TRYs that had its CATCH removed and/or empty FINALLY.
  if (n.getType() == Token.TRY) {
    Node body = n.getFirstChild();
    Node catchOrFinallyBlock = body.getNext();
    Node finallyBlock = catchOrFinallyBlock.getNext();

    if (!catchOrFinallyBlock.hasChildren() &&
        (finallyBlock == null || !finallyBlock.hasChildren())) {
      n.removeChild(body);
      parent.replaceChild(n, body);
      compiler.reportCodeChange();
      n = body;
    }
  }
  GraphNode<Node, Branch> gNode = curCfg.getNode(n);
  if (gNode == null) { // Not in CFG.
    return;
  }
  if (gNode.getAnnotation() != GraphReachability.REACHABLE ||
      (removeNoOpStatements && !NodeUtil.mayHaveSideEffects(n))) {
    removeDeadExprStatementSafely(n, parent);
  }
}
 
开发者ID:andyjko,项目名称:feedlack,代码行数:32,代码来源:UnreachableCodeElimination.java

示例7: replaceReturnWithBreak

import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
 * Replace the 'return' statement with its child expression.
 *   "return foo()" becomes "{foo(); break;}" or
 *      "{resultName = foo(); break;}"
 *   "return" becomes {break;} or "{resultName = void 0;break;}".
 */
private static Node replaceReturnWithBreak(Node current, Node parent,
    String resultName, String labelName) {

  if (current.getType() == Token.FUNCTION
      || current.getType() == Token.EXPR_RESULT) {
    // Don't recurse into functions definitions, and expressions can't
    // contain RETURN nodes.
    return current;
  }

  if (current.getType() == Token.RETURN) {
    Preconditions.checkState(NodeUtil.isStatementBlock(parent));

    Node resultNode = getReplacementReturnStatement(current, resultName);
    Node name = Node.newString(Token.NAME, labelName);
    Node breakNode = new Node(Token.BREAK, name);

    // Replace the node in parent, and reset current to the first new child.
    parent.replaceChild(current, breakNode);
    if (resultNode != null) {
      parent.addChildBefore(resultNode, breakNode);
    }
    current = breakNode;
  } else {
    for (Node c = current.getFirstChild(); c != null; c = c.getNext()) {
      // c may be replaced.
      c = replaceReturnWithBreak(c, current, resultName, labelName);
    }
  }

  return current;
}
 
开发者ID:andyjko,项目名称:feedlack,代码行数:39,代码来源:FunctionToBlockMutator.java

示例8: remove

import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
 * Remove this node.
 */
public void remove() {
  Node rhs = node.getNext();
  Node last = parent;
  for (Node ancestor : assignAncestors) {
    if (NodeUtil.isExpressionNode(ancestor)) {
      lastAncestor.removeChild(ancestor);
    } else {
      rhs.detachFromParent();
      ancestor.replaceChild(last, rhs);
    }
    last = ancestor;
  }
  compiler.reportCodeChange();
}
 
开发者ID:andyjko,项目名称:feedlack,代码行数:18,代码来源:GoogleCodeRemoval.java

示例9: inlineConstReturn

import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
 * Replace the provided object and its method call with the tree specified
 * in returnedValue. Should be called only if the object reference has
 * no side effects.
 */
private void inlineConstReturn(Node parent, Node call,
    Node returnedValue) {
  Node retValue = returnedValue.cloneTree();
  parent.replaceChild(call, retValue);
  compiler.reportCodeChange();
}
 
开发者ID:andyjko,项目名称:feedlack,代码行数:12,代码来源:InlineGetters.java

示例10: replacePrototypeMemberDeclaration

import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
 * Replaces a member declaration to an assignment to the temp prototype
 * object.
 */
private void replacePrototypeMemberDeclaration(
    PrototypeMemberDeclaration declar) {
  // x.prototype.y = ...  ->  t.y = ...
  Node assignment = declar.node.getFirstChild();
  Node lhs = assignment.getFirstChild();
  Node name = NodeUtil.newQualifiedNameNode(
      prototypeAlias + "." + declar.memberName, declar.node,
      declar.memberName);

  // Save the full prototype path on the left hand side of the assignment
  // for debugging purposes.
  // declar.lhs = x.prototype.y so first child of the first child
  // is 'x'.
  Node accessNode = declar.lhs.getFirstChild().getFirstChild();
  Object originalName = accessNode.getProp(Node.ORIGINALNAME_PROP);

  String className = "?";

  if (originalName != null) {
    className = originalName.toString();
  }

  NodeUtil.setDebugInformation(name.getFirstChild(), lhs,
                               className + ".prototype");

  assignment.replaceChild(lhs, name);
}
 
开发者ID:andyjko,项目名称:feedlack,代码行数:32,代码来源:ExtractPrototypeMemberDeclarations.java

示例11: inlineReturnValue

import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
 * Inline a function that fulfills the requirements of
 * canInlineReferenceDirectly into the call site, replacing only the CALL
 * node.
 */
private Node inlineReturnValue(Node callNode, Node fnNode) {
  Node block = fnNode.getLastChild();
  Node callParentNode = callNode.getParent();

  // NOTE: As the normalize pass guarantees globals aren't being
  // shadowed and an expression can't introduce new names, there is
  // no need to check for conflicts.

  // Create an argName -> expression map, checking for side effects.
  Map<String, Node> argMap =
      FunctionArgumentInjector.getFunctionCallParameterMap(
          fnNode, callNode, this.safeNameIdSupplier);

  Node newExpression;
  if (!block.hasChildren()) {
    newExpression = NodeUtil.newUndefinedNode();
  } else {
    Node returnNode = block.getFirstChild();
    Preconditions.checkArgument(returnNode.getType() == Token.RETURN);

    // Clone the return node first.
    Node safeReturnNode = returnNode.cloneTree();
    Node inlineResult = FunctionArgumentInjector.inject(
        safeReturnNode, null, argMap);
    Preconditions.checkArgument(safeReturnNode == inlineResult);
    newExpression = safeReturnNode.removeFirstChild();
  }

  callParentNode.replaceChild(callNode, newExpression);
  return newExpression;
}
 
开发者ID:andyjko,项目名称:feedlack,代码行数:37,代码来源:FunctionInjector.java

示例12: visit

import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  if (n.getType() != Token.VAR) {
    return;
  }

  // It is only safe to collapse anonymous functions that appear
  // at top-level blocks.  In other cases the difference between
  // variable and function declarations can lead to problems or
  // expose subtle bugs in browser implementation as function
  // definitions are added to scopes before the start of execution.

  Node grandparent = parent.getParent();
  if (!(parent.getType() == Token.SCRIPT ||
        grandparent != null &&
        grandparent.getType() == Token.FUNCTION &&
        parent.getType() == Token.BLOCK)) {
    return;
  }

  // Need to store the next name in case the current name is removed from
  // the linked list.
  Preconditions.checkState(n.hasOneChild());
  Node name = n.getFirstChild();
  Node value = name.getFirstChild();
  if (value != null &&
      value.getType() == Token.FUNCTION &&
      !isRecursiveFunction(value)) {
    Node fnName = value.getFirstChild();
    fnName.setString(name.getString());
    NodeUtil.copyNameAnnotations(name, fnName);
    name.removeChild(value);
    parent.replaceChild(n, value);
    compiler.reportCodeChange();
  }
}
 
开发者ID:andyjko,项目名称:feedlack,代码行数:37,代码来源:CollapseAnonymousFunctions.java

示例13: convertLastReturnToStatement

import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
 * Replace the 'return' statement with its child expression.
 *   "return foo()" becomes "foo()" or "resultName = foo()"
 *   "return" is removed or becomes "resultName = void 0".
 *
 * @param block
 * @param resultName
 */
private static void convertLastReturnToStatement(
    Node block, String resultName) {
  Node ret = block.getLastChild();
  Preconditions.checkArgument(ret.getType() == Token.RETURN);
  Node resultNode = getReplacementReturnStatement(ret, resultName);

  if (resultNode == null) {
    block.removeChild(ret);
  } else {
    block.replaceChild(ret, resultNode);
  }
}
 
开发者ID:andyjko,项目名称:feedlack,代码行数:21,代码来源:FunctionToBlockMutator.java

示例14: removeVarDeclaration

import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
 * Tries to remove variable declaration if the variable has been coalesced
 * with another variable that has already been declared.
 */
private void removeVarDeclaration(Node name) {
  Node var = name.getParent();
  Node parent = var.getParent();

  // Special case when we are in FOR-IN loop.
  if (NodeUtil.isForIn(parent)) {
    var.removeChild(name);
    parent.replaceChild(var, name);
  } else if (var.getChildCount() == 1) {
    // The removal is easy when there is only one variable in the VAR node.
    if (name.hasChildren()) {
      Node value = name.removeFirstChild();
      var.removeChild(name);
      Node assign = new Node(Token.ASSIGN, name, value);

      // We don't need to wrapped it with EXPR node if it is within a FOR.
      if (parent.getType() != Token.FOR) {
        assign = NodeUtil.newExpr(assign);
      }
      parent.replaceChild(var, assign);

    } else {
      // In a FOR( ; ; ) node, we must replace it with an EMPTY or else it
      // becomes a FOR-IN node.
      NodeUtil.removeChild(parent, var);
    }
  } else {
    if (!name.hasChildren()) {
      var.removeChild(name);
    }
    // We are going to leave duplicated declaration otherwise.
  }
}
 
开发者ID:andyjko,项目名称:feedlack,代码行数:38,代码来源:CoalesceVariableNames.java

示例15: remove

import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
@Override
public void remove() {
  Node var = name.getParent();
  Preconditions.checkState(var.getFirstChild() == var.getLastChild(),
      "AST should be normalized first");
  Node parent = var.getParent();
  Node rValue = name.removeFirstChild();
  Preconditions.checkState(parent.getType() != Token.FOR);
  parent.replaceChild(var, NodeUtil.newExpr(rValue));
}
 
开发者ID:andyjko,项目名称:feedlack,代码行数:11,代码来源:DefinitionsRemover.java


注:本文中的com.google.javascript.rhino.Node.replaceChild方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。