【问题标题】:AspectJ: parameter binding with 'args()' for methods with multiple parametersAspectJ:具有多个参数的方法的参数绑定与 'args()'
【发布时间】:2017-03-29 17:49:45
【问题描述】:

我在为具有多个参数的方法创建 before 方面时遇到了一些问题。

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

这方面不匹配。我只需要在覆盖代码中使用 ByteBuffer 参数即可。

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

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

【问题讨论】:

    标签: java aop aspectj


    【解决方案1】:

    其实终于搞定了

    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");
    }
    

    【讨论】:

    • 如果您想更好地理解为什么需要从参数列表的左侧或右侧知道参数位置:因为args(.., ByteBuffer, ..) 会模棱两可。想象一下有两个ByteBuffer 参数。 AspectJ 应该选择哪个?我回答了一个类似的问题here。如果您确实有参数列表不同的方法,以至于您无法将它们与单个切入点匹配,那么您需要更多切入点或必须全部捕获它们然后在运行时迭代JoinPoint.getArgs()(慢)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    • 2013-03-06
    • 1970-01-01
    • 1970-01-01
    • 2017-08-08
    • 1970-01-01
    相关资源
    最近更新 更多