Search code examples
javascalaclasscastexceptionlagom

ClassCastException from subtype to supertype (in scala)


We are encountering a strange error consistently only on one dev machine, for others it works fine (personally I cannot reproduce the issue):

Caused by: java.lang.ClassCastException: scala.collection.immutable.$colon$colon cannot be cast to scala.collection.Seq
    at scala.Option.map (Option.scala:163)
    at com.lightbend.lagom.maven.RunAllMojo.$anonfun$execute$15 (ServiceMojos.scala:402)

Seq is a linear supertype of :: ($colon$colon) per https://www.scala-lang.org/api/2.12.0/scala/collection/immutable/$colon$colon.html. We use Lagom 1.6.0-M2 for Java DSLs, with Scala 2.12. Java version:

openjdk version "1.8.0_212"
OpenJDK Runtime Environment Corretto-8.212.04.2 (build 1.8.0_212-b04)
OpenJDK 64-Bit Server VM Corretto-8.212.04.2 (build 25.212-b04, mixed mode)

It happens during lagom dev-server bootstrap. Purging and redownloading the maven repo (especially scala-lang and lagom deps) did not solve this problem, so this might not be due to a corrupted file.

I diffed the scala jars on my machine with the ones on the bad machine, and they were the same.

What could cause a ClassCastException from a subtype to supertype, where should we start to investigate this issue?


Solution

  • It was a classloader issue as Alexey said.

    The core problem was that a maven plugin used the <extensions>true</extensions> config, which messed up the classloaders somehow.

    The actual issue was fixed, removing the extension flag solved the problem:

    https://discuss.lightbend.com/t/java-lang-classcastexception-scala-collection-immutable-colon-colon-cannot-be-cast-to-scala-collection-seq-during-runall/4821/3