Title: AspectJ and AP
1AspectJ and AP
2Two enhancements
- A new kind of static crosscutting
- declare traversal t strategy
- introduces a new traversal method that traverses
objects of Source(strategy) according to the
strategy - A new kind of type patterns
- Define a set of types using a strategy apply
strategy to class graph. - ContainedIn(strategy)
- ReachableThrough(strategy)
3A new kind of static crosscutting
- declare traversal t strategy
- introduces a new traversal method that traverses
objects of Source(strategy) according to the
strategy - this generalizes the AspectJ introduction Type
TypePattern.Id(Formals)Body that defines a new
method on all types in TypePattern. With the
traversal introduction the body depends on the
current type.
4Use of traversals
- The declared traversals can be invoked on objects
o by giving a visitor object as argument o.t(v). - This is similar to DemeterJ.
5Type patterns
- What is already in AspectJ
- A type pattern defines a collection of types.
- Type names are type patterns.
- A special type name picks out all types,
including primitive types. - Subtype pattern A denotes all subtypes of A.
6New kinds of type patterns Strategy type patterns
- define a set of types declaratively
- two kinds of strategy type patterns
- ContainedIn(D) selects the set of all types that
are in the scope of strategy D. - ReachableThrough(D) selects the set of all types
t from which the target of D can be reached
through the source of D and following D.
7Scope of a strategy
- The scope of a strategy modulo a class graph is
the set of types whose instances may appear on
traversal paths of objects of the class graph.
8Strategy type patterns
- Reachable(D) x there is a path in the class
graph from x to a node in ContainedIn(D) - ReachableThrough(D) xContainedIn(join(x to
Source(D), D)) is not empty.
9Examples
- For security purposes we want to advise all calls
of get methods of classes from Portfolio to
Posting. Strategy type pattern - ContainedIn(from Portfolio to Posting)
10Examples
- We want to advise all methods of types from which
Money is reachable through Retirement - ReachableThrough(from Retirement to Money)
- Reachable(Money) all types from which we can
reach Money.
11Two kinds of traversal methods
- traversals with side effects
- return type void for around methods
- traversals without side effects
- non-void return type
12Traversal strategies
- D A,B join(D1,D2) merge(D1,D2)
- We can use them in three different graphs
relevant to programming - call trees
- class graphs
- object trees
13Interpretation of traversal strategies
- D A,B join(D1,D2) merge(D1,D2)
- Source(A,B) A
- Target(A,B) B
- Source(join(D1,D2) )Source(D1)
- Target(join(D1,D2) )Target(D2)
- Source(merge(D1,D2) )Source(D1)
- Target(merge(D1,D2) )Target(D1)
14Interpretation of traversal strategies
- D A,B join(D1,D2) merge(D1,D2)
- WF(A,B) true // well-formed
- WF(join(D1,D2) )WF(D1) WF(D2) Target(D1)
Source(D2) - WF(merge(D1,D2) ) WF(D1) WF(D2)
Source(D1)Source(D2) Target(D1)Target(D2)
15Dynamic call tree
- nodes are operation calls labeled by operation
name and arguments - edges a operation calls another operation
- Path back contents of run-time stack
16Interpretation of traversal strategies
- D A,B join(D1,D2) merge(D1,D2)
- A and B are operation names
- A,B the set of B-nodes reachable from A-nodes
- join(D1,D2) the set of Target(D2)-nodes
reachable from Source(D1)-nodes following D1 and
then following D2.
17Interpretation of traversal strategies
- merge(D1,D2) the union of the set of
Target(D1)-nodes reachable from Source(D1)-nodes
following D1 and the set of Target(D2)-nodes
reachable from Source(D2)-nodes following D2.
18Translation Rules
- t(D1)
- flow(A) B
- t(D1) t(D2)
- flow(t(D1)) t(D2)
- D1
- from A to B
- merge(D1,D2)
- join(D1,D2)
rules join Target(D1) Source(D2) merge
Source(D1) Source(D2) Target(D1) Target(D2)
Source, Target definitions Source(from A to B)
A Target(from A to B) B Source(join(D1,D2)
Source(D1) Target(join(D1,D2)
Target(D2) Source(merge(D1,D2))
Source(D1) Target(merge(D1,D2)) Target(D1)
19Correspondences
- t(D1)
- flow(A) B
- flow(A)
- flow(flow(A) B) C
- flow(flow(flow(A) B) C) E
- (flow(flow(A) B1) C) (flow(flow(A)
B2) C) - t(D1) t(D2)
- flow(t(D1)) t(D2)
- flow(flow(A) B) (flow(B) C)
- flow(flow(A) B) C
- D1
- from A to B
- from A to
- from A via B to C
- from A via B via C to E
- merge(from A via B1 to C,
from A via B2 to C) - merge(D1,D2)
- join(D1,D2)
- join (from A to B, from B to C)
subset(flow(B)) flow(B) subset(flow(B))
20Class graph
- D
- A,B
- join(D1,D2)
- merge(D1,D2)
- PathSet(D)
- Paths(A,B)
- PathSet(D1).PathSet(D2)
- PathSet(D1) PathSet(D2)
we are only interested in the set of
nodes touched by the path sets -gt subgraph of
class graph
21Object tree
- subgraph of O
- subgraph of O consisting of all paths from an
A-node to a B-node, including prematurely
terminated paths.
22Object tree
- subgraph of O
- subgraph of O consisting of all paths following
D1 and those reaching Target(D1) concatenated
with all paths following D2.
23Object tree
- subgraph of O
- subgraph of O consisting of all paths following
D1 or following D2.
24Purposes of strategies
- DJ
- Traversal
- strategy graph
- class graph
- object graph
- Purposes
- select og with sg
- extract node labels
- select cg with sg
- AspectJ
- General computation
- strategy call graph
- static call graph
- dynamic call graph
- Purposes
- select dcg with sycg
- extract node labels
- select scg with sycg
25Examples
- select subgraph from call of serviceRequest to
doWork
26program without arguments
- With aspects we can avoid passing certain
arguments. - What kind of method arguments can we avoid this
way?
27addition example with dynamic call graph
- compute cost of a service request from the call
of service_request, whenever we arrive at a call
of make_connection, add 5 whenever we arrive at
a call of make_priority_connection that is not
within a call of service_reservation, add 10 - merge(from service_request to make_connection,
from service_request bypassing service_reservation
to make_priority_connection)
28How to program in DemeterJ-style in AspectJ
- Compute total salaries paid be a company from
Company.traverse to Salary.traverse - assume traverse is from Company to
- my try
- aspect Sum
- int total 0 static public int
getTotal()return total - pointcut add(Salary s) target(s)
- call(void .traverseSum(..))
- before() add(Salary s) total s.getVal()
-
- // Company c new c.traverseSum()
println(Sum.getTotal())
29How to program visitor in AspectJ
- aspect Sum issingleton // default exactly one
instance - aspect Sum
- int total 0 static public int
getTotal()return total - pointcut add(Salary s) target(s)
- call(void .traverseSum(..))
- before() add(Salary s) total s.getVal()
-
- // Company c new c.traverseSum()
println(Sum.getTotal()) - // alternative Sum sumAspectObject
Sum.aspectOf() - // println(sumAspectObject.getTotal())
- // but getTotal may no longer be static???
30Visitors in AspectJ
class Cd_graph int countInhRels()
this.allInh(new UniversalVisitor()) return
0
31Visitors in AspectJ
aspect Count static int total pointcut
init() target(Cd_graph) call(int
countInhRels()) pointcut incrementing()
target(Vertex) call( allInh_Cd_graph_trv_b
ef(UniversalVisitor)) before()init()
total0 before()counting() total int
around() init() thisJoinPoint.proceed()
return total
32aspect TraversalAdvice static int total //
accumulation pointcut init() target(Source)
call(int adaptiveMethod()) pointcut
processing() target(Target) call(
where_Source_trv_bef(UniversalVisitor))
before()init() total0 before()processing()
total int around() init()
thisJoinPoint.proceed() return total
33How to program visitor in AspectJ
- aspect Sum issingleton // default exactly one
instance - aspect Sum
- int total public int getTotal()return total
- // for setting total to 0
- pointcut init() call Company.traverse()
- pointcut add(Salary s) target(s)
- call(void .traverseSum(..))
- before init() total 0
- before add(Salary s) total s.getVal()
-
- // Company c new c.traverseSum()
- // Sum sumAspectObject Sum.aspectOf()
- // println(sumAspectObject.getTotal())
34Simulate a DJ visitor
- want aspect Id percflow(call Company.traverseSum)
- but this is not reusable
35Typed collections as an aspect
- aspect that checks that all elements of a list
belong to class fruit. Applies to a list with a
specific name. - better done through a JAXB approach.
36Our Body
- NervousSystem CentralNervousSystem
PeripheralNervousSystem. - CentralNervousSystem Brain SpinalCord.
- PeripheralNervousSystem SensoryDivision
MotorDivision. - MotorDivision SomaticNervousSystem
AutonomicNervousSystem.
37Our Body
- SomaticNervousSystem voluntary action
- AutonomicNervousSystem involuntary action
SympatheticNervousSystem ParasympatheticNervousSys
tem. - SympatheticNervousSystem fight or flight.
- ParasympatheticNervousSystem rest and digest.