samedi 19 avril 2014

Java - appel procéder à n paramètres dans AspectJ - Stack Overflow


assuming that i defined a pointcut of this form


* *.*(..)

and i want to define an around advice, how can i call proceed with an arbitrary number of parameters?


I thought about using reflection and thisJoinPoint.getArgs() but before trying, i would like to know if there's a clean and easy way.




It is a common misunderstanding that proceed takes the same arguments as the method that matches the pattern does. However, proceed takes arguments that advice prescribes.


Example:


class C {   
public void foo(int i, int j, char c) {
System.out.println("T.foo() " + i*j + " " + c);
}
}

class Context {
public int bar = 7;
public void doStuff() {
C c = new C();
c.foo(2, 3, 'x');
}
}

with an aspect:


public aspect MyAspect {

pointcut AnyCall() :
call(* *.*(..)) && !within(MyAspect);

void around(Context c) : AnyCall() && this(c) {
if (c.bar > 5)
proceed(c); // based on "around(Context c)"
}
}


assuming that i defined a pointcut of this form


* *.*(..)

and i want to define an around advice, how can i call proceed with an arbitrary number of parameters?


I thought about using reflection and thisJoinPoint.getArgs() but before trying, i would like to know if there's a clean and easy way.



It is a common misunderstanding that proceed takes the same arguments as the method that matches the pattern does. However, proceed takes arguments that advice prescribes.


Example:


class C {   
public void foo(int i, int j, char c) {
System.out.println("T.foo() " + i*j + " " + c);
}
}

class Context {
public int bar = 7;
public void doStuff() {
C c = new C();
c.foo(2, 3, 'x');
}
}

with an aspect:


public aspect MyAspect {

pointcut AnyCall() :
call(* *.*(..)) && !within(MyAspect);

void around(Context c) : AnyCall() && this(c) {
if (c.bar > 5)
proceed(c); // based on "around(Context c)"
}
}

0 commentaires:

Enregistrer un commentaire