AOP with Aspect J Awais Rashid Steffen Zschaler

  • Slides: 19
Download presentation
AOP with Aspect. J Awais Rashid, Steffen Zschaler © Awais Rashid, Steffen Zschaler 2009

AOP with Aspect. J Awais Rashid, Steffen Zschaler © Awais Rashid, Steffen Zschaler 2009

Compatibility with Java • Aspect. J provides additional keywords and constructs to write aspects

Compatibility with Java • Aspect. J provides additional keywords and constructs to write aspects – Aspects operate with reference to features in standard Java code • An aspect can have ordinary Java code – Member variables and methods – Can implement interfaces – There is also aspect inheritance which we will discuss later • The Aspect. J compiler produces standard Java byte code – Can be executed on any Java virtual machine

Aspect. J and Eclipse • AJDT: Aspect. J Development Tools – An Eclipse plug-in

Aspect. J and Eclipse • AJDT: Aspect. J Development Tools – An Eclipse plug-in – Facilitates writing Aspect. J program while exploiting the powerful features of the Eclipse environment – Already installed on the lab machines

Aspect. J Join Point Model • We looked at one type of join point

Aspect. J Join Point Model • We looked at one type of join point – A Method Call • Types of join points – Call to a method or constructor – Execution of a method or constructor – Getting or setting a class field – Initialisation of a class or object – Execution of an exception handler – Others which we will not discuss

Understanding the Join Point Model (1) class Account { class Test { public static

Understanding the Join Point Model (1) class Account { class Test { public static void main(String args[ ]) { private int balance; Account a = new Account(200); int x = a. get. Balance( ); a. set. Balance(x+100); public Account(int starting. Balance) { this. balance = starting. Balance; } public void set. Balance(int new. Balance) { this. balance = new. Balance; } public int get. Balance( ) { return this. balance; } } }

Understanding the Join Point Model (2) • Let us look at the following join

Understanding the Join Point Model (2) • Let us look at the following join points in our code – Calls to methods and constructors – Execution of methods and constructors – Getting value of a field – Setting value of a field • We will not consider the other types of join points at this stage

The Call Graph in Our Example Test. main( ) Methods in Account Fields in

The Call Graph in Our Example Test. main( ) Methods in Account Fields in Account a = new Account(200); Account constructor execution balance value set start of Constructor call start of Constructor execution start of Set Field end of int x = a. get. Balance( ) execution start of Method call start of Method execution start of Get Field end of a. set. Balance(x+100) set. Balance( ) execution start of Method call start of Method execution start of Set Field end of get balance value set

Pointcut • Aspect. J construct – Specified using the pointcut keyword • Used to

Pointcut • Aspect. J construct – Specified using the pointcut keyword • Used to specify which join points are of interest – pointcut constructor. Call( ): call(Account. new(int)); – pointcut get. Balance. Method. Call( ): call(int get. Balance( )) ; – pointcut set. Balance. Field. Value( ): set(int balance); – pointcut set. Balance. Method. Execution( ): ) execution( void set. Balance(int) ;

Advice • Method like construct • Used to specify behaviour we want to execute

Advice • Method like construct • Used to specify behaviour we want to execute once a join point is matched by a specified pointcut • Three types of advice – Before – After – Around

Before Advice • Used to execute code before the code at the matched join

Before Advice • Used to execute code before the code at the matched join point executes before( ): constructor. Call( ) { System. out. println(“Constructor call about to start”); }

After Advice • Used to execute code after the code at the matched join

After Advice • Used to execute code after the code at the matched join point has executed after( ): constructor. Call( ) { System. out. println(“Constructor call has now finished”); }

Around Advice (1) • Can be used in two ways – Wrap code around

Around Advice (1) • Can be used in two ways – Wrap code around the execution of the code for the matched join point • Requires a call to special proceed method in the advice body • Can be thought of as a combined before and after advice – Circumvent the code that would otherwise have executed at the matched join point • Requires that there is no call to proceed in the advice body

Around Advice: Wrapping Around a Join Point void around( ): set. Balance. Method. Execution(

Around Advice: Wrapping Around a Join Point void around( ): set. Balance. Method. Execution( ) { System. out. println(“Who gives you extra? ”); proceed( ); System. out. println(“Howard does”); } Note the return value. Why is this needed for around advice?

Around Advice: Circumventing a Join Point void around( ): set. Balance. Method. Execution( )

Around Advice: Circumventing a Join Point void around( ): set. Balance. Method. Execution( ) { System. out. println(“I won’t let you change your balance); }

Why Distinguish Call and Execution Join Points • Allow one to specify two different

Why Distinguish Call and Execution Join Points • Allow one to specify two different temporal points during program execution – When the call happens – When the method actually executes • Aspect. J weaver requires access to your Java code – If you are using third party libraries, you do not have access to the code – call pointcut designator is useful to trap calls to methods in third party library classes – execution pointcut is useful to apply aspect behaviour with reference to methods in one’s own classes

Outline of an Aspect. J Aspect aspect My. Aspect { // pointcut definitions //

Outline of an Aspect. J Aspect aspect My. Aspect { // pointcut definitions // advices // any Java members // inter-type declarations // to be discussed later }

Using AJDT • Similar to using Eclipse to write Java code

Using AJDT • Similar to using Eclipse to write Java code

Aspect. J Hello. World • Open the Hello. World Java project in Eclipse •

Aspect. J Hello. World • Open the Hello. World Java project in Eclipse • Convert it to an Aspect. J project • Write an aspect that does the following: – Captures the call to print. Message method and prints a message before and after to confirm it has captured the call. – Note: You may want to use the call pointcut designator and before, after advice

Tracing Exercise • Open the Tracing. Example application in Eclipse • Convert it to

Tracing Exercise • Open the Tracing. Example application in Eclipse • Convert it to an Aspect. J project • Write a simple tracing aspect – Print trace messages before and after: • • The call to constructor of Account The setting of the balance field in Account The call to get. Balance() method in Account The execution of the set. Balance() method in Account