Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] capturing implicit toString() function calls



Ben,

While the call to "toString()" appears to be implicit the join point exists
just not in HelloWorld. The code that is generated looks a bit like this:

                   System.out.println(new
StringBuffer(hw.toString()).append(hw));

So the call to "HelloWorld.toString()" i.e. the join point exists in
"StringBuffer.append()" (which you can't advise without weaving rt.jar).
Using the following has a similar problem:

      System.out.println(hw);

In that you are making a call to the overloaded "println(Object)" method
rather than "println(String)". This is confirmed by printing a stack trace:

      public String toString()
       {
                  new Exception().printStackTrace();
                   return "Hello World ";
       }

java.lang.Exception
jpt hit
      at benjamin.messing.HelloWorld.toString(HelloWorld.java:22)
      at benjamin.messing.HelloWorld.main(HelloWorld.java:15)
java.lang.Exception
      at benjamin.messing.HelloWorld.toString(HelloWorld.java:22)
      at java.lang.String.valueOf(String.java:2177)
      at java.lang.StringBuffer.append(StringBuffer.java:361)
Hello World Hello World
Hello World
      at benjamin.messing.HelloWorld.main(HelloWorld.java:15)
java.lang.Exception
      at benjamin.messing.HelloWorld.toString(HelloWorld.java:22)
      at java.lang.String.valueOf(String.java:2177)
      at java.io.PrintStream.print(PrintStream.java:462)
      at java.io.PrintStream.println(PrintStream.java:599)
      at benjamin.messing.HelloWorld.main(HelloWorld.java:17)

Therefore if you change your pointcut you can catch the additional
"implicit" "toString()" calls:

aspect TestAspect
{
       before() : call (String HelloWorld.toString())
       || (call(void PrintStream.println(..)) && args(HelloWorld))
       || (call(StringBuffer StringBuffer.append(..)) && args(HelloWorld))
       {
                   System.out.println("jpt hit");
       }
}

jpt hit
jpt hit
Hello World Hello World
jpt hit
Hello World

Matthew Webster
AOSD Project
Java Technology Centre, MP146
IBM Hursley Park, Winchester,  SO21 2JN, England
Telephone: +44 196 2816139 (external) 246139 (internal)
Email: Matthew Webster/UK/IBM @ IBMGB, matthew_webster@xxxxxxxxxx
http://w3.hursley.ibm.com/~websterm/

"Benjamin Mesing" <benjamin.mesing@xxxxxxxxxxxxxxx>@eclipse.org on
11/11/2004 12:25:31

Please respond to aspectj-users@xxxxxxxxxxx

Sent by:    aspectj-users-admin@xxxxxxxxxxx


To:    aspectj-users@xxxxxxxxxxx
cc:
Subject:    [aspectj-users] capturing implicit toString() function calls


Hello,

I have stumbled over a problem. When specifying a pointcut, implicit
calls of toString functions will not be captured. Consider the following
example, wher "jpt hit" will be printed only once.
Is there anything I can do about it?

public class HelloWorld {

             public static void main(String[] args) {
                         HelloWorld hw = new HelloWorld();
                         // here occurs an explicit and an implicit call of
                         // toString()
                         System.out.println(hw.toString() + hw);
             }
             public String toString()
             {
                         return "Hello World ";
             }
}

public aspect TestAspect
{
             before() : call (String HelloWorld.toString())
             {
                         System.out.println("jpt hit");
             }
}

Greetings Ben
_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
http://dev.eclipse.org/mailman/listinfo/aspectj-users




Back to the top