Writing a java annotation for timing method call

后端 未结 9 1408
逝去的感伤
逝去的感伤 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条回答
  •  -上瘾入骨i
    2020-12-24 12:59

    AFAIK, Tomasz is right in saying that this can't be done using annotations. I think the confusion stems from the fact that Python decorators and Java annotations share the same syntax but are completely different in terms of the behavior they offer!

    Annotations are metadata attached to your class/methods/fields. This blog post addresses the point of timing methods using AOP. Though it uses Spring, the basic premise remains the same. If you are good to go with an AOP compiler, it shouldn't be too difficult to translate the code. Another reference (spring specific) here.

    EDIT: If your aim is to have a overall method timing for your application without using full blown profilers, you can use hprof for collecting total execution statistics.

提交回复
热议问题