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
Comments
Imported From: https://issues.scala-lang.org/browse/SI-7328?orig=1 |
@xeno-by said: 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
} |
Jeff Olson (jdolson) said: 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)
}
} |
Jeff Olson (jdolson) said: 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
}
} |
@JamesIry said: |
@xeno-by said: |
Reflection understands correctly the type of value-class fields, but the instance mirrors return values of the wrong type when accessing these fields:
The text was updated successfully, but these errors were encountered: