Having a problem using reflected constructor

36 views
Skip to first unread message

Greg Zoller

unread,
Mar 11, 2015, 12:52:07 AM3/11/15
to scala...@googlegroups.com
Hello,

I'm trying to dynamically instantiate case classes from their reflected constructors.  For a simple case class with "normal" members this works easily.  But if my case class has a member that is a value class, strange things happen.


In this example you'll see my Simple class is easily constructed.  My Complex class (having a value class data member) is also "successfully" constructed--but... the second argument (the "you" field) is null!!  What happened there?  I have output to show that all arguments/types passed into the reflected constructor appear present and correct.

Anyone have any ideas what I'm missing?

Som Snytt

unread,
Mar 11, 2015, 2:06:58 AM3/11/15
to Greg Zoller, scala-user
Something is missing, for sure.

It seems to share the code path with:

https://issues.scala-lang.org/browse/SI-9102


--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Som Snytt

unread,
Mar 11, 2015, 2:38:46 AM3/11/15
to Greg Zoller, scala-user
Since the original reporter suggested the simple fix, I'll submit it and save myself a third round.

Welcome to Scala version 2.11.6-20150305-162549-e8e9c0d6b9 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_40).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import reflect.runtime._, universe._
import reflect.runtime._
import universe._

scala> class C { def f(i: Int, j: => Int) = i + j }
defined class C

scala> typeOf[C].member(TermName("f"))
res0: reflect.runtime.universe.Symbol = method f

scala> .asMethod
res1: reflect.runtime.universe.MethodSymbol = method f

scala> currentMirror reflect (new C)
res2: reflect.runtime.universe.InstanceMirror = instance mirror for C@74f7d1d2

scala> res2 reflectMethod res1
res3: reflect.runtime.universe.MethodMirror = method mirror for def f(i: scala.Int,j: => scala.Int): scala.Int (bound to C@74f7d1d2)

scala> res3(2,3)
res4: Any = 5


was:

scala> res3(2,3) 
java.lang.IllegalArgumentException
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:497)
  at scala.reflect.runtime.JavaMirrors$JavaMirror$JavaMethodMirror.jinvokeraw(JavaMirrors.scala:335)
  at scala.reflect.runtime.JavaMirrors$JavaMirror$JavaMethodMirror.jinvoke(JavaMirrors.scala:339)
  at scala.reflect.runtime.JavaMirrors$JavaMirror$JavaTransformingMethodMirror.apply(JavaMirrors.scala:436)
  ... 33 elided


Greg Zoller

unread,
Mar 11, 2015, 10:39:36 AM3/11/15
to scala...@googlegroups.com, gzo...@gmail.com
I'm confused a bit, here... Is this a note of clarification or is this a fix to my problem?

One difference here is that what you've shown requires an instance: currentMirror reflect (new C)

In my code the method I'm trying to call dynamically is the constructor, so I have nothing but the static class to reflect.

Som Snytt

unread,
Mar 11, 2015, 1:08:23 PM3/11/15
to Greg Zoller, scala-user
It's rather the same, but I added your use case to the test.

https://github.com/scala/scala/pull/4378/files

Reply all
Reply to author
Forward
0 new messages