a short introduction to aspect oriented programming€¦ · single responsibility principle an...
TRANSCRIPT
ASPECT ORIENTED
PROGRAMMING
A short introduction to
OpenTechTalk OTT002
STATUS QUO
Not the band...
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
CROSS CUTTING CONCERNS
It‘s all over the place...
Ususal Suspects
• Logging
• Error checking
• Security
• Synchronization
• Transactions
A POSSIBLE SOLUTION
Don‘t worry, we got this.
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“?
THE JOIN POINT MODEL
Jay Pee Em
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
ASPECTJ
This was also created at Palo Alto?!
AspectJ
• Aspect-oriented extension of Java
• De-facto standard for AOP
• Provides own compiler and aspect weaver
• Decent integration into Eclipse (AJDT)
POINTCUTS
Along the dotted line
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));
ADVICE
Told you so.
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);
}
INTER-TYPE DECLARATIONS
I would have designed that class differently.
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; }
}
PRACTICAL APPLICATION
Well, there‘s logging... and... uhm... Did I mention logging?