jeudi 20 août 2020

Write method to a class dynamically at runtime in scala and create a jar

I would like to understand is there a way to write a method to existing class at runtime and to create a jar dynamically in scala.

So far i tried to create a class dynamically and able to run it thru reflection, however the class is dynamic class which isnt generated.

val mirror = runtimeMirror(getClass.getClassLoader)
val tb = ToolBox(mirror).mkToolBox() 

val function = q"def function(x: Int): Int = x + 2"
    val functionWrapper = "object FunctionWrapper { " + function + "}"
data.map(x => tb.eval(q"$functionSymbol.function($x)"))

i got this from other source, however the class is available only for this run and will not be generated.

i would like to add a function to the existing class at runtime and able to compile it and create a jar for it.

Kindly suggest me the way

Thanks in advance.





Aucun commentaire:

Enregistrer un commentaire