[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
[aspectj-users] Preventing method parameters from being evaluated in advice
|
Hi all,
I'm a rather unexperienced user of aspectj and would like to share the
following problem with this list because I do not see any solution to it:
In a large project we use log4j for logging and there are, of course,
lots of logger.debug() calls in the code. If I deactivate the debug
level in the log4j configuration, the debug methods do not write any
debugging statements, but of course they are still executed. This means,
that whatever argument is passed, it's evaluated first, which might mean
rather expensive operations like initializing xslt transformers for xml
output and so on.
The obvious solution is to check with logger.isDebugEnabled() in an if
statement around the debug() calls but this is a policy which is hard to
enforce in a team with more than a few developers. Besides it makes for
ugly and unreadable code. So I thought, no problem, I can use aspectj,
define a pointcut to capture the debug (and, by extension, other log
level) calls, ask log4j, if it is debug enabled and proceed() only then
in an around advice. But - alas, its not that easy. When the around
advice is entered, the argument for the debug method has already been
evaluated. Here's a minimal test frame:
== Foo.java ========================
public class Foo {
public void myMethod(String s) {
System.out.println("In myMethod()");
}
public static void main(String[] args) {
Foo f = new Foo();
f.myMethod(f.new Bar().doSomething());
}
public class Bar {
public String doSomething() {
System.out.println("in doSomething()");
return "";
}
}
}
== Test.aj ========================
public aspect Test {
pointcut blockMyMethod() : call(public * Foo.myMethod(..));
void around() : blockMyMethod() {
System.out.println("in aspect");
proceed();
}
}
================================
This prints:
in doSomething()
in aspect
In myMethod():
instead of
in aspect
in doSomething()
In myMethod():
Stepping through it with the debugger corresponds to this result. So it
seems that the arguments to a method call are already evaluated when I
enter the advice which presumably is reasonable as I have to be able to
use them by way of the args pointcut. Just to make sure I tried other
pointcuts (execution, cflow) and before advice but the result is the same.
The question is now, if there is any way to prevent the evaluation of
arguments to method calls. In this case I would very much like to learn
how,
thanks and many greetings,
tarek