【问题标题】:aspectj - how to find a method of an annotated class is calling another method of same class. i.e. nested callsaspectj - 如何找到带注释的类的方法正在调用同一类的另一个方法。即嵌套调用
【发布时间】:2011-01-31 11:10:20
【问题描述】:

我有一个注释@AppManaged,它用于表示需要编织某些行为的类。编织的一种行为是将方法调用转换为并发GPars(一个groovy并行库)调用。

但是,我不希望在同一个类上调用嵌套方法。 所以……

@AppManaged
class someclass 
{ 
     public void method1(){ 
         method2(); **// should not be advised**
     } 
     public void method2(){ 
     } 
} 

但是,如果方法调用是从 AppManaged 类到另一个类,那么它应该被建议,因此像 !cflowbelow(@within(AppManaged)) 这样的东西没有帮助。

@AppManaged 
class Someotherclass 
{ 
     private someclass s; 

     public void method3(){ 
        s.method2();**// Should be advised.** 
     } 
} 

基本上,我正在寻找一个切入点,它只匹配同一对象实例中的嵌套调用并阻止它们被建议。

任何帮助将不胜感激。

感谢和问候 亚伯拉罕·梅纳切里。

【问题讨论】:

    标签: annotations aspectj


    【解决方案1】:

    怎么样:

    pointcut appManagedExecution(Object appManaged) : execution(* (@AppManaged *).*(..)) && this(appManaged);
    
    pointcut appManagedCall(Object called) : call(* (@AppManaged *).*(..)) && target(called);
    
    pointcut routed(Object appManaged, Object called) : appManagedCall(called) && cflowbelow(appManagedExecution(appManaged)) && if(called != appManaged);
    

    【讨论】:

    • 嗨 Ramnivas,我有 Aspectj in Action 这本书,很高兴能从你那里得到答案!您建议的解决方案效果很好。然而,我在我的问题中犯了一个错误。应该是:应建议从类(appmanaged 或非 appmanaged)到 AppManaged 类的任何调用,不应建议同一对象内的嵌套调用。无论如何,我已经使用以下切入点来捕获从其他类到 @AppManaged 注释类的调用。切入点 firstCall() : @within(AppManaged) && !cflowbelow(@within(AppManaged));这样好吗?
    猜你喜欢
    • 1970-01-01
    • 2017-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-15
    • 1970-01-01
    • 1970-01-01
    • 2011-03-15
    相关资源
    最近更新 更多