Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use scala reflection #13

Open
squito opened this issue Dec 18, 2013 · 1 comment
Open

use scala reflection #13

squito opened this issue Dec 18, 2013 · 1 comment

Comments

@squito
Copy link
Contributor

squito commented Dec 18, 2013

switch to scala reflection, which will allow sumac to support types like List[Int]. Finally found the key thing required for this:

import scala.reflect.runtime.{universe => ru}
def getType[T: ru.TypeTag](obj: T) = ru.typeTag[T].tpe
class Foo
class Bar extends Foo
val b: Foo = new Bar
scala> getType(b)
res0: reflect.runtime.universe.Type = Foo

scala> b.getClass()
res1: Class[_ <: Foo] = class Bar


class Foo {
  def doStuff() = { 
    this.getClass().getDeclaredField("y")
  }
}

class Bar extends Foo {
  val x: Int = 17
  val y: List[Int] = List(1,2,3)
}


def getRuntimeType[A](item: A) = {
  val mirror = ru.runtimeMirror(this.getClass.getClassLoader)
  mirror.classSymbol(item.getClass).toType
}


val b : Foo = new Bar
scala> getRuntimeType(b).members.collect{case x if x.isTerm => x.asTerm}.filter{_.isGetter}.map{x => x -> x.typeSignature}.toMap
Map(value y -> => scala.List[scala.Int], value x -> => scala.Int)

http://stackoverflow.com/questions/17010237/using-scala-reflection-to-find-most-derived-runtime-type

@squito
Copy link
Contributor Author

squito commented Dec 20, 2013

scala reflection won't be thread safe until 2.11
https://issues.scala-lang.org/browse/SI-6240

this is a very real problem -- I ran into it immediately when writing unit tests. At first I just side-stepped it by turning off parallel execution of tests. But I realized, we just can't have Sumac be so unsafe. (even though it is entirely single-threaded, the users code might invoke reflection in another thread at the same time ... and then we're hosed.)

I think this means we have to wait till 2.11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant