I'm trying to normalize URIs across an application using AspectJ. I'm catching every call that is made to a method passing in a java.net.URI parameter using this code:
Object around() : execution(* *(..,java.net.URI,..)) {
for ( Object arg : thisJoinPoint.getArgs() ) {
if ( arg instanceof URI ) {
// normalize
}
}
return proceed();
}
However, since URI is immutable, I can't swap in the normalized value into the existing object. What I need is to call proceed with the new, normalized URI objects (and possibly passing along the other arguments unchanged). However, the proceed call only lets me pass along arguments that were collected by the join point. Is there any way to accomplish this for a variable number of arguments (mostly being interested in any URI argument(s), but willing to collect and pass along all arguments)?
You need to change your advice's signature:
Object around(Object[] args) : execution(* *(..,java.net.URI,..)) && args(args) {
for(int i = 0; i < args.length; i++){
Object arg = args[i];
if ( arg instanceof URI ) {
args[i] = normalizeUrI((URI)arg);
}
}
return proceed(args);
}
Update: the above code doesn't work unfortunately. But this should:
Object around() throws URISyntaxException : execution(* **.*(..,URI,..)) {
final Object[] args = thisJoinPoint.getArgs();
for(int i = 0; i < args.length; i++){
final Object arg = args[i];
if ( arg instanceof URI ) {
args[i] = normalizeUrI((URI)arg);
}
}
try{
return ((ProceedingJoinPoint)thisJoinPoint).proceed(args);
} catch(final Throwable e){
throw new IllegalStateException(e);
}
}
What follows should be a comment not a full answer but my limited rep doesn't allow me to comment...
The code in the question and also in the answer by Sean Patrick Floyd won't work because there can only be one occurrence of the wildcard ".." in a method signature pattern. A short explanation of why it is so can be found here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With