2 years later I ran into this situation and I solved my case using java proxies. The actual magic is built in using java's invocation handler () and Proxy ()
Hat Tip to (How to use java proxy in scala) where this answer is based on.
scala> :paste
// Entering paste mode (ctrl-D to finish)
import java.lang.reflect.{Method, InvocationHandler, Proxy}
object ProxyTesting {
// You need an interface or java's proxy to work.
trait ProxyTestIface {
// your function signature here
def createApplication( firstDay:String, lastDay:String, details:String, substitute:String, projectManager:String):Unit = ???
}
// your object implementation
class ProxyTest extends ProxyTestIface{
// your function implementation
override def createApplication( firstDay:String, lastDay:String, details:String, substitute:String, projectManager:String):Unit = {
println("Actual Function is called")
Unit
}
}
def test() {
val obj = new ProxyTest
val impl = Proxy.newProxyInstance(
obj.getClass.getClassLoader,
Array(classOf[ProxyTestIface]),
new InvocationHandler {
def invoke(proxy: scala.AnyRef, method: Method, args: Array[AnyRef]): AnyRef = {
// this shows you can execute prior and post your decorated function.
println(s"Proxy Funcion is called: ${args.mkString(",")}")
val res = method.invoke(obj, args:_*)
println(s"Proxy Funcion is called: 2nd time ${args.mkString(",")}")
res
}
}
).asInstanceOf[ProxyTestIface]
impl.createApplication("firstDayMsg", "lastDayMsg", "detailsMsg", "substituteMsg", "projgMgrMsg")
}
}
// Exiting paste mode, now interpreting.
import java.lang.reflect.{Method, InvocationHandler, Proxy}
defined object ProxyTesting
scala> ProxyTesting.test()
Proxy Funcion is called: firstDayMsg,lastDayMsg,detailsMsg,substituteMsg,projgMgrMsg
Actual Funcion is called
Proxy Funcion is called: 2nd time firstDayMsg,lastDayMsg,detailsMsg,substituteMsg,projgMgrMsg
scala>