2011-03-28 55 views
3

我在創建具有多個參數的方法的before方面遇到了一些麻煩。AspectJ:使用'args()'參數綁定多個參數的方法

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

回答

3

其實終於它與

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

如果你想更好地理解爲什麼你需要知道無論是從參數列表的左側或右側的參數位置工作:因爲'ARGS (..,ByteBuffer,..)'是不明確的。試想一下,有兩個「ByteBuffer」參數。 AspectJ應該選擇哪一個?我回答了一個類似的問題[這裏](http://stackoverflow.com/a/15605403/1082681)。如果你確實有方法參數列表如此不同以至於你無法將它們與單個切入點匹配,那麼要麼需要更多的切入點,要麼必須全部抓住它們,然後在運行時(慢速)迭代'JoinPoint.getArgs()'。 – kriegaex 2017-03-29 09:39:33