Overloaded Advices vs. Nested If-Else Statements
In previous examples, we have seen that JBoss AOP supports different types of advices and that those can have several signatures. In this example, we will show how to expand that flexibility by binding pointcuts to overloaded advices. Overloaded Advices vs. Nested If-Else Statements Overloaded advices can be useful to avoid nested if-else statements. Look at this version of JoinPointAspect.aroundAdvice: public void aroundAdvice(Invocation invocation) throws Throwable { if (invocation instanceof ConstructorInvocation) { System.out.println(">>> aroundAdvice on constructor of class: " + (ConstructorInvocation) invocation).getConstructor().getDeclaringClass().getName()); } else if (invocation instanceof MethodInvocation) { System.out.println(">>> aroundAdvice on method execution: " + ((MethodInvocation) invocation).getMethod().getName()); } else if (invocation instanceof FieldReadInvocation) { System.out.println(">>> aroundAdvice on field read: " + ((FieldReadInvocation) invocation).getField().getName()); } else if (invocation instanceof FieldWriteInvocation) { System.out.println(">>> aroundAdvice on field write: " + ((FieldWriteInvocation) invocation).getField().getName()); } return invocation.invokeNext(); } ___________________________ As you can see, aroundAdvice is a simple advice that logs constructor and method executions, field reads and field writes. Despite that, its implementation does not look so simple as a logging advice should be.