An aspect is a common feature that’s typically scattered across methods, classes, object hierarchies, or even entire object models.
Express this structure in code with traditional object-oriented techniques.
For example, let’s say you wanted to add code to an application to measure the amount of time it would take to invoke a particular method. In plain Java, the code would look something like the following.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public class Account { public void Calculate(double amount) { long startTime = System.currentTimeMillis(); try { // Actual method body... } finally { long endTime = System.currentTimeMillis() - startTime; System.out.println("Draw Actual area," + endTime); } } } |
Implementing metrics in a AOP Interceptor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public class Metrics implements org.jboss.aop.advice.Interceptor { public Object invoke(Invocation invocation) throws Throwable { long startTime = System.currentTimeMillis(); try { return invocation.invokeNext(); // if return fails, Test it your self. } finally { long endTime = System.currentTimeMillis() - startTime; java.lang.reflect.Method m = ((MethodInvocation)invocation).method; System.out.println(" Our kutays methods end : " + m.toString() + " time: " + endTime + "ms"); } } } |