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");
}
kriegaex
  • 63,017
  • 15
  • 111
  • 202
UmYeah
  • 810
  • 2
  • 14
  • 21

1 Answers1

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");
}
buræquete
  • 14,226
  • 4
  • 44
  • 89
UmYeah
  • 46
  • 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](http://stackoverflow.com/a/15605403/1082681). 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 Mar 29 '17 at 09:39