Created
September 30, 2015 10:07
-
-
Save magro/9ad0e607f3c23d1621b9 to your computer and use it in GitHub Desktop.
Scala proxy for lazy values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.concurrent.atomic.AtomicReference | |
import scala.reflect.ClassTag | |
import java.lang.reflect.{Proxy => JProxy, Method, InvocationHandler} | |
/** | |
* Creates a proxy for a lazy value, in a project moving to dependency injection this | |
* was meant to replace legacy stuff where at app start time dependencies were loaded | |
* too early. | |
* | |
* Not sure if this is useful in another context, because `lazy` should be enough in the | |
* normal world and a runtime DI solution should resolve dependencies on its own... | |
*/ | |
object Proxy { | |
def apply[T](provider: => T)(implicit ct: ClassTag[T]): T = { | |
val clazz = ct.runtimeClass.asInstanceOf[Class[T]] | |
val handler = new InvocationHandler { | |
private val proxied = new AtomicReference[T]() | |
override def invoke(proxy: scala.Any, method: Method, args: Array[AnyRef]): AnyRef = { | |
if(proxied.get() == null) proxied.set(provider) | |
method.invoke(proxied.get(), args:_ *) | |
} | |
} | |
JProxy.newProxyInstance(clazz.getClassLoader, Array(clazz), handler).asInstanceOf[T] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment