The idea is, that we add a new parse tree visitor that extends the existing visitor. The subclass writes to a buffer that contains the refactored code. Every rule is extended to first do matching for refactorings, and if it matches, apply the rewrite to the buffer.