import edu.neu.ccs.demeter.dj.*; import java.util.*; class A { A(B b, C c) { this.b = b; this.c = c; } B b; C c; List gather(Traversal what){ List result = what.gather(this); // System.out.println("Traversal Graph "); // System.out.println(what); return result; } } class B { B(D d) { this.d = d; } D d; } class C { C(U u) {this.u = u; } U u;} class D { D(E ei, X xi){ e = ei; x = xi;} E e; X x; } class E { E(F fi){ f = fi;} F f; } class F { F(int ii){ i = ii;} int i; } // classes U, R etc. below class Main { public static void help(ClassGraph cg, A a, A a1111, String whereToGo, Visitor v) { Traversal tg; List result1; Integer res; tg = new Traversal(whereToGo ,cg); // System.out.println(whereToGo); result1 = a.gather(tg); res = (Integer) tg.traverse(a, v); System.out.println("List size = " + result1.size() + " alt " + res.intValue()); result1 = a1111.gather(tg); res = (Integer) tg.traverse(a1111, v); System.out.println("List size (a1111) = " + result1.size() + " alt " + res.intValue()); } public static void main(String[] args) { ClassGraph cg = new ClassGraph(true,false); // constructed by reflection // System.out.println("Class graph"); // System.out.println(cg); // System.out.println("End of Class graph"); A a = new A ( new B( new D( new E( new F(7) ), new X( new F(9), null) ) ), new C(new S())); A a1111 = new A ( new B( new D( new E( new F(7) ), new X( new F(9), new D( new E(new F(27)), new X(new F(35), null) ) ) ) ), new C(new R(new X (new F(15), null), new F(20)) ));; Strategy sg = new Strategy("from A via X to F"); Traversal tg1 = new Traversal(sg, cg); tg1.traverse(a1111, new MyVisitor()); String whereToGo; Traversal tg; List result1; Integer res; UVisitor v = new UVisitor(); whereToGo = "from A via X to F"; Main.help(cg, a, a1111, whereToGo,v); // // whereToGo = "{source:A -> target:{X,B,D,C,U,R} X -> target:{D,E,F}}"; // tg = new Traversal(whereToGo ,cg); // // System.out.println(whereToGo); // result1 = a.gather(tg); // res = (Integer) cg.traverse(a, whereToGo,v); // System.out.println("List size = " + result1.size() + " alt " + // res.intValue()); // result1 = a1111.gather(tg); // res = (Integer) cg.traverse(a1111, whereToGo,v); // System.out.println("List size (a1111) = " + result1.size() + " alt " + // res.intValue()); whereToGo = "from A to F"; Main.help(cg, a, a1111, whereToGo,v); // tg = new Traversal(whereToGo ,cg); // // System.out.println(whereToGo); // result1 = a.gather(tg); // res = (Integer) cg.traverse(a, whereToGo,v); // System.out.println("List size = " + result1.size() + " alt " + // res.intValue()); // result1 = a1111.gather(tg); // res = (Integer) cg.traverse(a1111, whereToGo,v); // System.out.println("List size (a1111) = " + result1.size() + " alt " + // res.intValue()); whereToGo = "from A to S"; Main.help(cg, a, a1111, whereToGo,v); } } class UVisitor extends Visitor { int count; public void start() { count = 0; } public void before(Object host) {count++;} public Object getReturnValue() {return new Integer(count);} } class MyVisitor extends Visitor { public void start() { System.out.println("begin"); } public void finish() { System.out.println("end"); } public void before(A o) { System.out.println("before A"); } public void after(A o) { System.out.println("after A"); } public void before(B o) { System.out.println("before B"); } public void after(B o) { System.out.println("after B"); } public void before(C o) { System.out.println("before C"); } public void after(C o) { System.out.println("after C"); } public void before(D o) { System.out.println("before D"); } public void after(D o) { System.out.println("after D"); } public void before(E o) { System.out.println("before E"); } public void after(E o) { System.out.println("after E"); } public void before(F o) { System.out.println("before F"); } public void after(F o) { System.out.println("after F"); } public void before(X o) { System.out.println("before X"); } public void after(X o) { System.out.println("after X"); } } class X { X(F fi, D di){ f = fi; d = di;} F f; D d; // part d is optional } abstract class U{ F f; } class R extends U { X x; R(X x, F f) { this.x = x; this.f = f; } } class S extends U { }