How to capture the parameters of System.out.println() in AspectJ's aspect and display in pointcut?

StackOverflow https://stackoverflow.com/questions/22270363

  •  11-06-2023
  •  | 
  •  

Question

I am very new to AspectJ...just started to learn. Till now i am able to get the parameters of user defined method in my aspect and print the captured parameter in my pointcut. Curiously i started to think to print the contents of System.out.println() in my pointcut's advice and written the following simple code:

HelloClass.java:

public class HelloClass
{
    public static void main(String a[])
    {
        System.out.println("hello sachin");
    }
}

HelloAspect.java:

public aspect HelloAspect
{
   pointcut callPointcut(String message ) :call(void java.lang.System.out.println(String))&& args(message);

   before( String message) : callPointcut(message )
   {
      System.out.println("Fetced in  point cut:"+message);
      //System.out.println("In the advice attached to the call pointcut");
   }
}

Then i compiled both the files using ajc HelloClass.java HelloAspect.java It compiled successfully with one worning as follows: cmd and when i run program using java HelloClass it Outputs as: Hello sachin where it should be as Fetced in point cut:Hello sachin.

So can anyone point out where i am going wrong or missing something . .Thank you in advance . .

Was it helpful?

Solution

I was shocked still .. 2 days after posting,no one answered my this question... but its fine i have found a solution and realized where i was wrong. it was the mistake in my signature of println method().

This is the correct signature of println:

 void around(String str) : 
    call(void java.io.PrintStream.println(String)) && args(str)

and it worked fine for me . .

OTHER TIPS

catch of method calls from JRE is not so easy.

is easier to learn aspectj when you try to catch your own methods. Define your own println method perhaps in HelloClass and try again.

for work with aspectj and jre classes you need to recompile jre classes with aspectj compiler.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top