ローカル変数の一覧を取得
Posted feedbacks - Scala
ひたすら厳しい・・・。
とりあえずJDIで無理やり。
JAVA_OPTSに
-Xrunjdwp:transport=dt_socket,address=5056,server=y,suspend=n
と設定して実行します。なにかいい方法があったら是非教えてほしいです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | import com.sun.jdi._
import com.sun.jdi.connect._
import com.sun.jdi.connect.Connector._
import scala.collection.mutable.JavaMapAdaptor
import scala.collection.mutable.HashMap
class _localVariables(threadName:String) extends Thread {
var result:HashMap[String, AnyRef] = null
override def run = {
val vmm = Bootstrap.virtualMachineManager
val acs = vmm.attachingConnectors().toArray.map(_.asInstanceOf[AttachingConnector])
val ac = acs.find(_.name == "com.sun.jdi.SocketAttach").get
val args = ac.defaultArguments
val portNumber = args.get("port").asInstanceOf[IntegerArgument]
portNumber.setValue(5056)
val hostname = args.get("hostname").asInstanceOf[StringArgument]
hostname.setValue("localhost")
val vm = ac.attach(args)
val threads = vm.allThreads.toArray.map(_.asInstanceOf[ThreadReference])
val mainthread = threads.find(_.name==threadName).get
mainthread.suspend
val f = mainthread.frame(4).asInstanceOf[StackFrame]
val result = new
JavaMapAdaptor[LocalVariable,Value](f.getValues(f.visibleVariables))
this.result = result.foldLeft(new HashMap[String, AnyRef]){(r, p) =>
r(p._1.name) = p._2
r
}
mainthread.resume
}
}
def localVariables = {
val th = new _localVariables(currentThread.getName)
th.start
th.join
th.result
}
def foo = {
val x = 1
val y = "hoge"
localVariables
}
println(foo)
|



にしお
#3391()
Rating0/0=0.00
Pythonで表現すると、下のコードの???部分を埋めることになります。
>>> def foo(): x = 1 y = "hello" ??? return result >>> foo() {'y': 'hello', 'x': 1}[ reply ]