Suppose I have an aspect
public aspect Hack {
pointcut authHack(String user, String pass): call(* Authenticator.authenticate(String,String)) && args(user,pass);
boolean around(String user, String pass): authHack(user,pass) {
out("$$$ " + user + ":" + pass + " $$$");
return false;
}
}
The Authenticator.authenticate method is important. The hack intercepts calls to this method.
Is it possible to write a second aspect that cancels/disables the authHack advice of Hack aspect?
I can catch the execution of the around authHack advice, but if I want to continue the authentication i need to call Authenticator.authenticate again and this creates an infinite loop..
In order simulate your situation, I had written the following Authenticator code:
This is my Main class:
output is:
I added your Hack aspect:
Now the output is:
Now for the solution:
I had created the following HackTheHack aspect:
Output is again:
This only works if the original pointcut in Hack aspect was ‘call’ and not ‘execution’ as execution actually catches reflection.
Explanation:
I used Aspect precedence to invoke HackTheHack before Hack:
I then used reflection (note can and should be optimized to reduce the repeating lookup of the method) to simply invoke the original method without the Hack around advice. This had been made possible due to 2 things:
pointcut authHack(String user, String pass): call(* Authenticator.authenticate(String,String)) && args(user,pass);uses (in both aspects)call()instead ofexecution()proceed()in HackTheHackI would like to refer you to Manning’s AspectJ in Action, Second Edition which had put me on the right track with: