I've been searching for a while and can't seem to find something that matches exactly what I need.
I want to have a way to wrap any class (let's say T) with something that would "do something cool" before and after each invocation of the public methods of this T class, and this thing would look like T to the compiler.
Here's some pseudo code to illustrate:
object CoolWrap {
def apply[T](underlying: T) {
new CoolWrapImpl(underlying).asInstanceOf[T]
}
}
class CoolWrapImpl[T](underlying: T) extends T {
def onPublicMethod(method: Method, params: Params) = {
// method would be the method that was invoked on T
doSomethingCool1(params) // like logging
val resp = measureTime { method.invoke(params) }
doAnotherCoolThing()
resp
}
}
Using reflection is not out of the question, this would only happen once per instance that would live in memory throughout the life of the process, so I'm not concerned with it being slow to instantiate.
Is this even possible? (currently forced to use Scala 2.9.2)
Thanks!