The AspectJ Programming Language Part III: Aspects and Advanced Features PowerPoint PPT Presentation

presentation player overlay
1 / 27
About This Presentation
Transcript and Presenter's Notes

Title: The AspectJ Programming Language Part III: Aspects and Advanced Features


1
The AspectJ Programming LanguagePart III
Aspects and Advanced Features
Spring 2009
  • CS 5382
  • Ramnivas Laddad, AspectJ in Action, Manning,
    2003.
  • The AspectJ Programming Guide, available from
  • http//www.eclipse.org/aspectj/doc/released/proggu
    ide/index.html

2
Outline
  • Aspect
  • Reflection
  • Aspect precedence
  • Aspect association
  • Exception softening
  • Privileged aspect

3
Aspect Syntax
  • Aspect PrivilegeOpt ModifiersOpt aspect Id
    ExtendsOpt ImplementsOpt
  • AssociationSpecificationOpt AspectBody
  • PrivilegedOpt privileged
  • ExtendsOpt extends Type
  • ImplementsOpt implements TypeList
  • AspectBody AspectBody AspectMemberDecl

4
Aspects Are Similar to Classes
  • Can include fields and methods.
  • Can have access specifications.
  • Can declare themselves to be abstract.
  • Can extend classes and abstract aspects, as well
    as implement interfaces.
  • Can be embedded inside classes and interfaces as
    nested aspects.
  • Cannot be directly instantiated.
  • Cannot inherit from concrete aspects.
  • Can be marked as privileged.

5
Abstract Aspects
To create reusable units of crosscutting
  • public abstract aspect AbstractLogging
  • public abstract pointcut logPoints()
  • public abstract Logger getLogger()
  • before() logPoints()
  • getLogger().log(Level.INFO, Before
    thisJoinPoint)

abstract pointcut
abstract method
advice to abstract pointcut
use of abstract method
6
Exercise
  • (1) Write a reusable aspect to memoize any
    functions, e.g., factorial and Fibonacci numbers.
    (Refer to the question below.)
  • (2) Write a memoization aspect for factorial
    function by reusing
  • the above aspect. To save memory, memoize only
    non-recursive calls.

7
Reflection
  • Programmatic access to static and dynamic
    information associated with join points
  • Dynamic info info that changes with each
    invocation of the same join points, e.g., this,
    target, and args.
  • Static info info that doesnt change between
    multiple executions, e.g., name and source
    location.
  • Three special objects
  • thisJoinPoint contains dynamic info of the
    advised join point
  • thisJoinPointStaticPart contains static info
    about the advised join point
  • thisEnclosingJoinPointStaticPart contains static
    info about the enclosing join point.

8
Reflective API
9
Example Tracing Aspect
  • import org.aspectj.lang.
  • Import org.aspectj.lang.reflect.
  • public aspect JointPointTracingAspect
  • private int indent -1
  • pointcut tracePoints() !within(JoinPointTraci
    ngAspect)
  • !call(.new(..)) !execution(.new(..)
    )
  • !initialization(.new(..))
    !staticinitialization()
  • before() tracingPoints()
  • ident
  • printDynamicJoinPointInfo(thisJoinPoint)
  • printStaticJoinPointInfo(thisJoinPointStaticPar
    t)
  • printStaticJoinPointInfo(thisEnclosingJoinPoint
    StaticPart)
  • after() tracingPoints()

10
Example (Cont.)
  • private void printDynamicJoinPointInfo(JoinPoin
    t jp)
  • println(This jp.getThis() Target
    jp.getTarget())
  • StringBuffer argStr new StringBuffer(Args
    )
  • Object args jp.getArgs()
  • for (int i 0 i lt args.length i)
  • argStr.append( i argsi)
  • println(argStr)
  • private void printStaticJointInfo(JoinPoint.Sta
    ticPart sp)
  • // print sp.getSignature(), sp.getKind(),
    sp.getSourceLocation()
  • private void println(Object msg)
  • for (int I 0, spaces indent 2 I lt spaces
    i)
  • System.out.println( )
  • System.out,println(msg)

11
Aspect Precedence
  • Advice in more than one aspect applicable to the
    same join point

aspect SaveEnergy before() call(void
Home.exit()) Sytem.out.println(Switching off
lights) after() call(void
Home.enter()) System.out.println(Switchi
ng on lights)
aspect HomeSecurity before() call(void
Home.exit()) Sytem.out.println(Engaging)
after() call(void Home.enter())
System.out.println(Disengaging)
Switching off lights Engaging Exiting Entering Di
sengaging Switching on lights
12
Ordering of Advice
  • The aspect with higher precedence executes its
    before advice on a join point before the one with
    lower precedence.
  • The aspect with higher precedence executes its
    after advice on a join point after the one with
    lower precedence.
  • The around advice in the higher-precedence aspect
    encloses the around advice in the
    lower-precedence.

