Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

aop

aspectj

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 . .

like image 650
Anonymous Avatar asked Dec 12 '25 06:12

Anonymous


1 Answers

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 . .

like image 109
Anonymous Avatar answered Dec 13 '25 19:12

Anonymous