Advice placement on any parameter of a given type in AspectJ
Is there any way to give advice on any method with a parameter of a given type. I am planning to use this for a class that requires input filtering.
As an example:
@Filtered
class Unsafe {
public void doStuff (String input) { ... }
public void doMoreStuff (String input, int value, String name) { ... }
}
I want to write a tip that applies to any string parameter of methods in this class so that I can replace their content with a safe, escaped version (to prevent SQL injection, for example).
Can you write a pointcut like this?
+1
a source to share
2 answers
In my opinion, you still need to know if there is a template for all the methods that are used in the SQL premise.
And then you can check the method arguments in your advice:
Object around() : PATTERN_FOR_METHODS_THAT_TAKES_IN_SQL_STRING {
Signature sig = thisJoinPointStaticPart.getSignature();
Object[] args = thisJoinPoint.getArgs();
//Check the type of each object in the args, and re-format String type object
return proceed();// Continue with the current joint-point method
}
Replace PATTERN_FOR_METHODS_THAT_TAKES_IN_SQL_STRING with actual pattern
0
a source to share