2011-02-11 12 views
5
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"); 
     } 

que necesitan un punto de corte y asesoramiento de tal manera que se apuntan a la derecha después de ClassX.fX (1, ​​"irritable") llamada al método y me van a dar acceso a ClassY.fY (String arg1, String arg2, int arg3) argumentos de llamada de función (es decir, arg1, arg2 y arg3) al mismo tiempo,Java para acceder a los parámetros de la función de llamada a través de AspectJ

He intentado esto pero no funcionó.

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

¿Cuáles serían los cambios puntuales y consejos para tomar esos valores en el lugar correcto?

Gracias de antemano.

Respuesta

7

Deberá usar un patrón de agujero de gusano para capturar los parámetros y hacerlos disponibles en un punto de unión posterior.

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

Aquí es un pequeño programa que escribí se soluciona el problema que usted está describiendo:

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

También puede utilizar:

Object[] parameterList = thisJoinPoint.getArgs(); 

System.out.println(Arrays.toString(parameterList)); 
0
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"); 
} 
} 
Cuestiones relacionadas