You can use Aspect Oriented Programming to achieve this - in particular AspectJ .
The idea is that you define pointcuts that correspond to the points in your code, and then write advice that runs at those points. The AspectJ compiler is then interwoven in your tips at these points.
So, for your problem, you first define a pointcut that is selected every time you call the print method on PrintStream
pointcut callPrint(PrintStream ps, String s) : call(* java.io.PrintStream.print*(..)) && target(ps) && args(s);
Then you could write advice that would go around this call to replace the argument if PrintStream is System.out (you can do the same with System.err .
void around(PrintStream ps, String s) : callPrint(ps,s) { if(ps.equals(System.out)){ String new_string = ... proceed(new_string); } else proceed(s); }
Then you need to put all this into an aspect and twist it into your code - there are many tutorials on the Internet on how to do this.
source share