100% found this document useful (1 vote)
233 views

Aspect Oriented Programming

This document provides an introduction to aspect-oriented programming (AOP) and AspectJ. It discusses the motivation for AOP from the perspective of separation of concerns. It also summarizes some key AOP concepts like join points, pointcuts, and advice. Pointcuts allow aspects to identify specific locations in a program, and advice allows inserting additional cross-cutting behavior at those pointcut locations. AspectJ is introduced as the most widely used AOP extension for Java that provides an aspect weaver. Examples of pointcuts and advice in AspectJ are also provided.

Uploaded by

OpenTechTalk
Copyright
© Attribution (BY)
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
233 views

Aspect Oriented Programming

This document provides an introduction to aspect-oriented programming (AOP) and AspectJ. It discusses the motivation for AOP from the perspective of separation of concerns. It also summarizes some key AOP concepts like join points, pointcuts, and advice. Pointcuts allow aspects to identify specific locations in a program, and advice allows inserting additional cross-cutting behavior at those pointcut locations. AspectJ is introduced as the most widely used AOP extension for Java that provides an aspect weaver. Examples of pointcuts and advice in AspectJ are also provided.

Uploaded by

OpenTechTalk
Copyright
© Attribution (BY)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

OpenTechTalk OTT002

A short introduction to

ASPECT ORIENTED PROGRAMMING

Not the band...

STATUS QUO

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.

SOLID
Robert C. Martin (in early 2000s) Single Responsibility Principle Open/Closed Principle Liskov Substitution Principle Interface Segregation Principle Dependency Inversion Principle

Single Responsibility Principle

An object should have only a single responsibility.

Open/Closed Principle

Software entities should be open for extension, but closed for modification.

Liskov Substitution Principle

Objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program.

Interface Segregation Principle

Many client specific interfaces are better than one general purpose interface.

Dependency Inversion Principle

Depend upon abstractions. Do not depend upon concretions.

BUT

Its all over the place...

CROSS CUTTING CONCERNS

Ususal Suspects
Logging Error checking Security Synchronization Transactions

Dont worry, we got this.

A POSSIBLE SOLUTION

Wouldnt 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?

Jay Pee Em

THE JOIN POINT MODEL

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

This was also created at Palo Alto?!

ASPECTJ

AspectJ
Aspect-oriented extension of Java De-facto standard for AOP Provides own compiler and aspect weaver Decent integration into Eclipse (AJDT)

Along the dotted line

POINTCUTS

Some Example Pointcuts


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

handler(ArrayOutOfBoundsException)
when an exception handler executes

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

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

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

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

Some Example Pointcuts


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

Some Example Pointcuts


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

Some Example Pointcuts


pointcut testEquality(Point p1, Point p2): target(p1) && call(boolean equals(Object) && args(p2));

Told you so.

ADVICE

An Example Advice
pointcut services(Server s): target(s) && call(public * *(..));
before(Server s): services(s) { if (s.disabled) { throw new DisabledException(); } }

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

I would have designed that class differently.

INTER-TYPE DECLARATIONS

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; } }
}

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; } }

Well, theres logging... and... uhm... Did I mention logging?

PRACTICAL APPLICATION

You might also like