Scala Parse String Class and Compile in Run-Time -


i want implement function in scala can parse string of source code (a class object) , compile object in run time.

for example, function have tried far. goal run compiled in run-time environment, can use constructor or function. code has run-time error don't understand how fix reflect class error. thanks!

object test {         def main(args: array[string]): unit = {             val m = universe.runtimemirror(getclass.getclassloader)             val tb = m.mktoolbox()             val clazz = tb.compile(tb.parse("class insideclass {\n    val model_field = 5\n   def insideclass(model: int) = {\n        val model_field = model \n    } \n\n    def test() : int = {\n        model_field\n    }\n\n}\nscala.reflect.classtag[insideclass].runtimeclass"))().asinstanceof[class[_]]             val classinside = universe.typeof[class[_]].typesymbol.asclass             val ctor = universe.typeof[class[_]].declaration(universe.nme.constructor).asmethod             val cm=m.reflectclass(classinside)             val ctorm=cm.reflectconstructor(ctor)             println(ctorm(10).test())         }     } 

the problem outside compiler not know "insideclass" exists class definition. 1 solution make inside class extend other class know both inside , outside compiler, example function[int, int]. need rename "test" method "apply" in case.

val clazz = tb.compile(tb.parse("class persondata(x:int) extends  function[int, int] {\n val allen = x.toint\n\n override def apply(x:int):int = allen}\n scala.reflect.classtag[persondata].runtimeclass"))().asinstanceof[class[_]]  val ctor = clazz.getdeclaredconstructors()(0)  val instance = ctor.newinstance(new integer(1)) // cast can succeed because outside knows function[int, int] println(instance.asinstanceof[function[int, int]].apply(1)) 

Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -