Aspect Oriented Programming
Aspect Oriented Programming
A short introduction to
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
Open/Closed Principle
Software entities should be open for extension, but closed for modification.
Objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program.
Many client specific interfaces are better than one general purpose interface.
BUT
Ususal Suspects
Logging Error checking Security Synchronization Transactions
A POSSIBLE SOLUTION
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
AspectJ
Aspect-oriented extension of Java De-facto standard for AOP Provides own compiler and aspect weaver Decent integration into Eclipse (AJDT)
POINTCUTS
handler(ArrayOutOfBoundsException)
when an exception handler executes
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
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
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); }
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; } }
PRACTICAL APPLICATION