4

I am having some trouble creating a before aspect for a method that has multiple parameters.

public class Sample {
    public boolean myCall(String s, String s1, String s3, ByteBuffer bytes, int i, int g) {
        System.out.println("Calling real sample");
    }
}

This aspect does not match. I only need to use the ByteBuffer parameter in the override code.

pointcut sampleCall(ByteBuffer bytes) :
    execution(boolean com.example.Sample.myCall (..)) && args(bytes);

before(ByteBuffer bytes) : sampleCall(bytes) {
    System.out.println("Before sample");
}

1 Answer 1

3

Actually finally got it to work with

pointcut sampleCall(ByteBuffer bytes) :
    execution(boolean com.example.Sample.myCall(String, String, String, ByteBuffer, ..))
    && args(String, String, String, bytes, ..);

before(ByteBuffer bytes) : sampleCall(bytes) {
    System.out.println("Before sample");
}
1
  • 1
    If you want to understand better why you need to know the parameter position either from the left or right hand side of the parameter list: because args(.., ByteBuffer, ..) would be ambiguous. Just imagine there are two ByteBuffer parameters. Which should would AspectJ select? I answered a similar question here. If you really have methods with parameter lists so different that you cannot match them with a single pointcut, either you need more pointcuts or have to catch them all and then iterate over JoinPoint.getArgs() during runtime (slow).
    – kriegaex
    Commented Mar 29, 2017 at 9:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.