Main {{ public static void p(String s){ System.out.print(s); } public static void main(String args[]) throws Exception { Start start = Start.parse(System.in); // some sample functionality p(start.display() + "\n"); p(start.print() + "\n"); p(start.toStr() + "\n"); System.out.println(start.equals(start)); p(start.magic().display() + "\n"); } }} Start {{ // magically replace apples by oranges and vice versa // and keep the rest of the structure the same public Start magic() { return new Traversal(new Bc(){ // B combine(Literal l){return new B(l.isPositive2());} B combine(Literal l){return new B(l.isPositive());} }). traverse(this); } }} Literal {{ boolean isPositive() { return new Traversal(new ID() { // assume one Pos in first field of Literal boolean combine(Pos pos) {return true;} // boolean combine() {return false;} boolean combine(Neg neg) {return false;} boolean combine(Literal o, boolean s) { return s; } // boolean combine(Object o, boolean s) { return s; } }, Control.bypass("gen.Literal.var")). traverse(this); // nicer: Control.strategy("Literal -> Pos") } }} Literal {{ boolean isPositive2() { return value.isPositive(); } }} Sign{{ abstract boolean isPositive(); }} Pos{{ boolean isPositive(){ return true;} }} Neg {{ boolean isPositive(){ return false;} }}