Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] parameter evaluation and advice

> [...] the parameter
> to the debug() method ('myString') is being evaluated,
> even if log is not debugEnabled and
> proceed() is never called.

That's true and a good observation.

The method-call join point is the call itself, and
does not include any required parameter evaluation.
That means causing the call to not proceed does not
prevent the arguments from being evaluated.

For more information, see Jim and Erik's new paper

  http://dev.eclipse.org/mhonarc/lists/aspectj-dev/msg00663.html

It also has a discussion on efficiency, using logging as
an example.

If evaluating parameters is expensive (e.g., String
concatenation), then for any logging system you'd
want to check whether you were logging before evaluating the
parameters.  There's no way in AspectJ to pick out
"parameter evaluation for this method call." However, if you
find your code falling into 3-4 patterns (e.g. concatenating
a label and an object), you might be able to refactor a
lot of logging calls to defer parameter evaluation to the
logging method.  Then you have a single point to short-
circuit the evaluation, rather than many scattered about
in every code snippet that calls the logger.

Wes

---- output
 $ j4 -classpath "$ajrt;." CallParms
getArg

---- program

public class CallParms {
  public static void main(String[] args) {
    go(getArg());
  }
  static void go(String s) {
    System.out.println("go " + s);
  }
  static String getArg() {
    System.out.println("getArg");
    return "arg";
  }
}
aspect A {
  void around() : call(void go(String)) {}
}

Peter Kalmus wrote:
Consider the following advice:

    void around(Logger aLog) :
        call(public * Logger.debug(..))
        && target(aLog)  {
            if (aLog.isDebugEnabled()) {
                proceed(aLog);
} }

This advice works as expected - it prevents calls to log.debug(myString) from executing if log is
not debugEnabled.
However, testing for a side effect on myString or examining byte code reveals that the parameter
to the debug() method ('myString') is being evaluated, even if log is not debugEnabled and
proceed() is never called.

I'm not familiar with how AspectJ is implemented, but... is there a way to prevent parameter
evaluation in this example?
Thanks,
Peter Kalmus

__________________________________
Do you Yahoo!?
Protect your identity with Yahoo! Mail AddressGuard
http://antispam.yahoo.com/whatsnewfree
_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
http://dev.eclipse.org/mailman/listinfo/aspectj-users




Back to the top