From d7b99c3c33de8d759353c7ffd1d89e2cb38d793b Mon Sep 17 00:00:00 2001 From: Som Snytt Date: Tue, 10 Mar 2015 23:54:39 -0700 Subject: SI-9102: Reflect method invoke with mixed args A missing default branch when a method had value class or by-name params caused other args to present as null under reflective invocation. --- test/files/run/t9102.scala | 73 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 test/files/run/t9102.scala (limited to 'test/files/run/t9102.scala') diff --git a/test/files/run/t9102.scala b/test/files/run/t9102.scala new file mode 100644 index 0000000000..37ae53b2ab --- /dev/null +++ b/test/files/run/t9102.scala @@ -0,0 +1,73 @@ + +object Test extends App { + import reflect.runtime._, universe._ + + class C { def f(i: Int, j: => Int) = i + j } + + class V(val v: Int) extends AnyVal { def doubled = 2 * v } + class D { def f(i: Int, j: V) = i + j.doubled } + + locally { + val ms = typeOf[C].member(TermName("f")).asMethod + val im = currentMirror reflect (new C) + val mm = im reflectMethod ms + assert(mm(2,3) == 5) + } + locally { + val ms = typeOf[D].member(TermName("f")).asMethod + val im = currentMirror reflect (new D) + val mm = im reflectMethod ms + assert(mm(2, new V(3)) == 8) + } +} + +/* Session tests without special init code should reside in simple script files. + * Also, provide filters such as for `(bound to C@74f7d1d2)`. + +import scala.tools.partest.SessionTest + +object Test extends SessionTest { +//Welcome to Scala version 2.11.6 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_40). + def session = + s"""|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 + | + |scala> :quit""" +} +*/ + +/* 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 +*/ + -- cgit v1.2.3