Skip to content

Commit

Permalink
SI-6181 method mirrors now support by-name args
Browse files Browse the repository at this point in the history
Arguments provided in by-name positions are now automatically wrapped
in Function0 instances by method mirrors.
  • Loading branch information
xeno-by committed Aug 6, 2012
1 parent 3aa221e commit 3c4f486
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/reflect/scala/reflect/runtime/JavaMirrors.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import internal.Flags._
import ReflectionUtils.{singletonInstance}
import language.existentials
import scala.runtime.{ScalaRunTime, BoxesRunTime}
import scala.reflect.internal.util.Collections._

trait JavaMirrors extends internal.SymbolTable with api.JavaUniverse { self: SymbolTable =>

Expand Down Expand Up @@ -262,6 +263,7 @@ trait JavaMirrors extends internal.SymbolTable with api.JavaUniverse { self: Sym
// rather than have them on a hot path them in a unified implementation of the `apply` method
private def mkJavaMethodMirror[T: ClassTag](receiver: T, symbol: MethodSymbol): JavaMethodMirror = {
if (isMagicMethod(symbol)) new JavaMagicMethodMirror(receiver, symbol)
else if (symbol.params.flatten exists (p => isByNameParamType(p.info))) new JavaByNameMethodMirror(receiver, symbol)
else new JavaVanillaMethodMirror(receiver, symbol)
}

Expand All @@ -281,6 +283,14 @@ trait JavaMirrors extends internal.SymbolTable with api.JavaUniverse { self: Sym
def apply(args: Any*): Any = jmeth.invoke(receiver, args.asInstanceOf[Seq[AnyRef]]: _*)
}

private class JavaByNameMethodMirror(val receiver: Any, symbol: MethodSymbol)
extends JavaMethodMirror(symbol) {
def apply(args: Any*): Any = {
val transformed = map2(args.toList, symbol.params.flatten)((arg, param) => if (isByNameParamType(param.info)) () => arg else arg)
jmeth.invoke(receiver, transformed.asInstanceOf[Seq[AnyRef]]: _*)
}
}

private class JavaMagicMethodMirror[T: ClassTag](val receiver: T, symbol: MethodSymbol)
extends JavaMethodMirror(symbol) {
def apply(args: Any*): Any = {
Expand Down
1 change: 1 addition & 0 deletions test/files/run/t6181.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2
8 changes: 8 additions & 0 deletions test/files/run/t6181.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import scala.reflect.runtime.universe._
import scala.reflect.runtime.{currentMirror => cm}

object Test extends App {
class C { def test(x: => Int) = println(x) }
val mm = cm.reflect(new C).reflectMethod(typeOf[C].member(newTermName("test")).asMethod)
mm(2)
}

0 comments on commit 3c4f486

Please sign in to comment.