Title: Better Separation of Crosscutting Concerns with Aspectual Components
1Better Separation of Crosscutting Concerns
withAspectual Components
- Karl Lieberherr
- College of Computer Science
- Demeter Team
- Northeastern University, Boston
- www.ccs.neu.edu/research/demeter
2Overview
- Aspect-Oriented Programming (AOP) Crosscutting
concerns, controlling tangling and scattering - Example Collect
- Aspectual Components DJ
- Comparison to AspectJ
- Summary
3Messages
- AOP is useful
- Aspectual component, an AOP mechanism, is useful
- Adaptive Programming mechanisms must be used to
properly express traversal-related aspects. Can
be done in plain Java using DJ library
4The MIT Technology Review (Jan./Feb. 2001 issue)
- Ten emerging technologies that will change the
world - Untangling Code - Aspect-Oriented Programming
(AOP) - Data Mining
- Micro Fluids
- Robot Design
- ...
5Quote MIT Technology Magazine
- The idea of aspects has been around for many
years and with many different names. It is called
adaptive programming at Northeastern University,
and subject-oriented programming at IBM,
6AOP not every concern fits into a component
crosscutting
Goal find new component structures that
encapsulate rich concerns
7AOP
- Example Logging record every operation an
application performs - When adding a new operation to this application,
always put a trace statement in - Keeping track of crosscutting concerns is
error-prone
8AOP
- Crosscutting concerns, when implemented in an
ad-hoc way, lead to a lot of tangling and
scattering in the program. - Goal of AOP is to control the tangling and
scattering.
9Scattering count number of components to which
color goes
ordinary program
better program
structure-shy functionality
CM1
Concern 1
structure
CM2
Concern 2
CM3
synchronization
Concern 3
10Aspect-Oriented ProgrammingExample
- Separating the following crosscutting concerns
- Object Structure concern (JAXB, optional package
for Java 2 platform, XML schema UML class graph) - Traversal-related concerns traverse a group of
connected objects and execute code attached to
nodes and edges of object graph (advice). - separate traversals and advice
- Those two kinds of concerns appear frequently.
11 Crosscutting in Equation System
overall graph object structure green graph
traversal purple advice
usedThings from EquationSystem through
Expression to Variable
equations
esEquationSystem
elsEquation_List
new HashSet
i1Ident
lhs
e1Equation
v1Variable
Object graph
rhs
elsExpression_List
c1Compound
i2Ident
v2Variable
a1Add
add
i3Ident
v3Variable
add
12What is a component?
- any identifiable slice of behaviour that delivers
a meaningful service, involving in general
several participants, - used via well-defined interfaces,
- formulated for an ideal ontology
- can be deployed into a concrete ontology,
- is subject to composition by 3rd parties
(black-box reuse) - is subject to refinement by 3rd parties
(white-box reuse)
13Aspectual Component
Class Graph
P1
P3
P2
Behavior Definition
P
P1
before / around
provided everything public
after
written to the CG similar to an OO
program is written to a concrete class graph
...
P3
enhancements before/around/after provided
...
before / around
after
14M1 Equation System
EquationSystem
equations
Equation_List
Ident
Variable
lhs
Equation
Numerical
rhs
Expression_List
Simple
args
Expression
op
Add
Compound
15Collect Things
System EquationSystem Definition
Equation Body Expression Thing
Variable
definedThings
System
Thing
usedThings
Definition
Body
definedThings from System bypassing Body to
Thing usedThings from System through Body
to Thing from System
constraint to Thing
16M1 Equation System
definedThings from EquationSystem bypassing
Expression to Variable
EquationSystem
equations
Equation_List
Ident
lhs
Equation
Variable
Numerical
rhs
Simple
args
Expression_List
Expression
S
T
op
Add
Compound
D
B
17M1 Equation System
usedThings from EquationSystem through
Expression to Variable
EquationSystem
equations
Equation_List
Ident
lhs
Equation
Variable
Numerical
rhs
Simple
args
Expression_List
Expression
S
T
op
Add
Compound
D
B
18Aspectual Component
- component COLLECT
- participant Source
- expected static ClassGraph cg
- public HashSet collect(String constraint)
- String id from Target to
edu.neu.ccs.demeter.Ident - Visitor v new Visitor()
- HashSet return_val new HashSet()
- void before(Target v1)
- return_val.add(cg.fetch(v1, id) )
- public Object getReturnValue()return
return_val - Source.cg.traverse(this,
- from Source constraint to Target,
v - participant Target
-
19Aspectual Component
- Find appropriate class graph and traversals for
task at hand - Simplify from graph with 11 nodes to graph with 4
nodes to graph with two nodes.
20Adapter part
- // EquationSystem class graph
- attach COLLECT
- EquationSystem Source with
- provide EquationSystemcg to
- Sourcecg
- Variable Target
-
21Related Work
- MzSchemes units Flatt/Felleisen, PLDI 1998.
Serves as foundation for a part of AC. - unit/lang Linguistic Reuse, Krishnamurthi.
- Each AC defines its own domain specific language
for describing its inputs. Adapters describe
composition of those languages.
22simulate in pure Java using DJ
- class System
- String id from Thing to edu.neu.ccs.demeter.I
dent - HashSet collect(ClassGraph cg, String
constraint) - Visitor v new Visitor()
- HashSet return_val new HashSet()
- void before(Thing v1)
- return_val.add(cg.fetch(v1, id) )
- public Object getReturnValue()return
return_val - cg.traverse(this,from Systemconstraintto
Thing, v) - return (HashSet)v.getReturnValue()
-
-
- HashSet defined(ClassGraph cg)
- return (HashSet) this.collect(cg, bypassing
Body ) - HashSet used(ClassGraph cg)
- return (HashSet) this.collect(cg, through
Body )
green traversal black bold structure purple
advice red parameters
23Ad-hoc Implementation oftraversal-related
concerns
- Leads to lots of tangled and scattered code with
numerous disadvantages - The question is not how to eliminate the tangling
but how to reduce it - AOP is about tangling control of the
implementation of crosscutting concerns - Crosscutting will always lead to some tangling at
code level
24Need more than localizationof crosscutting
concerns
- If we localize a crosscutting traversal-related
concern in the standard way, we get a method that
violates the Law of Demeter it duplicates much
class graph information - In addition Use traversal strategies to
eliminate accidental noise in class graph - Need AP to improve AOP
25AspectJ (Xerox PARC)
- A general aspect-oriented programming language
for Java. - An aspect is like a class and may contain
pointcut definitions for a set of join points and
advice saying what to do before, after or instead
of the join points.
26DJ (Northeastern)
- Is a Java package that supports AOP for
traversal-related concerns. - Integration with AspectJ both can be used
simultaneously. - DJ provides an implementation of Adaptive
Programming (AP).
27AspectJ (Xerox) DJ (NEU)
refers to existing join points
refers to new join points in traversal
- aspect SimpleTracing
- pointcut traced()
- call(void D.update()
- call(void D.repaint()
- before()traced()
- println(Entering
- thisJoinPoint)
- class Source
- HashSet collect(ClassGraph cg,
- String constraint)
- String s from Source
- constraint to Target
- HashSet result (HashSet)
- /weave/ cg.traverse(this, s,
- new Visitor()
- public void before
- (Target h)
- public void start() )
- return result
-
green pointcut purple advice
28AspectJ (Xerox) DJ (NEU)
- Abstract pointcut
- set of execution points
- where to watch
- Advice
- what to do
- Concrete pointcut
- set notation using regular expressions
- Abstract object slice
- set of entry/exit points
- where to go
- Visitor
- what to do
- Actual object slice
- traversal strategies
29Concepts needed(DJ classes)
- ClassGraph
- Strategy ( Java String traversal strategy)
- Visitor
30Adaptive Programming
Strategy
Bold names refer to DJ classes.
is use-case based abstraction of
ClassGraph
defines family of
Object
31Adaptive Programming
Strategy
defines traversals of
Object
32Adaptive Programming
Strategy
guides and informs
Visitor
33AOP AP
- Program with aspects that correspond to the
concerns of the programmer.
- Relieve the programmer from the details of some
concern. - Create robustness to changes in an aspect.
- AP is about join point reduction.
- Example structure-shyness
34Benefits of Adaptive Programming
- robustness to changes
- shorter programs
- design matches program,
- more understandable code
- partially automated evolution
- keep all benefits of OO technology
- improved productivity
Applicable to design and documentation and
implementation of your current Java systems.
35Summary
- AOP getting a lot of attention. Addresses an
important problem how to program crosscutting
concerns. - AP is about relieving the programmer from the
details of a concern traditionally from the
structural concern. - DJ supports Adaptive and Aspect-Oriented
Programming for traversal concerns in Java.
36Summary
- We view components as slices of behavior
- Aspectual components
- reconcile between functional and object
decomposition - add new behavior and modify existing behavior
- are a good model for AOP