How can I add similar functionality to a number of methods in java?

Posted by Roman on Stack Overflow See other posts from Stack Overflow or by Roman
Published on 2010-06-12T11:32:58Z Indexed on 2010/06/12 11:43 UTC
Read the original article Hit count: 162

I have a lot of methods for logging, like logSomeAction, logAnotherAction etc.

Now I want all these methods make a small pause after printing messages (Thread.sleep).

If I do it manually, I would do something like this:

//before:
public static void logSomeAction () {
   System.out.println (msg(SOME_ACTION));
}

//after:
public static void logSomeAction () {
   System.out.println (msg(SOME_ACTION));
   try {
      Thread.sleep (2000);
   } catch (InterruptedException ignored) { }
}

I remember that Java has proxy classes and some other magic-making tools. Is there any way avoid copy-n-pasting N sleep-blocks to N logging methods?

© Stack Overflow or respective owner

Related posts about java

Related posts about language-features