Integrating Demeter into Aspect J using AspectOriented Programming

  • Slides: 16
Download presentation
Integrating Demeter into Aspect. J using Aspect-Oriented Programming Paul Freeman COM 3362 Final Homework

Integrating Demeter into Aspect. J using Aspect-Oriented Programming Paul Freeman COM 3362 Final Homework Review

Problem Posed PARTS 1 & 2: 1. Produce the class graph cg from a

Problem Posed PARTS 1 & 2: 1. Produce the class graph cg from a Java program. 2. Read in a set of declared traversal strategies from an Aspect file (Traversals. java): declare traversal t 1( ) : "from Basket to *"; 3. Produce a traversal graph from each traversal strategy. 4. Using the interface of the AP Library available in DJ, generate an Aspect that contains the traversal code for each traversal graph (Traversals. Implementation. java). 4. Generate an Aspect containing test code that will test your traversal code (Traversals. Test. java).

Problem Posed PART 3: Add a visitor declaration language to the traversal language: declare

Problem Posed PART 3: Add a visitor declaration language to the traversal language: declare visitors : Visitor 1 Interface; declare traversal t 1(Visitor 1 Interface) : "from Basket to *"; Defining the visitor methods in an interface (including edge and around methods): interface Visitor 1 Interface { void before(A a); void after(B host); void after(C host); void after_e(A host, B dest); void around(C host, Subtraversal st); void start(); void finish(); }

Problem Posed PART 3 (cont. ): Aspect. J should be used as follows: ajc

Problem Posed PART 3 (cont. ): Aspect. J should be used as follows: ajc *. java Main > traversals. java. . . ajc *. java Main. . . // first compilation // first run (stopped prematurely) // second compilation // second run where: first compilation: - weaves the aspects that capture information about the user’s program into the user’s program itself. first run (of the user’s program is stopped prematurely): - capture the classgraph, traversal graphs, and visitor interfaces - produce the files Traversals. Implementation. java, Traversals. Test. java second compilation: - recompile the user’s program, weaving in the “first run” generated Traversal aspects second run: - the actual run of the user’s program with the incorporated traversal code

Common Solutions Capture of Strategies, Traversal Graphs, and Elements: Methods used: 1. Class. Graph

Common Solutions Capture of Strategies, Traversal Graphs, and Elements: Methods used: 1. Class. Graph captured around a call to main(String[ ] args) 2. Strategies captured at parse of traversal file and stored in an intermediate data structure 3. Each Strategy was used to create a Traversal. Graph 2. node. Sets removed from the TG using the AP Library 3. the outgoing. Edge. Sets were removed from each node 4. Each outgoing. Edge. Set was examined for the various edge types: - construction edge - inheritance edge - alternation edge 5. the Visitor interfaces are examined for the methods declared.

Common Solutions Code Generation: Methods used: 1. The structures used to hold the various

Common Solutions Code Generation: Methods used: 1. The structures used to hold the various elements were then traversed to produce the traversal code. 2. Test code was generated based on the Traversal Graphs constructed and the names of the traversals read from the traversal declaration file.

What was learned Integration of Demeter into Aspect. J: Production of the traversal code

What was learned Integration of Demeter into Aspect. J: Production of the traversal code itself was done in a common fashion, but methods used to capture and execute visitor advice varied. Integration is possible but non-trivial. In particular, implementation of the visitor methods, ‘around(Host, Subtraversal)’ and ‘around_e(Source, Destination, Subtraversal)’ are difficult to implement. This is due to the fact that the user writes the body of the advice declared in the visitor interface. And that there is possibility that a call of Subtraversal. apply() is made, but it is not required. If apply() is not called from within the visitor’s around advice, the traversal should not continue. However if apply is called, the traversal should continue down the traversal path chosen, i. e. apply(“edge_label”) or apply() could be called, OR no apply could be called at all. (example on next slide)

What was learned Integration of Demeter into Aspect. J (example of major problem): interface

What was learned Integration of Demeter into Aspect. J (example of major problem): interface Visitor 1{ around(Basket b, Subtraversal st); } class My. Visitor implements Visitor 1{ around(Basket b, Subtraversal st){ if( path. equals. Ignore. Case(“All”) ){ st. apply(); } else if( path. equals. Ignore. Case(“Fruit”) ){ st. apply(“f”); } else if( path. equals. Ignore. Case(“Pencil”) ){ st. apply(“p”); } else{ /* do nothing */ } } }

