lundi 15 avril 2019

Constructor.newInstance replaces Scala object

I was debugging a deserialization issue with Jackson where Scala object instances seemed to be replaced. I managed to drill the issue down to this code:

object WaitWhat extends App {

  object XX

  val x1 = XX

  // Notice: no assignment!
  XX.getClass.getConstructor().newInstance()

  val x2 = XX

  println(x1)
  println(x2)
}

The output is:

WaitWhat$XX$@5315b42e
WaitWhat$XX$@2ef9b8bc

(Of course the actual hash codes change each run.)

IntelliJ's debugger also indicates that x1 and x2 really are different instances, despite the fact that the result of newInstance is completely ignored.

I would have expected a no-op, or an exception of some kind. How is it possible that the actual object instance gets replaced by this call?





Aucun commentaire:

Enregistrer un commentaire