Crosscutting Capabilities for Java and Aspect J through
Crosscutting Capabilities for Java and Aspect. J through DJ Demeter Team
Expanding Definitions • Strat: Literal | Intersection | Variable. – Literal = String. • Reduce to: Strat : Literal
Similar to: Expression evaluation E : S | C. S = int. C = Op <arg 1> E <arg 2> E. Op : A | M. A=. M=. Only want: E : S.
Class graph: Find undefined things System Ident defined. Things * used. Things * S T def * Definition body D Fig. UML 1 Thing * Body B defined. Things= from System bypassing Body to Thing used. Things = from System through Body to Thing
Fig. Eq 1 M 1: Equation System Equation. System equations Equation_List Ident * Variable lhs Equation Numerical rhs Expression_List Simple args * op Compound Add
Fig. G 1 CS 1: UML class diagram Grammar 0. . * Entry EParse entries Grammar BParse Production Body rhs parts 0. . * Concrete Abstract Part lhs Non. Term
YOUR PROJECT class graph
Java Program: Adaptive Method with DJ class System{ String id = “from Thing to edu. neu. ccs. demeter. Ident”; void rep. Undef(Class. Graph cg){ rep. Undef is a check. Defined(cg, get. Def. Things(cg)); } modular unit Hash. Set get. Def. Things(Class. Graph cg){ of crosscutting String defined. Things = implementation. "from System bypassing Body to Thing"; Ad-hoc Visitor v = new Visitor(){ implementation may Hash. Set return_val = new Hash. Set(); cut across void before(Thing v 1){ 100 classes. return_val. add(cg. fetch(v 1, id) ); } public Object get. Return. Value(){return_val; }}; cg. traverse(this, defined. Things, v); green: traversal return (Hash. Set)v. get. Return. Value(); black bold: structure } purple: advice red: parameters
Java Program: Adaptive Method with DJ void check. Defined(Class. Graph cg, final Hash. Set class. Hash){ String used. Things = ”from System through Body to Thing"; cg. traverse(this, used. Things, new Visitor(){ void before(Thing v){ Ident vn = cg. fetch(v, vi); if (!class. Hash. contains(vn)){ System. out. println("The object "+ vn + " is undefined. "); }}}); } }
Control Tangling and Scattering of Class Structure, Traversals and Traversal Advice
Aspect-Oriented Programming • Separating the following cross-cutting concerns: – Object Structure – Traversals through Objects – Advice on Traversals – New behaviors based on collaborating objects • Focus on those four concerns only. They appear frequently.
overall graph: object structure; green graph: traversal; purple: advice Why crosscutting? r=0; Route 1: Bus. Route bus. Stops buses : Bus. List : Bus. Stop. List Central. Square: Bus. Stop waiting : Person. List Bus 15: Diesel. Powered passengers : Person. List Joan: Person Paul: Person r++; Seema: Person r++; Eric: Person
Why aspects: Oblivious • Object Structure – does not have to know about traversals and advice on traversals • Traversals – don’t have to know about advice on traversals • Advice on Traversals – has to know minimally about object structure and traversals
Ad-hoc Implementation of three concerns • Leads to lots of tangled code with numerous disadvantages • The question is not how to eliminate the tangling but how to reduce it • AOP is about tangling control the implementation of crosscutting concerns • Crosscutting will always lead to some tangling at code level
Example • Check whether all used entities are defined. • Object structure, traversal (basically an introduction), advice on traversal
Find undefined things System defined. Things * used. Things * * Definition Thing * Body defined. Things= from System bypassing Body to Thing used. Things = from System through Body to Thing
Fig. Eq 1 M 1: Equation System Equation. System equations Equation_List Ident * Variable lhs Equation Numerical rhs Expression_List Simple args * op Compound Add
Name map Definition Class. Def Production Equation
High-level description • It is useful to have a high-level description of the collaboration besides the Java source code. Useful documentation. • Ultimately we are interested in the executable form of the collaboration (Java source code).
Collaboration with strategies collaboration check. Def { role System { out rep. Undef(){(uses get. Def. Things, check. Defined)}; get. Def. Things(){(uses defined. Things)}; check. Defined(){(uses used. Things)}; in defined. Things = from System bypassing Body to Thing; in used. Things = from System through Body to Thing; } role Body { } role Thing { } role Definition { } }
Collaboration with strategies collaboration COLLECT { role System { out Hash. Set collect(){}; defined(){(uses collect, defined. Things)}; used(){(uses collect, used. Things)}; in defined. Things = from System bypassing Body to Thing; in used. Things = from System through Body to Thing; } role Body { } role Thing { } role Definition { } }
Use of collaboration: Adapter Need to provide the expected methods (in methods) and provide name map. • name map: – – System : Equation. System Definition : Equation Body : Expression Thing : Variable • expected methods: – in defined. Things // use default – in used. Things // use default
What is an aspect? • An aspect is a modular unit of crosscutting implementation. • A Java method is a modular unit. • Can we make a Java method an aspect? • Yes, we call such methods adaptive methods. • They cut across many classes in an ad-hoc implementation.
Java Program: Adaptive Method class System{ String id = “from Thing to edu. neu. ccs. demeter. Ident”; void rep. Undef(Class. Graph cg){ check. Defined(cg, get. Def. Things(cg)); } Hash. Set get. Def. Things(Class. Graph cg){ String defined. Things = "from System bypassing Body to Thing"; Visitor v = new Visitor(){ Hash. Set return_val = new Hash. Set(); void before(Thing v 1){ return_val. add(cg. fetch(v 1, id) ); } public Object get. Return. Value(){return_val; }}; cg. traverse(this, defined. Things, v); green: traversal return (Hash. Set)v. get. Return. Value(); black bold: structure } purple: advice red: parameters
Java Program: Adaptive Method void check. Defined(Class. Graph cg, final Hash. Set class. Hash){ String used. Things = ”from System through Body to Thing"; cg. traverse(this, used. Things, new Visitor(){ void before(Thing v){ Ident vn = cg. fetch(v, id); if (!class. Hash. contains(vn)){ System. out. println("The object "+ vn + " is undefined. "); }}}); } }
After applying name map • For now we manually edit the Java program.
Java Program with less tangling class Equation. System{ String id = “from Variable to edu. neu. ccs. demeter. Ident”; void rep. Undef(Class. Graph cg){ check. Defined(cg, get. Def. Things(cg)); } Hash. Set get. Def. Things(Class. Graph cg){ String defined. Things = "from Equation. System bypassing Expression to Variable"; Visitor v = new Visitor(){ Hash. Set return_val = new Hash. Set(); void before(Variable v 1){ return_val. add(cg. fetch(v 1, id) ); } public Object get. Return. Value(){return_val; }}; cg. traverse(this, defined. Things, v); green: traversal return (Hash. Set)v. get. Return. Value(); black bold: structure } purple: advice red: parameters
Java Program with less tangling void check. Defined(Class. Graph cg, final Hash. Set class. Hash){ String used. Things = ”from Equation. System through Expression to Variable"; cg. traverse(this, used. Things, new Visitor(){ void before(Variable v){ Ident vn = cg. fetch(v, id); if (!class. Hash. contains(vn)){ System. out. println("The object "+ vn + " is undefined. "); }}}); } }
Tangling is localized Scattering eliminated • Instead of having code spread across several classes, it is localized in one class. • Java program is describing the abstract pattern behind the computation of interest: checking whether used entities are defined. • Tangling control through abstraction of patterns. We abstract away from structure.
defined. Things = from Class. G bypassing Body to Class. Name CS 1: UML class diagram Class. G 0. . * Entry EParse entries Class. G BParse Class. Def Body parts 0. . * Concrete Abstract Part class. Name Class. Name
used. Things = from Class. G through Body to Class. Name CS 1: UML class diagram Class. G 0. . * Entry EParse entries Class. G BParse Class. Def Body parts 0. . * Concrete Abstract Part class. Name Class. Name
Fig. Eq 1 M 1: Equation System Equation. System equations Equation_List Ident * Variable lhs Equation Numerical rhs Expression_List Simple args * op Compound Add
defined. Things = from Equation. System Fig. Eq 2 bypassing Expression to Variable M 1: Equation System Equation. System equations Equation_List Ident * Equation lhs Variable Numerical rhs Expression * Simple args Expression_List op Compound S T Add D B
used. Things = from Equation. System through Expression to Variable Fig. Eq 3 M 1: Equation System Equation. System equations Equation_List Ident * Equation lhs Variable Numerical rhs Expression * Simple args Expression_List op Compound S T Add D B
Fig. Eq 4 Equation System Object equations es: Equation. System els: Equation_List i 1: Ident e 1: Equation lhs rhs v 2: Variable i 2: Ident v 1: Variable
Example: x = 1. 0. y = (+ x 4. 0). z = (* x y). used. Things = from Equation. System through Expression to Variable M 1: Equation System Equation. System = <equations> List(Equation). Equation = <lhs> Variable “=“ <rhs> Expression “. ”. Variable = Ident. Expression : Simple | Compound. Simple : Variable | Numerical. Compound = “(“ Op <args> List(Expression) “)”. Op : Add | Mul. Add = “+”. Mul = “*”. Numerical = float.
Example: x = 1. 0. y = (+ x 4. 0). z = (* x y). defined. Things= from Equation. System bypassing Expression to Variable M 1: Equation System Equation. System = <equations> List(Equation). Equation = <lhs> Variable “=“ <rhs> Expression “. ”. Variable = Ident. Expression : Simple | Compound. Simple : Variable | Numerical. Compound = “(“ Op <args> List(Expression) “)”. Op : Add | Mul. Add = “+”. Mul = “*”. Numerical = float.
Fig. G 1 CS 1: UML class diagram Grammar 0. . * Entry EParse entries Grammar BParse Production Body rhs parts 0. . * Concrete Abstract Part lhs Non. Term
defined. Things = from Grammar bypassing Body to Non. Term Fig. G 2 CS 1: UML class diagram Grammar 0. . * Entry EParse entries Grammar BParse Production Body rhs parts 0. . * Part lhs Non. Term S Concrete Abstract T D B
used. Things = from Grammar through Body to Non. Term Fig. G 3 CS 1: UML class diagram Grammar 0. . * Entry EParse entries Grammar BParse Production Body rhs parts 0. . * Part lhs Non. Term S Concrete Abstract T D B
What DJ adds to Aspect. J • Point cut definitions based on connectivity in class graph. • Define point cuts by specifying a (traversal) program based on class graph. • Point cut reduction (high-level point cut designator): free programmer from details of class graph.
Context Switch • Textbook and project: – Chapter 7: Before The Project – Chapter 6: While You are Coding
- Slides: 42