@Override public void replaceSuccessor(Node oldSuccessor, Node newSuccessor) { if (oldSuccessor == body) { newSuccessor.removePredecessor(oldSuccessor); newSuccessor.addPredecessor(this); body = (SequentialNode) newSuccessor; } else { super.replaceSuccessor(oldSuccessor, newSuccessor); } }
@Override public void replaceSuccessor(Node oldSuccessor, Node newSuccessor) { if (successor == oldSuccessor) { successor = null; oldSuccessor.removePredecessor(this); newSuccessor.removePredecessor(oldSuccessor); newSuccessor.addPredecessor(this); successor = newSuccessor; } }
/** * Inserts another node right before this node. * <p/> * The new node is added as a successor of all predecessors of this node and this node is set as the successor of the new * node. * * @param newNode The node that should be inserted. */ public void insertBefore(SequentialNode newNode) { newNode.setSuccessor(this); Set<Node> predecessors = new HashSet<Node>(getPredecessors()); for (Node predecessor : predecessors) { predecessor.replaceSuccessor(this, newNode); } predecessors.clear(); // previous predecessors are no predecessors anymore addPredecessor(newNode); } }
/** * Inserts a node after this sequential node and its successor. This method will rewire the nodes to keep the graph * consistent. It will do the following: * <ol> * <li>Remove the connection between this node and its successor.</li> * <li>Add a bi-directional connection between this node and the new node.</li> * <li>Add a bi-directional connection between the new node and this nodes old successor.</li> * </ol> * * @param newNode The new node that should be inserted in the control flow after this one. */ public void insertAfter(SequentialNode newNode) { Node oldSuccessor = successor; successor = newNode; newNode.addPredecessor(this); if (oldSuccessor != null) { newNode.successor = oldSuccessor; oldSuccessor.removePredecessor(this); oldSuccessor.addPredecessor(newNode); } }
@Override public void replaceSuccessor(Node oldSuccessor, Node newSuccessor) { if (oldSuccessor == positive) { newSuccessor.removePredecessor(oldSuccessor); newSuccessor.addPredecessor(this); // cast is safe since branch of if-then-else cannot be the end node setPositive((SequentialNode) newSuccessor); } else if (oldSuccessor == negative) { newSuccessor.removePredecessor(oldSuccessor); newSuccessor.addPredecessor(this); // cast is safe since branch of if-then-else cannot be the end node setNegative((SequentialNode) newSuccessor); } else { super.replaceSuccessor(oldSuccessor, newSuccessor); } }
/** * Removes the given node from the control flow graph. The graph will be traversed in order to find the node to be * removed. Once the desired node is found, its predecessors are rewired to have the old node's successor as direct * successors. * * @param node node to be removed */ public void removeNode(SequentialNode node) { Node successor = node.getSuccessor(); successor.removePredecessor(node); Set<Node> predecessors = new HashSet<Node>(node.getPredecessors()); for (Node predecessor : predecessors) { successor.addPredecessor(predecessor); predecessor.replaceSuccessor(node, successor); } }