Using SORM with Play Framework causes reflection e

2020-03-25 16:25发布

问题:

I've been trying to get SORM working with Play Framework 2.2-SNAPSHOT as well as 2.1.1. Currently I'm trying to run a minimalistic sample application that I created in order to track down the issue more easily. Unfortunately the error message I receive doesn't help me at all.

val appDependencies = Seq(
    "org.sorm-framework" % "sorm" % "0.3.8",
    "com.h2database" % "h2" % "1.3.168"
)

# Database configuration
#
db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:mem:play"
# db.default.user=sa
# db.default.password=""

case class Car( brand: String, wheels: Int )

object Db extends Instance( Seq( Entity[Car]() ), "jdbc:h2:mem:play" )

object Application extends Controller {
  def index = Action {
    Db.query[Car].count()
    Ok("hello wolrd")
  }
}

Results in this stacktrace:

play.api.Application$$anon$1: Execution exception[[RuntimeException: java.lang.NoSuchMethodError: scala.reflect.internal.TreeInfo.firstArgument(Lscala/reflect/internal/Trees$Tree;)Lscala/reflect/internal/Trees$Tree;]]
    at play.api.Application$class.handleError(Application.scala:289) ~[play_2.10.jar:2.1.1]
    at play.api.DefaultApplication.handleError(Application.scala:383) [play_2.10.jar:2.1.1]
    at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$17$$anonfun$apply$24.apply(PlayDefaultUpstreamHandler.scala:326) [play_2.10.jar:2.1.1]
    at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$17$$anonfun$apply$24.apply(PlayDefaultUpstreamHandler.scala:324) [play_2.10.jar:2.1.1]
    at play.api.libs.concurrent.PlayPromise$$anonfun$extend1$1.apply(Promise.scala:113) [play_2.10.jar:2.1.1]
    at play.api.libs.concurrent.PlayPromise$$anonfun$extend1$1.apply(Promise.scala:113) [play_2.10.jar:2.1.1]
java.lang.RuntimeException: java.lang.NoSuchMethodError: scala.reflect.internal.TreeInfo.firstArgument(Lscala/reflect/internal/Trees$Tree;)Lscala/reflect/internal/Trees$Tree;
    at play.api.mvc.ActionBuilder$$anon$1.apply(Action.scala:222) ~[play_2.10.jar:2.1.1]
    at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$2$$anonfun$apply$5$$anonfun$apply$6.apply(Action.scala:109) ~[play_2.10.jar:2.1.1]
    at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$2$$anonfun$apply$5$$anonfun$apply$6.apply(Action.scala:109) ~[play_2.10.jar:2.1.1]
    at play.utils.Threads$.withContextClassLoader(Threads.scala:18) ~[play_2.10.jar:2.1.1]
    at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$2$$anonfun$apply$5.apply(Action.scala:108) ~[play_2.10.jar:2.1.1]
    at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$2$$anonfun$apply$5.apply(Action.scala:106) ~[play_2.10.jar:2.1.1]
Caused by: java.lang.NoSuchMethodError: scala.reflect.internal.TreeInfo.firstArgument(Lscala/reflect/internal/Trees$Tree;)Lscala/reflect/internal/Trees$Tree;
    at scala.tools.nsc.typechecker.Typers$Typer.parentTypes(Typers.scala:1550) ~[scala-compiler.jar:na]
    at scala.tools.nsc.typechecker.Namers$Namer.templateSig(Namers.scala:861) ~[scala-compiler.jar:na]
    at scala.tools.nsc.typechecker.Namers$Namer.getSig$1(Namers.scala:1300) ~[scala-compiler.jar:na]
    at scala.tools.nsc.typechecker.Namers$Namer.typeSig(Namers.scala:1347) ~[scala-compiler.jar:na]
    at scala.tools.nsc.typechecker.Namers$Namer$$anonfun$monoTypeCompleter$1$$anonfun$apply$1.apply$mcV$sp(Namers.scala:709) ~[scala-compiler.jar:na]
    at scala.tools.nsc.typechecker.Namers$Namer$$anonfun$monoTypeCompleter$1$$anonfun$apply$1.apply(Namers.scala:708) ~[scala-compiler.jar:na]

回答1:

Okay. It's not a bug. SORM 0.3.8 depends on Scala 2.10.1 and Play 2.1.x uses Scala 2.10.0. The exceptions you get are caused by Play mixing artifacts from both Scala versions.

To fix this issue all you need to do is just tell Play to use a proper Scala version by adding scalaVersion := "2.10.1" to project settings in a file project/Build.scala.

The final build script may look like this:

object ApplicationBuild extends Build {

  val appName         = "play-test"
  val appVersion      = "1.0-SNAPSHOT"

  val appDependencies = Seq(
    "org.sorm-framework" % "sorm" % "0.3.8",
    "com.h2database" % "h2" % "1.3.168"
  )

  val main = play.Project(appName, appVersion, appDependencies).settings(
    resolvers += 
      "Local Maven Repository" at 
      "file:///"+Path.userHome.absolutePath+"/.m2/repository",
    scalaVersion := "2.10.1" // <--- ! This is the fix !
  )

}