35
ASPECT ORIENTED PROGRAMMING A short introduction to OpenTechTalk OTT002

A short introduction to ASPECT ORIENTED PROGRAMMING€¦ · Single Responsibility Principle An object should have only a single responsibility. Open/Closed Principle Software entities

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: A short introduction to ASPECT ORIENTED PROGRAMMING€¦ · Single Responsibility Principle An object should have only a single responsibility. Open/Closed Principle Software entities

ASPECT ORIENTED

PROGRAMMING

A short introduction to

OpenTechTalk OTT002

Page 2: A short introduction to ASPECT ORIENTED PROGRAMMING€¦ · Single Responsibility Principle An object should have only a single responsibility. Open/Closed Principle Software entities

STATUS QUO

Not the band...

Page 3: A short introduction to ASPECT ORIENTED PROGRAMMING€¦ · Single Responsibility Principle An object should have only a single responsibility. Open/Closed Principle Software entities

Separation of Concerns

Edsgar W. Dijkstra (in 1974):

This is what I mean by "focusing one's attention upon some aspect": it does not mean ignoring the other aspects, it is just doing justice to the fact that from this aspect's point of view, the other is irrelevant. It is being one- and multiple-track minded simultaneously.

Page 4: A short introduction to ASPECT ORIENTED PROGRAMMING€¦ · Single Responsibility Principle An object should have only a single responsibility. Open/Closed Principle Software entities

SOLID

Robert C. Martin (in early 2000s)

• Single Responsibility Principle

• Open/Closed Principle

• Liskov Substitution Principle

• Interface Segregation Principle

• Dependency Inversion Principle

Page 5: A short introduction to ASPECT ORIENTED PROGRAMMING€¦ · Single Responsibility Principle An object should have only a single responsibility. Open/Closed Principle Software entities

Single Responsibility Principle

An object should have only

a single responsibility.

Page 6: A short introduction to ASPECT ORIENTED PROGRAMMING€¦ · Single Responsibility Principle An object should have only a single responsibility. Open/Closed Principle Software entities

Open/Closed Principle

Software entities should be open for

extension, but closed for modification.

Page 7: A short introduction to ASPECT ORIENTED PROGRAMMING€¦ · Single Responsibility Principle An object should have only a single responsibility. Open/Closed Principle Software entities

Liskov Substitution Principle

Objects in a program should be replaceable

with instances of their subtypes without

altering the correctness of that program.

Page 8: A short introduction to ASPECT ORIENTED PROGRAMMING€¦ · Single Responsibility Principle An object should have only a single responsibility. Open/Closed Principle Software entities

Interface Segregation Principle

Many client specific interfaces are better

than one general purpose interface.

Page 9: A short introduction to ASPECT ORIENTED PROGRAMMING€¦ · Single Responsibility Principle An object should have only a single responsibility. Open/Closed Principle Software entities

Dependency Inversion Principle

Depend upon abstractions.

Do not depend upon concretions.

Page 10: A short introduction to ASPECT ORIENTED PROGRAMMING€¦ · Single Responsibility Principle An object should have only a single responsibility. Open/Closed Principle Software entities

BUT

Page 11: A short introduction to ASPECT ORIENTED PROGRAMMING€¦ · Single Responsibility Principle An object should have only a single responsibility. Open/Closed Principle Software entities

CROSS CUTTING CONCERNS

It‘s all over the place...

Page 12: A short introduction to ASPECT ORIENTED PROGRAMMING€¦ · Single Responsibility Principle An object should have only a single responsibility. Open/Closed Principle Software entities

Ususal Suspects

• Logging

• Error checking

• Security

• Synchronization

• Transactions

Page 13: A short introduction to ASPECT ORIENTED PROGRAMMING€¦ · Single Responsibility Principle An object should have only a single responsibility. Open/Closed Principle Software entities

A POSSIBLE SOLUTION

Don‘t worry, we got this.

Page 14: A short introduction to ASPECT ORIENTED PROGRAMMING€¦ · Single Responsibility Principle An object should have only a single responsibility. Open/Closed Principle Software entities

Wouldn‘t it be nice if we...

• ...could point at a piece of code and give it

a label?

• ...could add behavior at that label?

• ...had access to variables in the scope of

that label?

• ...never had to touch the affected source

file?

• ...could wrap it all up and call it „aspect“?

Page 15: A short introduction to ASPECT ORIENTED PROGRAMMING€¦ · Single Responsibility Principle An object should have only a single responsibility. Open/Closed Principle Software entities

THE JOIN POINT MODEL

Jay Pee Em

Page 16: A short introduction to ASPECT ORIENTED PROGRAMMING€¦ · Single Responsibility Principle An object should have only a single responsibility. Open/Closed Principle Software entities

JPM Terminology

• Join Points• all the points in code that can be addressed

• Pointcuts• a set of Join Points that fulfil certain conditions

• Advice• additional behavior to be inserted before, after or

even around a certain Pointcut

Page 17: A short introduction to ASPECT ORIENTED PROGRAMMING€¦ · Single Responsibility Principle An object should have only a single responsibility. Open/Closed Principle Software entities

ASPECTJ

This was also created at Palo Alto?!

Page 18: A short introduction to ASPECT ORIENTED PROGRAMMING€¦ · Single Responsibility Principle An object should have only a single responsibility. Open/Closed Principle Software entities

AspectJ

• Aspect-oriented extension of Java

• De-facto standard for AOP

• Provides own compiler and aspect weaver

• Decent integration into Eclipse (AJDT)

