43
CSC450 Software Engineering Devon M. Simmonds University of North Carolina, Wilmington Introduction to AspectJ 1

CSC450 Software Engineering

  • Upload
    keene

  • View
    50

  • Download
    0

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Page 1: CSC450 Software Engineering

CSC450Software Engineering

Devon M. SimmondsUniversity of North Carolina, Wilmington

Introduction to AspectJ

1

Page 2: CSC450 Software Engineering

Outline• Quick review• Introduction to AspectJ

– Aspects – Pointcuts– advice– Intertype declarations

• Money transfer example aspect• Review & Summary

2

Page 3: CSC450 Software Engineering

3

• Something that is of interest to a stakeholder, – Functional– Non-functional

• Security, reliability, performance

What are Concerns?

3

A Quick Review

Page 4: CSC450 Software Engineering

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

Page 5: CSC450 Software Engineering

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

Page 6: CSC450 Software Engineering

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

Page 7: CSC450 Software Engineering

Outline• Quick review• Introduction to AspectJ

– Aspects – Pointcuts– advice– Intertype declarations

• Money transfer example aspect• Review & Summary

7

Page 8: CSC450 Software Engineering

AspectJ

AspectJ

• AspectJ is an implementation of aspect-oriented programming for Java.

8

Class

Methods Variables

Package

Aspect

Methods Variables

Advice

IntertypeDeclarations

Pointcut

Java

Page 9: CSC450 Software Engineering

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

Methods Variables

Package

Aspect

Methods Variables

Advice

IntertypeDeclarations

Pointcut

Java

Page 10: CSC450 Software Engineering

The HelloWorld Aspectpublic 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

Page 11: CSC450 Software Engineering

Outline• Quick review• Introduction to AspectJ

– Aspects – Pointcuts– advice– Intertype declarations

• Money transfer example aspect• Review & Summary

11

Page 12: CSC450 Software Engineering

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

Page 13: CSC450 Software Engineering

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

Page 14: CSC450 Software Engineering

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

Page 15: CSC450 Software Engineering

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

Page 16: CSC450 Software Engineering

Example 1: A FigureElement UML Diagram

Display FigureElement

+setXY(int, int)+setX(int)+setY(int)+draw()

<<Factory>>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

Page 17: CSC450 Software Engineering

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

Page 18: CSC450 Software Engineering

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 crosscutting18

Page 19: CSC450 Software Engineering

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

Page 20: CSC450 Software Engineering

Outline• Quick review• Introduction to AspectJ

– Aspects – Pointcuts

– advice– Intertype declarations

• Money transfer example aspect• Review & Summary

20

Page 21: CSC450 Software Engineering

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

Page 22: CSC450 Software Engineering

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

Page 23: CSC450 Software Engineering

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

Page 24: CSC450 Software Engineering

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

Page 25: CSC450 Software Engineering

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

Page 26: CSC450 Software Engineering

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

Page 27: CSC450 Software Engineering

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

Page 28: CSC450 Software Engineering

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

Page 29: CSC450 Software Engineering

Outline• Quick review• AOSD terminology• Introduction to AspectJ

– Aspects – Pointcuts– advice– Intertype declarations

• Money transfer example aspect• Summary

29

Page 30: CSC450 Software Engineering

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.

Page 31: CSC450 Software Engineering

Intertype Declarations

• Examples– Adding variables

private Address Account.address;private String MoneyTransferService.serviceName;

– Adding methodspublic void Account.changeAddress(Address newAddress){

//… }

31

Page 32: CSC450 Software Engineering

Intertype Declarations

• Changing the inheritance hierarchydeclare parents: Account implements NewAccount; declare parents: StockClient extends Frame;

32

<<Interface>>NewAccount

Account

Frame

StockClient

Page 33: CSC450 Software Engineering

Outline• Quick review• Introduction to AspectJ

– Aspects – Pointcuts– advice– Intertype declarations

• Money transfer example aspect• Review & Summary

33

Page 34: CSC450 Software Engineering

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

ClientAccount

deposit(double amt)withdraw(double amt)

MoneyTransferServicemoneyTransfer(a:Account, b:Account, amount:double)Info(info:String)

requestsmanages

1..*

1

1..*

1..*

34

Page 35: CSC450 Software Engineering

MoneyTransferService.javapackage 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

Page 36: CSC450 Software Engineering

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

36

Page 37: CSC450 Software Engineering

MoneyTransferServiceAspect.javapackage 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

Page 38: CSC450 Software Engineering

MoneyTransferServiceAspect.javapackage 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

Page 39: CSC450 Software Engineering

Outline• Quick review• Introduction to AspectJ

– Aspects – Pointcuts– advice– Intertype declarations

• Money transfer example aspect• Review & Summary

39

Page 40: CSC450 Software Engineering

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

Page 41: CSC450 Software Engineering

Summary

• Crosscutting concerns• AOSD using AspectJ

– Aspects• Pointcut, advice, intertype declaration

• A complete example with compilation instructions– LINK

41

Page 42: CSC450 Software Engineering

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 ajc compiler page: http://www.eclipse.org/aspectj/doc/released/devguide/ajc-ref.html

42

Page 43: CSC450 Software Engineering

The End

43

______________________Devon M. Simmonds

Computer Science DepartmentUniversity of North Carolina Wilmington

_____________________________________________________________

Qu es ti ons?