after advice
around advice
before advice
13
Explicit Aspect Precedence
  • AspectMemberDecl PrecedenceDecl
  • PrecedenceDecl declare precedence
    TypePatternList
  • TypePatternList TypePattern TypePattern ,
    TypePatternList
  • public aspect HomeSystemCoordinationAspect
  • declare precedence HomeSecurity, SaveEnergy
  • Q. Why a separate aspect for specifying
    precedence?

14
More on Precedence
  • Inheritance and precedence
  • A sub-aspect implicitly dominates its
    super-aspect.
  • Ordering of advice in a single aspect
  • The advice that appears first lexically inside
    the aspect executes first.
  • The only way to control precedence among multiple
    advices in an aspect is to arrange them lexically

15
Exercise
  • Write outputs for java Test and java Test
    dummy.

class Test public static void main(String
args) System.out.println(Main)
private static aspect TestAspect pointcut
exe(String s) execution( main(..))
args(s) after(String s) exe(s)
System.out.println(After) void
around(String s) exe(s)
System.out.println(Around start) if
(s.length gt 0) proceed(s)
System.out.println(Around end) before(Stri
ng s) exe(s) System.out.println(Before)

16
Aspect Association
  • How many aspect instances?
  • Per virtual machine (default) issingleton()
  • Per object perthis(), pertarget()
  • Per control-flow percflow(), percflowbelow()
  • The advice defined in an aspect can run only when
    there is
  • an appropriate instance of the aspect.

17
Association Specification Syntax
  • Aspect PrivilegeOpt ModifiersOpt aspect Id
    ExtendsOpt ImplementsOpt
  • AssociationSpecificationOpt AspectBody
  • AssociationSpecificationOpt issingleton()
  • AssociationSpecifier ( Pointcut )
  • AssociationSpecifier perthis pertarget
    percflow percflowbelow

public aspect CacheAspect perthis(access())
pointcut access() execution(
banking..Account.(..)) //
18
Per-object Association
  • An aspect instance is associated with each object
    matching the association specification.
  • perthis() Associates a separate aspect instance
    with the executing object (this).
  • pertarget() Associates a separate aspect
    instance with the target object.
  • The aspects state forms a part of each executing
    (or target) objects state.

public aspect ExecutionCounterAspect
perthis(exec()) private int count pointcut
exec() execution( Point.(..))
!Point.new(..) before() exec() count
19
2
2
20
Exercise
  • Explain and compare the behaviors of following
    aspects.

aspect A1 int cnt before() call(
Point.(..)) cnt
aspect A2 static int cnt before()
call( Point.(..)) cnt
aspect A3 pertarget(calls()) int cnt
pointcut calls() call( Point.(..))
before() calls() cnt
aspect A4 pertarget(calls()) static int
cnt pointcut calls() call( Point.(..))
before() calls() cnt
21
Exercise
  • Compare the use of per-object association and
    member
  • introduction when extending object states.

aspect A1 perthis(acc()) Color color
pointcut acc() execution( Point.(..))
//
aspect A2 Color Point.color //
22
Per-control-flow Association
  • An aspect instance is associated with the control
    flow.
  • percflow() Defines a separate aspect for each
    entrance to the control flow of the join point.
  • pertcflowbelow() Defines a separate aspect for
    each entrance to the control flow below the join
    point
  • The aspects state forms a part of each control
    flows state.

public aspect ExecutionCounterAspect
percflow(exec()) pointcut exec()
execution( Point.(..)) !Point.new(..)
23
Example
setX()
setY()
24
Exception Softening
  • Java exception model
  • Checked vs. unchecked exception
  • Checked exceptions must be explicitly handled,
    e.g., by catching or by declaring in the throws
    clause.
  • Exception softening
  • Allows checked exceptions to be treated as
    unchecked ones.
  • Eliminates the need to either catch the exception
    or declare it in the callers throw clause.

25
Example
class RobustPoint private int x, y public
void setX(int x) throws CoordinateException
if (x gt 0) this.x x else throw new
CoordinateException(x) // class
Editor void moveToOrigin(Point p)
p.setX(0) //
direct subclass of exception
aspect SoftenCoordinateException declare soft
CoordinateException call() pointcut exec()
call(void setX(int))
What is wrong with this code and how to fix it in
Java?
26
Syntax and Semantics
AspectMemberDecl DeclareSoftDecl DeclareS
oftDecl declare soft TypePattern Pointcut
  • Make the joint point, if throws any exceptions of
    the given exception
  • types, behave like as if it is throwing an
    instance of
  • org.aspectj.lang.SoftException, which extends
    RuntimeException.

27
Privileged Aspects
  • Aspects often need to access private parts of
    advised classes.
  • Privileged aspects are similar to C friend
    classes.

privileged aspect PrivilegedAspect
after(Point p) call( setX(int)) target(p)
p.x p.x 10
Q Is it in general a good idea to use privileged
aspects?
Write a Comment
User Comments (0)
About PowerShow.com