Last active
April 12, 2018 08:01
-
-
Save maropu/49005000c0eee3f298543823273e4190 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///////// Invocation of Scala collection object methods ///////// | |
--- | |
scala> import scala.reflect.runtime.universe._ | |
scala> val mapClazz = scala.collection.immutable.Map.getClass | |
mapClazz: Class[_ <: scala.collection.immutable.Map.type] = class scala.collection.immutable.Map$ | |
scala> val mirror = runtimeMirror(mapClazz.getClassLoader) | |
mirror: reflect.runtime.universe.Mirror = JavaMirror with ... | |
scala> val moduleSymbol = mirror.moduleSymbol(mapClazz) | |
moduleSymbol: reflect.runtime.universe.ModuleSymbol = object Map | |
scala> val instanceMirror = mirror.reflect(moduleSymbol) | |
scala> val toStringSymbol = moduleSymbol.typeSignature.member(TermName("toString")) | |
toStringSymbol: reflect.runtime.universe.Symbol = method toString | |
scala> instanceMirror.reflectMethod(toStringSymbol.asMethod)() // OK | |
res9: Any = object Map | |
scala> val newBuilderSymbol = moduleSymbol.typeSignature.member(TermName("newBuilder")) | |
newBuilderSymbol: reflect.runtime.universe.Symbol = method newBuilder | |
scala> instanceMirror.reflectMethod(newBuilderSymbol.asMethod)() // NG | |
scala.ScalaReflectionException: expected a member of <$anon: Symbols.this.ModuleSymbol with SynchronizedSymbols.this.SynchronizedTermSymbol>, you provided method scala.collection.generic.GenMapFactory.newBuilder | |
at scala.reflect.runtime.JavaMirrors$JavaMirror.scala$reflect$runtime$JavaMirrors$JavaMirror$$abort(JavaMirrors.scala:115) | |
at scala.reflect.runtime.JavaMirrors$JavaMirror.scala$reflect$runtime$JavaMirrors$JavaMirror$$ErrorNotMember(JavaMirrors.scala:121) | |
at scala.reflect.runtime.JavaMirrors$JavaMirror$$anonfun$scala$reflect$runtime$JavaMirrors$JavaMirror$$checkMemberOf$1.apply(JavaMirrors.scala:214) | |
at scala.reflect.runtime.JavaMirrors$JavaMirror.ensuringNotFree(JavaMirrors.scala:204) | |
at scala.reflect.runtime.JavaMirrors$JavaMirror.scala$reflect$runtime$JavaMirrors$JavaMirror$$checkMemberOf(JavaMirrors.scala:213) | |
at scala.reflect.runtime.JavaMirrors$JavaMirror$JavaInstanceMirror.reflectMethod(JavaMirrors.scala:247) | |
at scala.reflect.runtime.JavaMirrors$JavaMirror$JavaInstanceMirror.reflectMethod(JavaMirrors.scala:233) | |
///////// Invocation of companion object methods ///////// | |
--- | |
scala> :paste | |
object TestObj { | |
def method1(): String = "test" | |
def method2[T](): T = null.asInstanceOf[T] | |
} | |
scala> import scala.reflect.runtime.universe._ | |
scala> val mirror = runtimeMirror(this.getClass.getClassLoader) | |
mirror: reflect.runtime.universe.Mirror = JavaMirror with ... | |
scala> val instanceMirror = mirror.reflect(TestObj) | |
instanceMirror: reflect.runtime.universe.InstanceMirror = instance mirror for TestObj$@5afa3c9 | |
scala> val toStringSymbol = typeOf[TestObj.type].member(TermName("toString")) | |
toStringSymbol: reflect.runtime.universe.Symbol = method toString | |
scala> instanceMirror.reflectMethod(toStringSymbol.asMethod)() // OK | |
res6: Any = TestObj$@1f1c7bf6 | |
scala> val test1Symbol = typeOf[TestObj.type].decl(TermName("method1")) | |
test1Symbol: reflect.runtime.universe.Symbol = method method1 | |
scala> instanceMirror.reflectMethod(test1Symbol.asMethod)() // OK | |
res2: Any = test | |
scala> val test2Symbol = typeOf[TestObj.type].decl(TermName("method2")) | |
test2Symbol: reflect.runtime.universe.Symbol = method method2 | |
scala> instanceMirror.reflectMethod(test2Symbol.asMethod)() // OK | |
res3: Any = null | |
--- | |
scala> :paste | |
object TestObj { | |
def method1(): String = "test" | |
def method2[T](): T = null.asInstanceOf[T] | |
} | |
scala> import scala.reflect.runtime.universe._ | |
scala> val clazz = TestObj.getClass | |
clazz: Class[_ <: TestObj.type] = class TestObj$ | |
scala> val mirror = runtimeMirror(clazz.getClassLoader) | |
mirror: reflect.runtime.universe.Mirror = JavaMirror with ... | |
scala> val moduleSymbol = mirror.moduleSymbol(clazz) | |
moduleSymbol: reflect.runtime.universe.ModuleSymbol = object TestObj | |
scala> val instanceMirror = mirror.reflect(moduleSymbol) | |
instanceMirror: reflect.runtime.universe.InstanceMirror = instance mirror for object TestObj | |
scala> val toStringSymbol = moduleSymbol.typeSignature.member(TermName("toString")) | |
toStringSymbol: reflect.runtime.universe.Symbol = method toString | |
scala> instanceMirror.reflectMethod(toStringSymbol.asMethod)() | |
res6: Any = object TestObj | |
scala> val test1Symbol = moduleSymbol.typeSignature.decl(TermName("method1")) | |
test1Symbol: reflect.runtime.universe.Symbol = method method1 | |
scala> instanceMirror.reflectMethod(test1Symbol.asMethod)() | |
scala.ScalaReflectionException: expected a member of <$anon: Symbols.this.ModuleSymbol with SynchronizedSymbols.this.SynchronizedTermSymbol>, you provided method TestObj.method1 | |
at scala.reflect.runtime.JavaMirrors$JavaMirror.scala$reflect$runtime$JavaMirrors$JavaMirror$$abort(JavaMirrors.scala:115) | |
at scala.reflect.runtime.JavaMirrors$JavaMirror.scala$reflect$runtime$JavaMirrors$JavaMirror$$ErrorNotMember(JavaMirrors.scala:121) | |
at scala.reflect.runtime.JavaMirrors$JavaMirror$$anonfun$scala$reflect$runtime$JavaMirrors$JavaMirror$$checkMemberOf$1.apply(JavaMirrors.scala:214) | |
at scala.reflect.runtime.JavaMirrors$JavaMirror.ensuringNotFree(JavaMirrors.scala:204) | |
at scala.reflect.runtime.JavaMirrors$JavaMirror.scala$reflect$runtime$JavaMirrors$JavaMirror$$checkMemberOf(JavaMirrors.scala:213) | |
at scala.reflect.runtime.JavaMirrors$JavaMirror$JavaInstanceMirror.reflectMethod(JavaMirrors.scala:247) | |
at scala.reflect.runtime.JavaMirrors$JavaMirror$JavaInstanceMirror.reflectMethod(JavaMirrors.scala:233) | |
... 33 elided | |
scala> val test2Symbol = moduleSymbol.typeSignature.decl(TermName("method2")) | |
test2Symbol: reflect.runtime.universe.Symbol = method method2 | |
scala> instanceMirror.reflectMethod(test2Symbol.asMethod)() | |
scala.ScalaReflectionException: expected a member of <$anon: Symbols.this.ModuleSymbol with SynchronizedSymbols.this.SynchronizedTermSymbol>, you provided method TestObj.method2 | |
at scala.reflect.runtime.JavaMirrors$JavaMirror.scala$reflect$runtime$JavaMirrors$JavaMirror$$abort(JavaMirrors.scala:115) | |
at scala.reflect.runtime.JavaMirrors$JavaMirror.scala$reflect$runtime$JavaMirrors$JavaMirror$$ErrorNotMember(JavaMirrors.scala:121) | |
at scala.reflect.runtime.JavaMirrors$JavaMirror$$anonfun$scala$reflect$runtime$JavaMirrors$JavaMirror$$checkMemberOf$1.apply(JavaMirrors.scala:214) | |
at scala.reflect.runtime.JavaMirrors$JavaMirror.ensuringNotFree(JavaMirrors.scala:204) | |
at scala.reflect.runtime.JavaMirrors$JavaMirror.scala$reflect$runtime$JavaMirrors$JavaMirror$$checkMemberOf(JavaMirrors.scala:213) | |
at scala.reflect.runtime.JavaMirrors$JavaMirror$JavaInstanceMirror.reflectMethod(JavaMirrors.scala:247) | |
at scala.reflect.runtime.JavaMirrors$JavaMirror$JavaInstanceMirror.reflectMethod(JavaMirrors.scala:233) | |
... 33 elided | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment