Download presentation
Presentation is loading. Please wait.
Published byOwen Carr Modified over 9 years ago
1
CSC450 Software Engineering Devon M. Simmonds University of North Carolina, Wilmington 1
2
Outline Quick review Introduction to AspectJ –Aspects –Pointcuts –advice –Intertype declarations Money transfer example aspect Review & Summary 2
3
3 Something that is of interest to a stakeholder, – Functional – Non-functional Security, reliability, performance What are Concerns? 3 A Quick Review
4
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 4 A Quick Review
5
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 logging is not modularized
6
6 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. Aspect-Oriented Software Development (AOSD) 6
7
Outline Quick review Introduction to AspectJ –Aspects –Pointcuts –advice –Intertype declarations Money transfer example aspect Review & Summary 7
8
AspectJ AspectJ is an implementation of aspect-oriented programming for Java. 8 Class MethodsVariables Package Aspect MethodsVariables Advice Intertype Declarations Pointcut Java
9
AspectJ 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. 9 Class MethodsVariables Package Aspect MethodsVariables Advice Intertype Declarations Pointcut Java
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 { pointcut outputLog() : call(public void helloWorld() ); before() : outputLog() { System.out.println(“before call”); } }//end of aspect 10
11
Outline Quick review Introduction to AspectJ –Aspects –Pointcuts –advice –Intertype declarations Money transfer example aspect Review & Summary 11
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). 12
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. 13
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. 14
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. 15
16
Example 1: A FigureElement UML Diagram DisplayFigureElement +setXY(int, int) +setX(int) +setY(int) +draw() > Figure +makePoint () +makeLine() Line -x: int -y: int Point -p1: Point -p2: Point 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)) 16
17
Pointcut Example General pointcut format: – pointcut pointcut-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)); 17
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 18
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. 19
20
Outline Quick review Introduction to AspectJ –Aspects –Pointcuts –advice –Intertype declarations Money transfer example aspect Review & Summary 20
21
Advice Pointcuts only 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. 21
22
Aspect with before Advice aspect TestPointcut { pointcut move(): 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"); } } 22
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. 23
24
Aspect with before Advice aspect TestPointcut { pointcut move(): call(void FigureElement.setXY(int,int)) || call(void Point.setX(int)) || call(void Point.setY(int)); after(): move() { System.out.println("just successfully moved"); } } 24
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. 25
26
Aspect with around Advice aspect TestPointcut { pointcut move(): 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"); } } 26
27
Aspect with around Advice aspect TestPointcut { pointcut move(): 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(); } } 27
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. 28
29
Outline Quick review AOSD terminology Introduction to AspectJ –Aspects –Pointcuts –advice –Intertype declarations Money transfer example aspect Summary 29
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. 30 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){ //… } 31
32
Intertype Declarations Changing the inheritance hierarchy declare parents: Account implements NewAccount; declare parents: StockClient extends Frame; 32 > NewAccount > NewAccount Account Frame StockClient
33
Outline Quick review Introduction to AspectJ –Aspects –Pointcuts –advice –Intertype declarations Money transfer example aspect Review & Summary 33
34
A Money Transfer Example Problems: 1.Initiate a transaction each time a money transfer begins. Close the transaction when the money transfer ends. 2.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 Client Account deposit(double amt) withdraw(double amt) MoneyTransferService moneyTransfer(a:Account, b:Account, amount:double) Info(info:String) requests manages 1..* 1 34
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 ); } 35
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(“Patrick", 12345, 400); acc2=new Account(“Corey", 12346, 375); service.moneyTransfer(acc1, acc2, 200); acc1.deposit(444); } catch(Exception e) { e.printStackTrace(); } } 36
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 37
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 38
39
Outline Quick review Introduction to AspectJ –Aspects –Pointcuts –advice –Intertype declarations Money transfer example aspect Review & Summary 39
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. 40
41
Summary Crosscutting concerns AOSD using AspectJ – Aspects Pointcut, advice, intertype declaration A complete example with compilation instructions – LINK LINK 41
42
Resources The AspectJ web site is: http://www.eclipse.org/aspectj/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 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 You can find information about the language in the Programmers Guide: http://www.eclipse.org/aspectj/doc/released/progguide/index.html 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 http://www.eclipse.org/aspectj/doc/released/quick5.pdf Compilation instructions may be found in the Development Environment Guide and the ajc compiler page: http://www.eclipse.org/aspectj/doc/released/devguide/ajc-ref.htmlDevelopment Environment Guide http://www.eclipse.org/aspectj/doc/released/devguide/ajc-ref.html 42
43
The End 43 ______________________ Devon M. Simmonds Computer Science Department University of North Carolina Wilmington _____________________________________________________________ Qu es ti ons?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.