430 likes | 597 Views
Devon M. Simmonds University of North Carolina, Wilmington. Introduction to AspectJ. CSC450 Software Engineering. Outline. Quick review Introduction to AspectJ Aspects Pointcuts advice Intertype declarations Money transfer example aspect Review & Summary. A Quick Review.
E N D
Devon M. Simmonds University of North Carolina, Wilmington Introduction to AspectJ CSC450Software Engineering
Outline • Quick review • Introduction to AspectJ • Aspects • Pointcuts • advice • Intertype declarations • Money transfer example aspect • Review & Summary
A Quick Review What are Concerns? • Something that is of interest to a stakeholder, • Functional • Non-functional • Security, reliability, performance 3
A Quick Review Cross-cutting concerns • Cross-cutting concerns are concerns whose implementation cuts across a number of program components. • Scattering – code for one feature appears in multiple modules • Tangling – code in one module addresses multiple concerns
A Quick Review Cross-cutting concerns logging is not modularized • Cross-cutting concerns are concerns whose implementation cuts across a number of program components. • Scattering – code for one feature appears in multiple modules • Tangling – code in one module addresses multiple concerns
Aspect-Oriented Software Development (AOSD) • Aspect-oriented software development (AOSD) is an approach to software development that supports the systematic identification, separation, representation and composition of crosscutting concerns. • In AOSD crosscutting concerns are encapsulated in modules known as aspects. 6
Outline • Quick review • Introduction to AspectJ • Aspects • Pointcuts • advice • Intertype declarations • Money transfer example aspect • Review & Summary
AspectJ • AspectJ is an implementation of aspect-oriented programming for Java. AspectJ Java Package Pointcut Class Advice Aspect Intertype Declarations Variables Variables Methods Methods
Java • AspectJ non-Java concepts • Pointcut- An AspectJ statement that identifies join points and values at those points. • Join point - a well-defined point in the program flow, e.g., a method call. • Advice - A piece of code that is executed when a join point is reached. • Inter-type declarations – AspectJ statements for modifying a program's static structure, namely, the members of its classes and the relationship between classes. • Aspect - The AspectJ unit of modularity for crosscutting concerns and may also include pointcuts, advice and inter-type declarations. Aspect AspectJ Package Pointcut Class Advice Intertype Declarations Variables Variables Methods Methods
The HelloWorld Aspect public class Test { public void helloWorld() { System.out.println(“Hello World”); } public static void main(String[] args){ Test test = new Test(); test.helloWorld(); } }//end of class public aspect TestAspect { pointcutoutputLog() : call(public void helloWorld() ); before() : outputLog() { System.out.println(“before call”); } }//end of aspect
Outline • Quick review • Introduction to AspectJ • Aspects • Pointcuts • advice • Intertype declarations • Money transfer example aspect • Review & Summary
Specifying AspectJ Join Points • Method call join point: call(MethodPattern) • Picks out each method call join point whose signature matches MethodPattern, e.g. call(public void helloWorld()). • Method execution join point: execution(MethodPattern) • Picks out each method execution join point whose signature matches MethodPattern, e.g., execution(public void helloWorld()). • Variable reference join point: get(FieldPattern) • Picks out each field reference join point whose signature matches FieldPattern, e.g., get(double balance). • Variable set join point: set(FieldPattern) • Picks out each field set join point whose signature matches FieldPattern, e.g., set(double balance).
Some AspectJ Pointcuts • Variable use join point: withincode(MethodPattern) • Picks out each join point where the executing code is defined in a method whose signature matches MethodPattern, e.g., withincode(public void deposit(double)). • Variable use join point: cflow(Pointcut) • Picks out each join point in the control flow of any join point P picked out by Pointcut, including P itself, e.g., cflow(P1 ) || cflow(P2)). • etc.
Join Point States • Each join point potentially has three pieces of state associated with it: • the currently executing object • the target object • an object array of arguments. • These are exposed by the three state-exposing pointcuts: • this, target, and args, respectively.
Pointcuts for Join Point States • this(Type or Id) • Picks out each join point where the currently executing object (the object bound to this) is an instance of Type, or of the type of the identifier • target(Type or Id) • Picks out each join point where the target object (the object on which a call or field operation is applied to) is an instance of Type, or of the type of the identifier Id . • args(Type or Id, ...) • Picks out each join point where the arguments are instances of the appropriate type (or type of the identifier if using that form). Pointcuts may be composed with boolean operators (&&, ||, !) to build up other pointcuts.
Example 1: A FigureElement UML Diagram FigureElement <<Factory>> Figure Display • Identify each point where the setX method in Point is called. • call(void Point.setX(int)) • Identify each point where the setX method in Point is executed. • execution(void Point.setX(int)) • What join points does this statement pick out? • call(void Point.setX(int)) || call(void Point.setY(int)) +setXY(int, int) +setX(int) +setY(int) +draw() +makePoint () +makeLine() Point Line -x: int -y: int -p1: Point -p2: Point
Pointcut Example • General pointcut format: • pointcutpointcut-name (parameters) :designator ; • pointcut move(): call(void FigureElement.setXY(int,int)); • pointcut move(): call(void FigureElement.setXY(int,int)) || call(void Point.setX(int)) || call(void Point.setY(int));
Aspect with Pointcut • aspect TestPointcut { pointcut move(): call(void FigureElement.setXY(int,int)) || call(void Point.setX(int)) || call(void Point.setY(int)); } Name-based crosscutting
Property-based Pointcuts • AspectJ also provides mechanisms that enable specifying a pointcut in terms of properties of methods other than their exact name. • E.g. using wildcards in certain fields of the method signature. • call(void Figure.make*(..)) • picks out calls to the factory methods makePoint and makeLine.
Outline • Quick review • Introduction to AspectJ • Aspects • Pointcuts • advice • Intertype declarations • Money transfer example aspect • Review & Summary
Advice • Pointcutsonly pick out join points. To actually implement crosscutting behavior, we use advice. • Advice brings together a pointcut (to pick out join points) and a body of code (to run at each of those join points). • Advice in AspectJ • Before advice • runs as a join point is reached, before the program proceeds with the join point. For example, before advice on a method call join point runs before the actual method starts running, just after the arguments to the method call are evaluated.
Aspect with before Advice • aspect TestPointcut { pointcutmove(): call(void FigureElement.setXY(int,int)) || call(void Point.setX(int)) || call(void Point.setY(int)) || call(void Line.setP1(Point)) || call(void Line.setP2(Point)); before(): move() { System.out.println("about to move"); } }
After Advice • After advice runs after code at the join point . • After advice on a method call join point runs after the method body has run, just before control is returned to the caller.
Aspect with before Advice • aspect TestPointcut { pointcutmove(): call(void FigureElement.setXY(int,int)) || call(void Point.setX(int)) || call(void Point.setY(int)); after(): move() { System.out.println("just successfully moved"); } }
Around Advice • Around advice on a join point runs as the join point is reached, and has explicit control over whether the program proceeds with the join point.
Aspect with around Advice • aspect TestPointcut { pointcutmove(): call(void FigureElement.setXY(int,int)) || call(void Point.setX(int)) || call(void Point.setY(int)) || call(void Line.setP1(Point)) || call(void Line.setP2(Point)); void around(): move() { System.out.println(“no method executed"); } }
Aspect with around Advice • aspect TestPointcut { pointcutmove(): call(void FigureElement.setXY(int,int)) || call(void Point.setX(int)) || call(void Point.setY(int)) || call(void Line.setP1(Point)) || call(void Line.setP2(Point)); void around(): move() { //do something proceed(); } }
Join Point Context • A pointcut declaration has a parameter list that can be used to expose variables defined at the join points. • Values exposed by a pointcut can be used in the body of advice declarations. • An advice declaration has a parameter list (like a method) that gives names to all the pieces of context that it uses.
Outline • Quick review • AOSD terminology • Introduction to AspectJ • Aspects • Pointcuts • advice • Intertype declarations • Money transfer example aspect • Summary
Intertype Declarations • AspectJ's mechanisms for modifying classes and their hierarchy by: • introducing or inserting new code into a target class of an application, • changing the inheritance or interface hierarchy of a class or collection of classes. • Compared to advice which is generally dynamic, introductions are static and are executed at compile time.
Intertype Declarations • Examples • Adding variables private Address Account.address; private String MoneyTransferService.serviceName; • Adding methods public void Account.changeAddress(Address newAddress){ //… }
Intertype Declarations • Changing the inheritance hierarchy declare parents: Account implements NewAccount; declare parents: StockClient extends Frame; <<Interface>> NewAccount Frame Account StockClient
Outline • Quick review • Introduction to AspectJ • Aspects • Pointcuts • advice • Intertype declarations • Money transfer example aspect • Review & Summary
A Money Transfer Example Client • Problems: • Initiate a transaction each time a money transfer begins. Close the transaction when the money transfer ends. • Problem 2: Cancel the transaction and charge a fee of 1% of the transfer amount, if the source and target accounts of a money transfer are the same Account deposit(double amt) withdraw(double amt) 1..* 1..* requests manages MoneyTransferService 1 1..* moneyTransfer(a:Account, b:Account, amount:double) Info(info:String)
MoneyTransferService.java package MoneyTransfer; import java.lang.*; import java.io.*; public class MoneyTransferService { Account acc1, acc2; public MoneyTransferService () { System.out.println("\nMoneyTransferService object created"); } public void moneyTransfer (Account a1, Account a2, double amount) { a1.withdraw(amount); a2.deposit(amount); } public void info (String str) { System.out.println( "MoneyTransferService: " + str ); } }
Client.java package MoneyTransfer; import java.lang.*; import java.io.*; public class Client { public static void main(String[] args) { Account acc1, acc2; try { MoneyTransferService service = new MoneyTransferService(); acc1=new Account(“Tammy", 12345, 400); acc2=new Account(“Jason", 12346, 375); service.moneyTransfer(acc1, acc2, 200); acc1.deposit(444); } catch(Exception e) { e.printStackTrace(); } } }
MoneyTransferServiceAspect.java package MoneyTransfer; import java.lang.*; import java.io.*; import java.util.*; public aspect MoneyTransferServiceAspect { //Problem 1: Initiate transaction when money transfer begins / Close transaction when money transfer ends. pointcut manageTransaction(MoneyTransferService s, Account acc1, Account acc2, double amount): execution(public void moneyTransfer(Account, Account, double)) && target(s) && args(acc1,acc2,amount); before (MoneyTransferService s, Account acc1, Account acc2, double amount): manageTransaction(s,acc1,acc2,amount) { try{ s.info("Transaction initiated..."); }catch(Exception e){} } after(MoneyTransferService s, Account acc1, Account acc2, double amount): manageTransaction(s,acc1,acc2,amount) { try{ s.info("Transaction committed..."); }catch(Exception e){} } }//end of aspect
MoneyTransferServiceAspect.java package MoneyTransfer; import java.lang.*; import java.io.*; import java.util.*; public aspect MoneyTransferServiceAspect { //Problem 2: Cancel the transaction and charge a fee of 1% of the transfer amount, if the source and target accounts of a money transfer are the same pointcut cancelTransaction(MoneyTransferService s, Account a1, Account a2, double amount): execution(public void moneyTransfer(Account, Account, double)) && target(s) && args(a1,a2,amount); void around(MoneyTransferService s, Account a1, Account a2, double amount): cancelTransaction(s,a1,a2,amount) { try{ if(a1.getAccNumber() == a2.getAccNumber()) { System.out.println("=== Calcellation fee will be charged ..."); a1.withdraw(amount*0.01); } else proceed(s,a1,a2,amount); }catch(Exception e){} }}//end of aspect
Outline • Quick review • Introduction to AspectJ • Aspects • Pointcuts • advice • Intertype declarations • Money transfer example aspect • Review & Summary
Review: AOSD Terminology • Aspect – • a program abstraction that encapsulates a crosscutting concern. • Join point – • an event in an executing program where crosscuting code may be executed. • Advice – • the (crosscutting) code executed at a join point • Pointcut – • a statement included in an aspect that defines the join points where the associated aspect advice should be executed. • Join point model – • the set of events that may be referenced in a pointcut. • Weaving – • incorporating aspect advice at specified join points by an aspect weaver.
Summary • Crosscutting concerns • AOSD using AspectJ • Aspects • Pointcut, advice, intertype declaration • A complete example with compilation instructions • LINK
Resources • The AspectJ web site is: http://www.eclipse.org/aspectj/ • You can download the latest stable release of AspectJ 5 (1.5.3) from http://www.eclipse.org/aspectj/downloads.php • You can find information about the language in the Programmers Guide: http://www.eclipse.org/aspectj/doc/released/progguide/index.html • The Quick Reference is also an excellent guide to the language syntax: http://www.eclipse.org/aspectj/doc/released/quick5.pdf • Compilation instructions may be found in the Development Environment Guide and the ajccompiler page: http://www.eclipse.org/aspectj/doc/released/devguide/ajc-ref.html
Qu es ti ons? The End ______________________ Devon M. Simmonds Computer Science Department University of North Carolina Wilmington _____________________________________________________________