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

reflection behaves badly with respect to value class fields #7328

Closed
scabug opened this issue Apr 4, 2013 · 7 comments
Closed

reflection behaves badly with respect to value class fields #7328

scabug opened this issue Apr 4, 2013 · 7 comments

Comments

@scabug
Copy link

scabug commented Apr 4, 2013

Reflection understands correctly the type of value-class fields, but the instance mirrors return values of the wrong type when accessing these fields:

import scala.reflect.runtime._

case class Foo(x: Int) extends AnyVal
case class Bar(foo: Foo)

object Test {
  def main(args: Array[String]) {
    val fooTerm = universe.typeOf[Bar].declaration(universe.newTermName("foo")).asMethod
    val fooType = fooTerm.returnType // correctly gives typeOf[Foo]
    println(fooType) // Foo

    val bar = Bar(Foo(3))
    println(bar.foo) // Foo(3)

    val im = currentMirror.reflect(bar)

    val obj1 = im.reflectField(fooTerm).get // incorrectly gives java.lang.Integer(3) not Foo(3)
    println(obj1) // 3

    val obj2 = im.reflectMethod(fooTerm)() // ditto
    println(obj2) // 3
  }
}
@scabug
Copy link
Author

scabug commented Apr 4, 2013

Imported From: https://issues.scala-lang.org/browse/SI-7328?orig=1
Reporter: Jeff Olson (jdolson)
Affected Versions: 2.10.1

@scabug
Copy link
Author

scabug commented Apr 9, 2013

@xeno-by said:
Workaround, courtesy of Jeff Olson:

  def getFieldValue(im: InstanceMirror, term: MethodSymbol): Any = {
    val rawValue = im.reflectMethod(term)()
    val tpe = term.returnType
    val symbol = tpe.typeSymbol.asClass
    if (symbol.isDerivedValueClass) {
      val ctor = tpe.declaration(nme.CONSTRUCTOR).asMethod
      val ctorMirror = mirror.reflectClass(symbol).reflectConstructor(ctor)
      ctorMirror(rawValue)
    }
    else rawValue
  }

@scabug
Copy link
Author

scabug commented Apr 11, 2013

Jeff Olson (jdolson) said:
This may be obvious, but setters are broken too:

import scala.reflect.runtime.universe._

case class Foo(n: Int) extends AnyVal
class Bar(var foo: Foo)

object Test {
  def main(args: Array[String]) {
    val bar = new Bar(Foo(3))

    val mirror = runtimeMirror(getClass.getClassLoader)
    val im = mirror.reflect(bar)
    val fooTerm = typeOf[Bar].declaration(newTermName("foo")).asTerm
    val fooField = im.reflectField(fooTerm)
    fooField.set(Foo(5)) // java.lang.IllegalArgumentException: Can not set int field Bar.foo to Foo
    fooField.set(5) // this works but probably shouldn't
    println(bar.foo)
  }
}

@scabug
Copy link
Author

scabug commented Apr 11, 2013

Jeff Olson (jdolson) said:
And constructors as well. However, in this case, there doesn't seem to be a workaround (at least that I've found so far):

import scala.reflect.runtime.universe._

case class Foo(n: Int) extends AnyVal
case class Bar(foo: Foo)

object Test { 
  def main(args: Array[String]) {
    val mirror = runtimeMirror(getClass.getClassLoader)
    val cm = mirror.reflectClass(typeOf[Bar].typeSymbol.asClass)
    val ctor = typeOf[Bar].declaration(nme.CONSTRUCTOR).asMethod
    val ctorm = cm.reflectConstructor(ctor)
    ctorm(Foo(3)) // java.lang.NoClassDefFoundError: no Java class corresponding to ErasedValueType(Foo) found
    ctorm(3)      // java.lang.NoClassDefFoundError: no Java class corresponding to ErasedValueType(Foo) found
  }
}

@scabug
Copy link
Author

scabug commented May 20, 2013

@JamesIry said:
2.10.2 is about to be cut. Kicking down the road and un-assigning to foster work stealing.

@scabug
Copy link
Author

scabug commented Jan 24, 2014

@xeno-by said:
The thing with constructors is #6411, which has just been fixed in a pull request.

@scabug
Copy link
Author

scabug commented Jan 24, 2014

@xeno-by said:
scala/scala#3409

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

No branches or pull requests

2 participants