Expr implements IAstNode { @Override public void replaceChild(IAstNode child, IAstNode newChild) { if (!(child instanceof Expr && newChild instanceof Expr)) { throw new IllegalArgumentException(); } for (int i = 0; i < getNumChildren(); i++) { if (getChild(i) == child) { setChild(i, (Expr) newChild); return; } } throw new ChildDoesNotExistException(child, this); }  @Override abstract Expr clone(); abstract Expr getChild(int index); abstract void setChild(int index, Expr expr); abstract int getNumChildren(); @Override void replaceChild(IAstNode child, IAstNode newChild); abstract boolean hasChild(IAstNode child); static void checkNoTopLevelCommaExpression(List<Expr> args);  }
@Test public void replaceChild() throws Exception { VariableIdentifierExpr v = new VariableIdentifierExpr("v"); VariableIdentifierExpr w = new VariableIdentifierExpr("v"); Expr pe = new ParenExpr(v); assertEquals(v, pe.getChild(0)); pe.replaceChild(v, w); assertEquals(w, pe.getChild(0)); }
@Test(expected = ChildDoesNotExistException.class) public void replaceChildBad1() throws Exception { VariableIdentifierExpr v = new VariableIdentifierExpr("v"); VariableIdentifierExpr w = new VariableIdentifierExpr("v"); Expr pe = new ParenExpr(v); pe.replaceChild(w, v); }
@Test(expected = IllegalArgumentException.class) public void replaceChildBad2() throws Exception { VariableIdentifierExpr v = new VariableIdentifierExpr("v"); Expr pe = new ParenExpr(v); pe.replaceChild(v, new NullStmt()); }