mercredi 22 janvier 2020

getClass.getFields returns fields from Scala Reflection library instead of fields from package

I am using the Scala reflection library to access and execute the main() function of a jar that I have attached to my project. I can do this much successfully, but now I would like to use Reflection to access a specific field in this jar and set its value at runtime.

However, when I run the code below, fields.map(f => print(f)) prints the fields of the Scala Reflection library, not the fields of the jar. Any idea why this is happening? I've been searching for a resolution for a week, and would greatly appreciate any help!

Code:

val rm = scala.reflect.runtime.currentMirror

val staticClass = rm.staticModule("com.dummy.package.name")

val im1 = rm.reflectModule(staticClass)

val x = im1.instance

val im = rm.reflect(x)

val fields = staticClass.getClass.getDeclaredFields

fields.map(f => print(f))

Output:

public final scala.reflect.internal.Symbols$Symbol scala.reflect.internal.Symbols$Symbol.scala$reflect$internal$Symbols$Symbol$$initOwner public final scala.reflect.internal.Names$Name scala.reflect.internal.Symbols$Symbol.scala$reflect$internal$Symbols$Symbol$$initName public final scala.reflect.internal.SymbolTable scala.reflect.internal.Symbols$SymbolContextApiImpl.$outer





Aucun commentaire:

Enregistrer un commentaire