Page 19: A short introduction to ASPECT ORIENTED PROGRAMMING€¦ · Single Responsibility Principle An object should have only a single responsibility. Open/Closed Principle Software entities

POINTCUTS

Along the dotted line

Page 20: A short introduction to ASPECT ORIENTED PROGRAMMING€¦ · Single Responsibility Principle An object should have only a single responsibility. Open/Closed Principle Software entities

Some Example Pointcuts

• call(void Point.setX(int))• when a method is called

• handler(ArrayOutOfBoundsException)• when an exception handler executes

Page 21: A short introduction to ASPECT ORIENTED PROGRAMMING€¦ · Single Responsibility Principle An object should have only a single responsibility. Open/Closed Principle Software entities

Some Example Pointcuts

• call(* setY(long))• any setY method that takes a long as an

argument, regardless of return type or declaring

type

• call(*.new(int, int))• the call to any classes' constructor, so long as it

takes exactly two ints as arguments

• call(public * *(..))• any call to a public method

Page 22: A short introduction to ASPECT ORIENTED PROGRAMMING€¦ · Single Responsibility Principle An object should have only a single responsibility. Open/Closed Principle Software entities

Some Example Pointcuts

• call(* MyInterface.*(..))• any call to a method in MyInterface's signature -

- that is, any method defined by MyInterface or

inherited by one of its a supertypes

Page 23: A short introduction to ASPECT ORIENTED PROGRAMMING€¦ · Single Responsibility Principle An object should have only a single responsibility. Open/Closed Principle Software entities

Some Example Pointcuts

• target(Point) && call(int *())• any call to an int method with no arguments on

an instance of Point, regardless of its name

• !this(Point) && call(int *(..))• any method call to an int method when the

executing object is any type except Point

Page 24: A short introduction to ASPECT ORIENTED PROGRAMMING€¦ · Single Responsibility Principle An object should have only a single responsibility. Open/Closed Principle Software entities

Some Example Pointcuts

• execution(void Point.setX(int))• when a particular method body executes

• within(MyClass)• when the executing code belongs to class MyClass

• cflow(call(void Test.main()))• when the join point is in the control flow of a call

to a Test's no-argument main method

Page 25: A short introduction to ASPECT ORIENTED PROGRAMMING€¦ · Single Responsibility Principle An object should have only a single responsibility. Open/Closed Principle Software entities

Some Example Pointcuts

pointcut setter():target(Point) &&(call(void setX(int)) ||call(void setY(int)));

Page 26: A short introduction to ASPECT ORIENTED PROGRAMMING€¦ · Single Responsibility Principle An object should have only a single responsibility. Open/Closed Principle Software entities

Some Example Pointcuts

pointcut setter(Point p):target(p) &&(call(void setX(int)) ||call(void setY(int)));

Page 27: A short introduction to ASPECT ORIENTED PROGRAMMING€¦ · Single Responsibility Principle An object should have only a single responsibility. Open/Closed Principle Software entities

Some Example Pointcuts

pointcut testEquality(Point p1, Point p2):

target(p1) &&

call(boolean equals(Object) &&

args(p2));

Page 28: A short introduction to ASPECT ORIENTED PROGRAMMING€¦ · Single Responsibility Principle An object should have only a single responsibility. Open/Closed Principle Software entities

ADVICE

Told you so.

Page 29: A short introduction to ASPECT ORIENTED PROGRAMMING€¦ · Single Responsibility Principle An object should have only a single responsibility. Open/Closed Principle Software entities

An Example Advice

pointcut services(Server s):target(s) &&call(public * *(..));

before(Server s): services(s){

if (s.disabled){

throw new DisabledException();}

}

Page 30: A short introduction to ASPECT ORIENTED PROGRAMMING€¦ · Single Responsibility Principle An object should have only a single responsibility. Open/Closed Principle Software entities

An Example Advice

after(Server s) throwing (FaultException e):services(s)

{

s.disabled = true;

reportFault(e);

}

Page 31: A short introduction to ASPECT ORIENTED PROGRAMMING€¦ · Single Responsibility Principle An object should have only a single responsibility. Open/Closed Principle Software entities

INTER-TYPE DECLARATIONS

I would have designed that class differently.

Page 32: A short introduction to ASPECT ORIENTED PROGRAMMING€¦ · Single Responsibility Principle An object should have only a single responsibility. Open/Closed Principle Software entities

An Example Aspect

aspect PointAssertions {

private boolean Point.assertX(int x){

return (x <= 100 && x >= 0);}

before(Point p, int x):target(p) && args(x) && call(void setX(int))

{if (!p.assertX(x)){

System.out.println("Illegal value");return;

}}

}

Page 33: A short introduction to ASPECT ORIENTED PROGRAMMING€¦ · Single Responsibility Principle An object should have only a single responsibility. Open/Closed Principle Software entities

Example Revisited

aspect FaultHandler{

private boolean Server.disabled = false;

public static void fixServer(Server s){

s.disabled = false;}

pointcut services(Server s): ...;

before(Server s): services(s)

{ if(s.disabled) {...} }

after(Server s) throwing (FaultException e): services(s) { s.disabled = true; }

}

Page 34: A short introduction to ASPECT ORIENTED PROGRAMMING€¦ · Single Responsibility Principle An object should have only a single responsibility. Open/Closed Principle Software entities

PRACTICAL APPLICATION

Well, there‘s logging... and... uhm... Did I mention logging?

Page 35: A short introduction to ASPECT ORIENTED PROGRAMMING€¦ · Single Responsibility Principle An object should have only a single responsibility. Open/Closed Principle Software entities