Writing a java annotation for timing method call

后端 未结 9 1385
逝去的感伤
逝去的感伤 2020-12-24 12:17

I want to write a java annotation which times the method call. something like this:

@TimeIt
public int someMethod() { ... }

and when this m

9条回答
  •  有刺的猬
    2020-12-24 12:54

    As of 2016, there's a nifty aspect annotation library jcabi-aspects.

    From the docs:

    Annotate your methods with @Loggable annotation and every time they are called, your SLF4J logging facility will receive a message with the details of execution and the total execution time:

    public class Resource {
      @Loggable(Loggable.DEBUG)
      public String load(URL url) {
        return url.openConnection().getContent();
      }
    }
    

    Something like this will appear in the log:

    [DEBUG] #load('http://www.google.com'): returned "

    Read more about @Loggable here.

提交回复
热议问题