diff options
author | Adriaan Moors <adriaan.moors@typesafe.com> | 2014-12-23 10:06:50 -0800 |
---|---|---|
committer | Adriaan Moors <adriaan.moors@typesafe.com> | 2014-12-23 10:06:50 -0800 |
commit | 841389bc6f879031a8d36a5ffcca5e3c0e1fdcef (patch) | |
tree | 898ac7b039c94779f1e52a7d8d0ebaccf2537c01 /test/files/run/t7965.scala | |
parent | a0b11b32aca74a4c3aee2e8c70356a29cd937727 (diff) | |
parent | 467872999e4940e06f2da811d1ae2d8278639126 (diff) | |
download | scala-841389bc6f879031a8d36a5ffcca5e3c0e1fdcef.tar.gz scala-841389bc6f879031a8d36a5ffcca5e3c0e1fdcef.tar.bz2 scala-841389bc6f879031a8d36a5ffcca5e3c0e1fdcef.zip |
Merge pull request #4139 from retronym/ticket/7965
SI-7965 Support calls to MethodHandle.{invoke,invokeExact}
Diffstat (limited to 'test/files/run/t7965.scala')
-rw-r--r-- | test/files/run/t7965.scala | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/test/files/run/t7965.scala b/test/files/run/t7965.scala new file mode 100644 index 0000000000..df80d4b5bb --- /dev/null +++ b/test/files/run/t7965.scala @@ -0,0 +1,54 @@ +// Test that scala doesn't apply boxing or varargs conversions to the +// @PolymorphicSignature magical methods, MethodHandle#{invoke, invokeExact} +object Test { + val code = """ + +object O { + private def foo = "foo" + private def bar(x: Int): Int = -x + private def baz(x: Box): Unit = x.a = "present" + val lookup = java.lang.invoke.MethodHandles.lookup +} + +import java.lang.invoke._ +class Box(var a: Any) + +object Test { + def main(args: Array[String]): Unit = { + def lookup(name: String, params: Array[Class[_]], ret: Class[_]) = { + val mt = MethodType.methodType(ret, params) + O.lookup.findVirtual(O.getClass, name, mt) + } + val fooResult = (lookup("foo", Array(), classOf[String]).invokeExact(O): Int) + assert(fooResult == "foo") + + val barResult = (lookup("bar", Array(classOf[Int]), classOf[Int]).invokeExact(O, 42): Int) + assert(barResult == -42) + + val box = new Box(null) + (lookup("baz", Array(classOf[Box]), Void.TYPE).invokeExact(O, box) : Unit) + assert(box.a == "present") + + // Note: Application in statement position in a block in Java also infers return type of Unit, + // but we don't support that, ascribe the type to Unit as above. + // as done in Java. + // lookup("baz", Array(classOf[Box]), Void.TYPE).invokeExact(O, box) + () + } +} + +""" + def main(args: Array[String]): Unit = { + if (util.Properties.isJavaAtLeast("1.7")) test() + } + + def test() { + import scala.reflect.runtime._ + import scala.tools.reflect.ToolBox + + val m = currentMirror + val tb = m.mkToolBox() + import tb._ + eval(parse(code)) + } +} |