Summary of Solutions Execution of Visitor advice: Two different camps: Execution of visitor advice

Summary of Solutions Execution of Visitor advice: Two different camps: Execution of visitor advice based on join points within the traversal. Insertion of visitor advice method calls into the traversal code at the appropriate locations.

Summary of Solutions Execution of visitor advice based on join points within the traversal

Summary of Solutions Execution of visitor advice based on join points within the traversal aspect Traversals. Implementation { void Basket. t 2(Visitor 2 vt 2) { if (f != null) t 2_f(vt 2); } void Basket. t 2_f(Visitor 2 vt 2) {f. t 2(vt 2); } void Fruit. t 2(Visitor 2 vt 2) { if (w != null) t 2_w(vt 2); } void Fruit. t 2_w(Visitor 2 vt 2) {w. t 2(vt 2); }. . . } :

Summary of Solutions Execution of visitor advice based on join points within the traversal

Summary of Solutions Execution of visitor advice based on join points within the traversal (cont. ): Class My. Visitor implements Visitor 2{ public void start( ){ System. out. println(“Visitor 2: public void start( )”); public void before(Basket basket 0){ System. out. println("Visitor 2: public void before(Basket basket 0)"); } } aspect Traversals. Test { pointcut visiting_Basket_t 2(Basket _basket, Visitor 2 my. V_visitor 2 ) : call(void t 2(Visitor 2) ) && target(_basket) && args(my. V_visitor 2); before (Basket _basket, Visitor 2 my. V_visitor 2) : visiting_Basket_t 2(_basket, my. V_visitor 2) { my. V_visitor 2. start(); my. V_visitor 2. before(_basket); } }

Summary of Solutions Insertion of visitor advice method calls into the traversal code: aspect

Summary of Solutions Insertion of visitor advice method calls into the traversal code: aspect Traversals. Implementation { void Basket. t 3(Visitor 1 v 1){ v 1. start(); v 1. before(this); if(f != null) t 3_f(v 1); v 1. finish(); } void Basket. t 3_f(Visitor 1 v 1){ f. t 3(v 1); }. . . }

Summary of Solutions Insertion of visitor advice method calls into the traversal code: class

Summary of Solutions Insertion of visitor advice method calls into the traversal code: class My. Visitor implements Visitor 1{ public void before(Basket host){ System. out. println(“Visitor 1: public void before(Basket host)”); } … }

Summary of Solutions Insertion of visitor advice method calls into the traversal code (around()’s):

Summary of Solutions Insertion of visitor advice method calls into the traversal code (around()’s): aspect Traversals. Implementation { void Basket. t 3(Visitor 1 v 1){ v 1. start(); v 1. around(this, new Subtraversal()); v 1. finish(); } void Basket. t 3_f(Visitor 1 v 1){f. t 3(v 1); } void Basket. subtraversal(Visitor 1 v 1){ if(f != null) t 3_f(v 1); } void Basket. subtraversal_f(Visitor 1 v 1){ if(f != null) t 3_f(v 1); } pointcut Basket_apply(Basket host, Visitor 1 v 1): call(void Subtraversal. apply(Object)) && this(v 1) && args(host); void around(Basket host, Visitor 1 v 1): Basket_apply(host, v 1) { host. subtraversal(v 1); } pointcut Basket_apply. On. Edge(Basket host, Visitor 1 v 1, String edge): call(void Subtraversal. apply(Object, String)) && this(v 1) && args(host, edge); void around(Basket host, Visitor 1 v 1, String edge): Basket_apply. On. Edge(host, v 1, edge) { if(edge. equals("f")){ host. subtraversal_f(v 1); } }. . . }

Summary of Solutions Insertion of visitor advice method calls into the traversal code (around()’s):

Summary of Solutions Insertion of visitor advice method calls into the traversal code (around()’s): class My. Visitor implements Visitor 1{ public void around(Basket host, Subtraversal st){ st. apply(host); } … }

Suggestions for future exercises: Next Step: Implementation of around methods that return values other

Suggestions for future exercises: Next Step: Implementation of around methods that return values other than void. Implementation of a functional visitor. Implementation of multiple visitor traversals where the visitors can communicate.