How to bind Class with Google Guice using a FQDN String - scala

I'm using Play 2.5 with the Guice dependency injection.
Normal bind works like:
bind(classOf[SomeClass]).to(classOf[DefaultClass])
I now only need to be able to bind a class where the className has been specified in the Configuration.
I tried something like:
val className = config.getString("someClass.className")
val x: Class[_] = Class.forName(className)
bind(classOf[SomeClass]).to(classOf[x])
But then the types are wrong.
It needs to be done via Guice, since the SomeClass has an argument that needs to be injected, otherwise I would have used something like
val className = config.getString("someClass.className")
val x = Class.forName(className).newInstance().asInstanceOf[SomeClass]
bind(classOf[SomeClass]).toInstance(x)
Does anybody have an idea on how to bind it via guice?

I think you are looking for something like this...
lazy val injector = (new GuiceApplicationBuilder).injector()
def inject[T : ClassTag]: T = {
injector.instanceOf[T]
}
This is the simplest version, but doesn't handle arguments. You would create a class and then call this as inject[SomeDep].
I've not found a good way of injecting on-the-fly, the only way of really doing it via #inject at the top most invoked class. We only use inject for unit tests at present.

Thanks for the reply, but I finally got it to work using Scala's Reflection.
object Reflection {
import scala.reflect.api
import reflect.runtime.universe._
import reflect.ClassTag
def classTagToClass[T: reflect.ClassTag]: Class[T] = {
def ctag = implicitly[reflect.ClassTag[T]]
ctag.runtimeClass.asInstanceOf[Class[T]]
}
def typeToClassTag[T: TypeTag]: ClassTag[T] = {
ClassTag[T]( typeTag[T].mirror.runtimeClass( typeTag[T].tpe ) )
}
def stringToTypeTag[A](name: String): TypeTag[A] = {
val c = Class.forName(name) // obtain java.lang.Class object from a string
val mirror = runtimeMirror(c.getClassLoader) // obtain runtime mirror
val sym = mirror.staticClass(name) // obtain class symbol for `c`
val tpe = sym.selfType // obtain type object for `c`
// create a type tag which contains above type object
TypeTag(mirror, new api.TypeCreator {
def apply[U <: api.Universe with Singleton](m: api.Mirror[U]): U#Type =
if (m eq mirror) {
tpe.asInstanceOf[U#Type]
}
else {
throw new IllegalArgumentException(s"Type tag defined in $mirror cannot be migrated to other mirrors.")
}
})
}
}
using the above object you can bind a class using it FQDN in the following way:
configuration.getString("config.className")
.map(className =>
bind(classOf[AbstractClass]).to(classTagToClass(typeToClassTag(stringToTypeTag[AbstractClass](className))))
).getOrElse(bind(classOf[AbstractClass]).to(classOf[AbstractClassImpl]))

Related

Is there anyway, in Scala, to get the Singleton type of something from the more general type?

I have a situation where I'm trying to use implicit resolution on a singleton type. This works perfectly fine if I know that singleton type at compile time:
object Main {
type SS = String with Singleton
trait Entry[S <: SS] {
type out
val value: out
}
implicit val e1 = new Entry["S"] {
type out = Int
val value = 3
}
implicit val e2 = new Entry["T"] {
type out = String
val value = "ABC"
}
def resolve[X <: SS](s: X)(implicit x: Entry[X]): x.value.type = {
x.value
}
def main(args: Array[String]): Unit = {
resolve("S") //implicit found! No problem
}
}
However, if I don't know this type at compile time, then I run into issues.
def main(args: Array[String]): Unit = {
val string = StdIn.readLine()
resolve(string) //Can't find implicit because it doesn't know the singleton type at runtime.
}
Is there anyway I can get around this? Maybe some method that takes a String and returns the singleton type of that string?
def getSingletonType[T <: SS](string: String): T = ???
Then maybe I could do
def main(args: Array[String]): Unit = {
val string = StdIn.readLine()
resolve(getSingletonType(string))
}
Or is this just not possible? Maybe you can only do this sort of thing if you know all of the information at compile-time?
If you knew about all possible implementations of Entry in compile time - which would be possible only if it was sealed - then you could use a macro to create a map/partial function String -> Entry[_].
Since this is open to extending, I'm afraid at best some runtime reflection would have to scan the whole classpath to find all possible implementations.
But even then you would have to embed this String literal somehow into each implementations because JVM bytecode knows nothing about mappings between singleton types and implementations - only Scala compiler does. And then use that to find if among all implementations there is one (and exactly one) that matches your value - in case of implicits if there are two of them at once in the same scope compilation would fail, but you can have more than one implementation as long as the don't appear together in the same scope. Runtime reflection would be global so it wouldn't be able to avoid conflicts.
So no, no good solution for making this compile-time dispatch dynamic. You could create such dispatch yourself by e.g. writing a Map[String, Entry[_]] yourself and using get function to handle missing pices.
Normally implicits are resolved at compile time. But val string = StdIn.readLine() becomes known at runtime only. Principally, you can postpone implicit resolution till runtime but you'll be able to apply the results of such resolution at runtime only, not at compile time (static types etc.)
object Entry {
implicit val e1 = ...
implicit val e2 = ...
}
import scala.reflect.runtime.universe._
import scala.reflect.runtime
import scala.tools.reflect.ToolBox
val toolbox = ToolBox(runtime.currentMirror).mkToolBox()
def resolve(s: String): Any = {
val typ = appliedType(
typeOf[Entry[_]].typeConstructor,
internal.constantType(Constant(s))
)
val instanceTree = toolbox.inferImplicitValue(typ, silent = false)
val instance = toolbox.eval(toolbox.untypecheck(instanceTree)).asInstanceOf[Entry[_]]
instance.value
}
resolve("S") // 3
val string = StdIn.readLine()
resolve(string)
// 3 if you enter S
// ABC if you enter T
// "scala.tools.reflect.ToolBoxError: implicit search has failed" otherwise
Please notice that I put implicits into the companion object of type class in order to make them available in the implicit scope and therefore in the toolbox scope. Otherwise the code should be modified slightly:
object EntryImplicits {
implicit val e1 = ...
implicit val e2 = ...
}
// val instanceTree = toolbox.inferImplicitValue(typ, silent = false)
// should be replaced with
val instanceTree =
q"""
import path.to.EntryImplicits._
implicitly[$typ]
"""
In your code import path.to.EntryImplicits._ is import Main._.
Load Dataset from Dynamically generated Case Class

What is the correct way to register generic type to spray-json-support in akka-http Scala?

So, I have this class:
case class Something[T](data: Option[T] = None)
And i register it like the instruction said in https://github.com/spray/spray-json and in https://doc.akka.io/docs/akka-http/current/common/json-support.html. Like this:
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
import spray.json.DefaultJsonProtocol
trait InternalJsonFormat extends SprayJsonSupport with DefaultJsonProtocol {
import spray.json._
implicit def somethingFormat[A :JsonFormat] = jsonFormat1(Something.apply[A])
}
And last i used complete from akka http directive. Like this:
import akka.http.scaladsl.server.Directives._
object ResponseIt extends InternalJsonFormat {
def apply[T](rawData: T) = {
val theResponse = Something(data = Some(rawData))
complete(theResponse)
}
}
And then i get an error in complete(theResponse). It said
Type mismatch, expected: ToResponseMarshallable, actual: Something[T]
===========================================================
I have try to edit the last code for debugging purpose, like this:
object ResponseIt extends InternalJsonFormat {
import spray.json._
def apply[T](rawData: T) = {
val theResponse = Something(data = Some(rawData))
val trying = theResponse.toJson
complete(theResponse)
}
}
and get new error in val trying = theResponse.toJson. like this:
No implicits found for parameter writer: JsonWriter[Something[T]]
So, i really confused what is wrong in my code?. Is there any correct way to use the spray json support in akka http?
Thanks in advance
You see, there is no evidence for existence of JsonFormat for your T here:
def apply[T](rawData: T) = {
// ^--- here
val theResponse = Something(data = Some(rawData))
val trying = theResponse.toJson
complete(theResponse)
}
One can rewrite this method to provide JsonFormat for generic T:
def apply[T](rawData: T)(implicit formatter: JsonFormat[T])

Vampiric methods, type provider macros and missing runtime.VolatileObjectRef

I am trying to write a type provider macro that uses the vampiric methods trick described in this gist; this is a minimal model of my implementation:
object DeriveFamily {
def minimal: Any = macro DeriveFamilyMacros.minimal
}
object DeriveFamilyMacros {
class body(tree: Any) extends StaticAnnotation
def bodyImpl(c: Context) = {
import c.universe._
val field = c.macroApplication.symbol
val bodyAnn = field.annotations.filter(_.tree.tpe <:< typeOf[body]).head
bodyAnn.tree.children.tail.head
}
def minimal(c: Context): c.Tree = {
import c.universe._
q"""object Foobar { val _x = "X"; #DeriveFamilyMacros.body(Foobar._x) def x: String = macro DeriveFamilyMacros.bodyImpl }; Foobar"""
}
}
This is a dumb example, the real implementation tries to use this trick to create bundles of derived typeclass instances that are accessible without reflective calls. The issue that both the real version and this one have is that they work fine if I use them in a function but they crash the compiler if I try to use them at the root of a class or object definition:
object Working {
def x = {
val foobar = DeriveFamily.minimal
println(foobar.x)
}
x // Prints "X"
}
object NotWorking {
val foobar = DeriveFamily.minimal
println(foobar.x)
/*
[trace] Stack trace suppressed: run last common/test:compileIncremental for the full output.
[error] (common/test:compileIncremental) java.lang.IllegalArgumentException: Could not find proxy for var Foobar$module: runtime.VolatileObjectRef in List(variable Foobar$module, value foobar, Object NotWorking, package <root>) (currentOwner= value <local NotWorking> )
*/
}
Any help in fixing this will be very welcome, thanks.

Proxy class support

Is there Scala way for runtime generation of proxy classes? not DynamicProxy but runtime types that extend provided class/interface and pass all calls through provided callback.
Java world uses cglib/javassist for that, but what is the best way to proxy in Scala?
def makeProxy[T](interceptor: Interceptor, implicit baseClass: Manifest[T]): T
Google says Scala macros can be used for this but I am unsure how.
Here is an example how (more-less) to do something like that with macro:
def testImpl[T : c.WeakTypeTag](c: Context): c.Expr[Any] = {
import c.universe._
val className = newTypeName(weakTypeTag[T].tpe.typeSymbol.name.toString) //not best way
val m = weakTypeOf[T].declarations.iterator.toList.map(_.asMethod) //`declaration` takes only current; `members` also takes inherited
.filter(m => !m.isConstructor && !m.isFinal).map { m => //all reflection info about method
q"""override def ${m.name} = 9""" //generating new method
}
c.Expr { q"""new $className { ..$m } """}
}
def t[T] = macro testImpl[T]
class Aaa{ def a = 7; def b = 8}
scala> t[Aaa].a
res39: Int = 9
scala> t[Aaa].b
res40: Int = 9
All such macro works only if overriden methods are not final as they can't change types (as it works in compile-time) - only create new and inherit. This example doesn't process classes with non-empty constructors and many other things. m here is instance of MethodSymbol and gives you full scala-style reflection about input class' method. You need only generate correct AST in response.
To read more about that:
macroses: http://docs.scala-lang.org/overviews/macros/overview.html
scala's runtime-reflection http://docs.scala-lang.org/overviews/reflection/environment-universes-mirrors.html
q"asiquotes" : http://docs.scala-lang.org/overviews/quasiquotes/expression-details.html
Another solution would be:
scala> def getClasss[T: ClassTag] = classTag[T].runtimeClass
getClasss: [T](implicit evidence$1: scala.reflect.ClassTag[T])Class[_]
Using this instance you can apply any asm/cglib/javassist or even DynamicProxy to it.

Getting TypeTag from a classname string

Simple question, how does one get a TypeTag from a classname?
So basically the TypeTag equivalent of Class.forName in Java.
Note: Manifest won't do for me here, I need a TypeTag. Although if there's a way to go from a manifest to a typetag, that would work too, as I can get a Manifest from the classname.
Manifests are deprecated and can probably disappear in future Scala versions. I don't think it is wise to rely on them. You can use only new Scala reflection to do what you want.
You can go from a string to a Class to get its class loader, and then it is possible to create a TypeTag from a Class through a mirror, as is outlined in this answer. Here is a slightly adapted piece of code demonstrating it:
import scala.reflect.runtime.universe._
import scala.reflect.api
def stringToTypeTag[A](name: String): TypeTag[A] = {
val c = Class.forName(name) // obtain java.lang.Class object from a string
val mirror = runtimeMirror(c.getClassLoader) // obtain runtime mirror
val sym = mirror.staticClass(name) // obtain class symbol for `c`
val tpe = sym.selfType // obtain type object for `c`
// create a type tag which contains above type object
TypeTag(mirror, new api.TypeCreator {
def apply[U <: api.Universe with Singleton](m: api.Mirror[U]) =
if (m eq mirror) tpe.asInstanceOf[U # Type]
else throw new IllegalArgumentException(s"Type tag defined in $mirror cannot be migrated to other mirrors.")
})
}
You can use Class.forName to get a Class from a String, ManifestFactory.classType to get a Manifest from the Class, and then scala.reflect.runtime.universe.manifestToTypeTag to get a TypeTag:
import scala.reflect.runtime.universe
import scala.reflect.ManifestFactory
val className = "java.lang.String"
val mirror = universe.runtimeMirror(getClass.getClassLoader)
val cls = Class.forName(className)
val t = universe.manifestToTypeTag(mirror,
ManifestFactory.classType(cls))