Writing a java annotation for timing method call

后端 未结 9 1449
逝去的感伤
逝去的感伤 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:40

    I am surprised to see that no one pointed out java.lang.reflect.Proxy. Its an old thread, but I think this information would be helpful to someone.

    Proxy has an interesting property which gives

    1. proxy instanceof Foo as true.
    2. You can have a method in your invocation handler, which prints the time first and then fires the actual method from the object.

    You can have this proxy for all objects by making them implement some interface or you can use Comparable.

    Look for section Dynamic proxies as decorator.

    http://www.ibm.com/developerworks/library/j-jtp08305/

提交回复
热议问题