1 / 43

CSC450 Software Engineering

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.

keene
Download Presentation

CSC450 Software Engineering

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Devon M. Simmonds University of North Carolina, Wilmington Introduction to AspectJ CSC450Software Engineering

  2. Outline • Quick review • Introduction to AspectJ • Aspects • Pointcuts • advice • Intertype declarations • Money transfer example aspect • Review & Summary

  3. A Quick Review What are Concerns? • Something that is of interest to a stakeholder, • Functional • Non-functional • Security, reliability, performance 3

  4. 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

  5. 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

  6. 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

  7. Outline • Quick review • Introduction to AspectJ • Aspects • Pointcuts • advice • Intertype declarations • Money transfer example aspect • Review & Summary

  8. AspectJ • AspectJ is an implementation of aspect-oriented programming for Java. AspectJ Java Package Pointcut Class Advice Aspect Intertype Declarations Variables Variables Methods Methods

  9. 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

  10. 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

  11. Outline • Quick review • Introduction to AspectJ • Aspects • Pointcuts • advice • Intertype declarations • Money transfer example aspect • Review & Summary

  12. 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).

  13. 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.

  14. 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.

  15. 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.

  16. 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

  17. 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));

  18. 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

  19. 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.

  20. Outline • Quick review • Introduction to AspectJ • Aspects • Pointcuts • advice • Intertype declarations • Money transfer example aspect • Review & Summary

  21. 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.

  22. 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"); } }

  23. 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.

  24. 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"); } }

  25. 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.

  26. 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"); } }

  27. 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(); } }

  28. 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.

  29. Outline • Quick review • AOSD terminology • Introduction to AspectJ • Aspects • Pointcuts • advice • Intertype declarations • Money transfer example aspect • Summary

  30. 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.

  31. Intertype Declarations • Examples • Adding variables private Address Account.address; private String MoneyTransferService.serviceName; • Adding methods public void Account.changeAddress(Address newAddress){ //… }

  32. Intertype Declarations • Changing the inheritance hierarchy declare parents: Account implements NewAccount; declare parents: StockClient extends Frame; <<Interface>> NewAccount Frame Account StockClient

  33. Outline • Quick review • Introduction to AspectJ • Aspects • Pointcuts • advice • Intertype declarations • Money transfer example aspect • Review & Summary

  34. 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)

  35. 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 ); } }

  36. 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(); } } }

  37. 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

  38. 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

  39. Outline • Quick review • Introduction to AspectJ • Aspects • Pointcuts • advice • Intertype declarations • Money transfer example aspect • Review & Summary

  40. 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.

  41. Summary • Crosscutting concerns • AOSD using AspectJ • Aspects • Pointcut, advice, intertype declaration • A complete example with compilation instructions • LINK

  42. 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

  43. Qu es ti ons? The End ______________________ Devon M. Simmonds Computer Science Department University of North Carolina Wilmington _____________________________________________________________

More Related