【问题标题】:Java accessing function call parameters via AspectJJava 通过 AspectJ 访问函数调用参数
【发布时间】:2011-02-11 07:17:05
【问题描述】:
package a;
    Class X
        public fX(int i, String s);

package b;
    Class Y
        public fY(String arg1, String arg2, int arg3){
            ...
            ClassX.fX(1,"testY");
            // Need to execute some stuff right here after this call

        }

    Class Z
        public fZ(int n, int m){
            ClassX.fX(2,"testZ");
        }

我需要这样一个切入点和建议,它将指向 在 ClassX.fX(1,"testY") 方法调用之后,会给我访问权限 到 ClassY.fY(String arg1, String arg2, int arg3) 函数同时调用参数(即 arg1, arg2 和 arg3),

我试过这个但是没用。

pointcut ParameterPointCut(String arg1, String arg2, int arg3) :
    withincode (public String ClassY.fY(String,String,int))&&
    call(public String ClassX.fX(int, String)) &&
    args(arg1,arg2,arg3);


after(String arg1, String arg2, int arg3): ParameterPointCut(arg1,arg2,arg3){
        System.out.println("arg1 =" + arg1);
    }

将这些值放在正确位置的切入点和建议更改是什么?

提前致谢。

【问题讨论】:

    标签: java aspectj


    【解决方案1】:

    您必须使用虫洞模式来捕获参数并在以后的连接点提供它们。

    http://my.safaribooksonline.com/9781933988054/the_wormhole_pattern

    这是我写的一个小程序,可以解决你描述的问题:

    public aspect Aspect {
    
        pointcut outerMethod(String arg1, String arg2, int arg3) : 
            execution(public void Y.fY(String,String,int)) && 
            args(arg1, arg2, arg3); 
    
        pointcut innerMethod() : call(public void X.fX(int, String));
    
        after(String arg1, String arg2, int arg3) : 
            cflow(outerMethod(arg1, arg2, arg3)) &&
            innerMethod() {
            System.out.println("I'm here!!!");
            System.out.println(arg1 + " " + arg2 + " " + arg3);
        }
    
        public static void main(String[] args) {
            Y.fY("a", "b", 1);
        }
    }
    
    class X {
        public static void fX(int i, String s) {
    
        }
    }
    
    class Y {
        public static void fY(String arg1, String arg2, int arg3) {
            X.fX(1, "testY");
        }
    }
    

    【讨论】:

      【解决方案2】:

      你也可以使用:

      Object[] parameterList = thisJoinPoint.getArgs();
      
      System.out.println(Arrays.toString(parameterList));
      

      【讨论】:

        【解决方案3】:
        public aspect ExampleAspect {
        
        pointcut methodCall() : execution(public void printStuff(..));
        
        before(): methodCall(){
            System.out.println(thisJoinPoint.getArgs().toString()
                    + "  <--- printed by the aspect");
        }
        
        }
        
        class AspectTest {
        
        public static void printStuff(String s) {
            System.out.println(s + "  <---  printed by the method");
        }
        
        public static void main(String[] args) {
            printStuff("printedParameter");
        }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2022-01-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多