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


Since you can have multiple lines, I don't see how you can do this in different places, but both classes must call the same method that will write to the database. At this point, you can use around to modify the string to make it safe and pass it instead.



0


a source


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







All Articles