dimanche 3 septembre 2017

scala reflect constructor java.lang.NoSuchMethodException

I have some

case class Person(age: Int)

I'm using reflection. I do:

val argsMap = Map(age -> 27)
val aClass = classOf[Person]

Then, i get constructor mirror using scala reflection

val rm = runtimeMirror(aClass)
val primaryConstructor = getPrimaryConstructor(aClass)
// ordering args as in primary constructor
val args = primaryConstructor.paramLists.flatten.map { symbol =>
  argsMap(symbol.name.toString)
}
val classMirror = rm.reflectClass(rm.classSymbol(aClass))
val constructorMirror = classMirror.reflectConstructor(primaryConstructor)

Then I applying args to constructor mirror and everything is OK

Some(constructorMirror.apply(args: _*).asInstanceOf[T])

BUT when I do the same with own written Parser (scala parser combinator library), I get NoSuchMethodException. The root of exception is in

private Constructor<T> getConstructor0(Class<?>[] parameterTypes, int which) throws NoSuchMethodException

value Class[] parameterTypes has different values, this two cases: 1st case, when everything OK it contains "int" class, 2nd case, when throws exception in contains "scala.Int" class It's funny because both cases my map with arguments shows me java.lang.Integer

And the funniest thing is that error reproduces only when i call Parser instantiation before direct instantiation (case 2 before case 1), and when viceversa, everything is OK!

I suppose the problem is in some class loading order or so...

Give me please some food for my brain to solve this problem. Thanx.





Aucun commentaire:

Enregistrer un commentaire