aires-feb2001.ppt

Download Report

Transcript aires-feb2001.ppt

Slides for Gregor Kiczales
• Two versions
– short version: Crosscutting capabilities for Java
and AspectJ through DJ (4 viewgraphs only)
– long version: Controlling tangling and
scattering ...
5/13/2016
DJ
1
Crosscutting Capabilities for Java
and AspectJ through DJ
Demeter Team
5/13/2016
DJ
2
Class graph:
Find undefined things
System
definedThings
*
usedThings *
*
Definition
Thing
*
Body
definedThings= from System bypassing Body to Thing
usedThings = from System through Body to Thing
5/13/2016
DJ
3
Java Program: Aspectual Method
with DJ
class System{
String vi = “from Thing to edu.neu.ccs.demeter.Ident”;
void isDefined(ClassGraph cg){
isDefined is a
checkDefined(cg, getClasses(cg));}
modular unit
HashSet getClasses(ClassGraph cg){
of crosscutting
String definedThings =
implementation.
"from System bypassing Body to Thing";
Ad-hoc
Visitor v = new Visitor(){
implementation may
HashSet return_val = new HashSet();
cut across
void before(Thing v1){
100 classes.
return_val.add(cg.fetch(v1, vi) );}
public Object getReturnValue(){return return_val;}};
cg.traverse(this, definedThings, v); green: traversal
return (HashSet)v.getReturnValue();
black bold: structure
}
5/13/2016
DJ
purple: advice
red: parameters
4
Java Program: Aspectual Method
with DJ
void checkDefined(ClassGraph cg, final HashSet classHash){
String usedThings =
”from System through Body to Thing";
cg.traverse(this, usedThings, new Visitor(){
void before(Thing v){ Ident vn = cg.fetch(v, vi);
if (!classHash.contains(vn)){
System.out.println("The object "+ vn
+ " is undefined.");
}}});}
}
5/13/2016
DJ
5
What DJ adds to AspectJ
• 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. Free programmer from
detail of some aspect.
5/13/2016
DJ
6
Control Tangling and Scattering
of Class Structure, Traversals and
Traversal Advice
Feb. PI Meeting
5/13/2016
DJ
7
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.
5/13/2016
DJ
8
overall graph: object structure; green graph: traversal; purple: advice
Why crosscutting?
r=0;
Route1:BusRoute
busStops
buses
:BusList
:BusStopList
CentralSquare:BusStop
waiting
:PersonList
Bus15:DieselPowered
passengers
:PersonList
Joan:Person
Paul:Person
r++;
5/13/2016
Seema:Person
r++;
DJ
Eric:Person
9
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
5/13/2016
DJ
10
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
5/13/2016
DJ
11
Example
• Check whether all used entities are defined.
• Object structure, traversal (basically an
introduction), advice on traversal
5/13/2016
DJ
12
Find undefined things
System
definedThings
*
usedThings *
*
Definition
Thing
*
Body
definedThings= from System bypassing Body to Thing
usedThings = from System through Body to Thing
5/13/2016
DJ
13
Name map
Roles
CS1
CS2
M1
System
ClassG
Grammar
EquationSystem
Body
Body
ProductionRHS Expression
Thing
ClassName
NonTerminal
Variable
Definition
ClassDef
Production
Equation
5/13/2016
DJ
14
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).
5/13/2016
DJ
15
Collaboration with strategies
collaboration checkDef {
role System {
out isDefined(){(uses getClasses, checkDefined)};
getClasses(){(uses definedThings)};
checkDefined(){(uses usedThings)};
in definedThings =
from System bypassing Body to Thing;
in usedThings =
from System through Body to Thing; }
role Body { }
role Thing { }
role Definition { }
}
5/13/2016
DJ
16
Use of collaboration: Adapter
Need to provide the expected methods (in methods) and provide name map.
• name map:
–
–
–
–
System : EquationSystem
Definition : Equation
Body : Expression
Thing : Variable
• expected methods:
– in definedThings // use default
– in usedThings // use default
5/13/2016
DJ
17
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 aspectual
methods.
• They cut across many classes in an ad-hoc
implementation.
5/13/2016
DJ
18
Java Program: Aspectual Method
class System{
String vi = “from Thing to edu.neu.ccs.demeter.Ident”;
void isDefined(ClassGraph cg){
checkDefined(cg, getClasses(cg));}
HashSet getClasses(ClassGraph cg){
String definedThings =
"from System bypassing Body to Thing";
Visitor v = new Visitor(){
HashSet return_val = new HashSet();
void before(Thing v1){
return_val.add(cg.fetch(v1, vi) );}
public Object getReturnValue(){return return_val;}};
cg.traverse(this, definedThings, v); green: traversal
return (HashSet)v.getReturnValue();
black bold: structure
}
5/13/2016
DJ
purple: advice
red: parameters
19
Java Program: Aspectual Method
void checkDefined(ClassGraph cg, final HashSet classHash){
String usedThings =
”from System through Body to Thing";
cg.traverse(this, usedThings, new Visitor(){
void before(Thing v){ Ident vn = cg.fetch(v, vi);
if (!classHash.contains(vn)){
System.out.println("The object "+ vn
+ " is undefined.");
}}});}
}
5/13/2016
DJ
20
After applying name map
• For now we manually edit the Java
program.
5/13/2016
DJ
21
Java Program with less tangling
class EquationSystem{
String vi = “from Variable to edu.neu.ccs.demeter.Ident”;
void isDefined(ClassGraph cg){
checkDefined(cg, getClasses(cg));}
HashSet getClasses(ClassGraph cg){
String definedThings =
"from EquationSystem bypassing Expression to Variable";
Visitor v = new Visitor(){
HashSet return_val = new HashSet();
void before(Variable v1){
return_val.add(cg.fetch(v1, vi) );}
public Object getReturnValue(){return return_val;}};
cg.traverse(this, definedThings, v); green: traversal
return (HashSet)v.getReturnValue();
black bold: structure
}
5/13/2016
DJ
purple: advice
red: parameters
22
Java Program with less tangling
void checkDefined(ClassGraph cg, final HashSet classHash){
String usedThings =
”from EquationSystem through Expression to Variable";
cg.traverse(this, usedThings, new Visitor(){
void before(Variable v){ Ident vn = cg.fetch(v, vi);
if (!classHash.contains(vn)){
System.out.println("The object "+ vn
+ " is undefined.");
}}});}
}
5/13/2016
DJ
23
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.
5/13/2016
DJ
24
definedThings = from ClassG bypassing Body to ClassName
CS1: UML class diagram ClassG
0..*
Entry
EParse
entries
ClassG
BParse
ClassDef
Body
parts
Part
className
0..*
ClassName
Concrete
5/13/2016
Abstract
DJ
25
usedThings = from ClassG through Body to ClassName
CS1:UML class diagram ClassG
0..*
Entry
EParse
entries
ClassG
BParse
ClassDef
Body
parts
Part
0..*
Concrete
5/13/2016
className
ClassName
Abstract
DJ
26
Example:
x = 1.0 .
y = (+ x 4.0).
z = (* x y).
usedThings = from EquationSystem
through Expression to Variable
M1: Equation System
EquationSystem = <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.
5/13/2016
DJ
27
Example:
x = 1.0 .
y = (+ x 4.0).
z = (* x y).
definedThings= from EquationSystem
bypassing Expression to Variable
M1: Equation System
EquationSystem = <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.
5/13/2016
DJ
28
DJ
• Is a Java package that supports AOP for the
three concerns: class structure, traversals,
and traversal advice.
• Tested by 50+ users.
• Connection to AspectJ: both can be used
simultaneously.
5/13/2016
DJ
29
Concepts needed
(DJ classes)
•
•
•
•
•
ClassGraph
Strategy
ObjectGraph
ObjectGraphSlice
Visitor
5/13/2016
DJ
30
Adaptive Programming
Bold names
refer to DJ
classes.
Strategy
is use-case based
abstraction of
ClassGraph
defines family of
ObjectGraph
5/13/2016
DJ
31
Adaptive Programming
Strategy
defines traversals
of
ObjectGraph
plus Strategy
defines
ObjectGraphSlice
5/13/2016
DJ
32
Adaptive Programming
Visitor
advises
Traversal
5/13/2016
DJ
33
AspectJ (Xerox)
• Abstract pointcut
• Abstract object slice
– set of execution points
– where to watch
– set of entry/exit points
– where to go
• Advice
• Visitor
– what to do
– what to do
• Concrete pointcut
• Actual object slice
– set notation using
regular expressions
5/13/2016
DJ (NEU)
– traversal strategies
DJ
34
AOP
AP
• Program with aspects
that correspond to the
concerns of the
programmer.
5/13/2016
• Relieve the programmer
from the details of some
concern.
• Create robustness to
changes in an aspect.
• AP is about point cut
reduction.
• Example: structureshyness
DJ
35
Technology Evolution
Object-Oriented Programming
Other
Technologies
Tangled structure/behaviors
robustness to structure changes
Adaptive Programming
Tangled concerns in general
(synchronization, etc.)
Aspect-Oriented Programming
robustness to aspect changes
Aspect-Oriented Programming II
5/13/2016
DJ
36
What DJ adds to AspectJ
• 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.
5/13/2016
DJ
37