P9: Creating and Removing Visitors


We discussed the Visitor design pattern in class; it is a sequence of refactorings.  The "Inverse Visitor" removes a Visitor and restores visit methods to their original classes with their original names.  It is the inverse mapping of the Visitor design pattern/refactoring.

Creating a Visitor or undoing a Visitor are tasks that you really want to have IDE help.  The obvious reason is that when you move a method, you have to adjust all of its references to preserve semantics, etc.  Here's a simple example.  Here's class A:
class A {
private int x, y;

public int add() { return x+y; }

public void execute() { add(); }
}
After moving add() to a Visitor, notice the changes that are made to A:
class Visitor {
public static final Visitor instance = new Visitor();

public int visit(A a) { return a.x + b.y; } // References are now indirect
}

class A {
public int x, y; // Referenced fields become public

public int add(Visitor v) { return v.visit(this); } // Method add invokes visit and change signature

public void execute() { add(Visitor.instance); } // References of Method add change their signatures
}
It is really easy to make mistakes when moving methods.  Letting a tool do it for you will eliminate a lot of problems.

We have found that creating or undoing a Visitor manually, even if it is for just 7-8 methods, is error prone.  Consequently, very few people attempt to create a Visitor manually.  Often it is the case that code generators create the scaffolding for Visitors, and users graft on their Visitors (which is easy enough).
In this assignment, you are to write R3 scripts to create and undo a Visitor.   Your scripts will be at most 20 LOC, so it is not that hard.  But they should work for Visitors of any size (i.e., that move any number of methods).  We will compare your results with that from a graduate class at Oregon State University where we posed the same tasks (about 6 weeks ago) that you are about to undertake in this assignment.


Your Assignment

You are to work alone. We will be using the same groups as before, and there are 2 parts to this assignment.  See comments on Canvas H5 to find the first part of this 2-part assignment.  Contact jongwook@cs.utexas.edu if you cannot find this information.

Work alone in this assignment -- NO GROUP PROJECTS PERMITTED.

We want your honest answers -- so if you discover that you made a mistake in part 1, that's OK.  Don't resubmit.  You won't be penalized for honest errors.  You WILL be penalized if you resubmit solutions.

No late assignments/submissions will be accepted.