7

I wish to use Quasar in my SBT project. Since Scala is not yet supported the only viable option left is to have SBT compile some java classes that use of Quasar.

I tried putting

javaOptions += "-javaagent:PATH_TO_JAR/quasar-core-0.5.0.jar"

and

fork := true

as I read that for using e.g. JRebel one must insert both these statements into build.sbt

But it does not seem to work either as using a Quasarish class (QuasarExample) yields:

[error]    IllegalArgumentException: : Fiber class HelloWorldSpec$$anonfun$1$$anonfun$apply$3$$anon$1 has not been instrumented.  (Fiber.java:151)
[error] co.paralleluniverse.fibers.Fiber.<init>(Fiber.java:151)
[error] co.paralleluniverse.fibers.Fiber.<init>(Fiber.java:171)
[error] co.paralleluniverse.fibers.Fiber.<init>(Fiber.java:448)

A piece of code that's expected to be running without errors after successful instrumentation:

new Fiber<Integer>() {
    @Override
    protected Integer run() throws SuspendExecution, InterruptedException {
        return 0;
    }
}.start();

See also this repository for a starter.

4

1 回答 1

5

根据@mjaskowski 的评论,你打算检测 Scala 编译器生成的字节码吗?, Scala 还不是受支持的语言。

简短的回答是:绝对。更长的答案是,是的,但不会很快。Scala 是一种非常(非常、非常、非常、非常)复杂的语言(哦,我的上帝,它是如此难以置信的复杂!),并且需要很长时间来测试具有许多(多对多)Scala 特性的 Quasar。鉴于 Scala 开发人员的数量相对较少,而且集成成本很高,这可能需要一些时间。但是我们支持 Clojure(更简单的集成),并且很快可能(或者我们已经支持)支持 Kotlin(简单的集成)。

继续查看在 sbt 中设置 javaagent 的可能方法。

这是我第一次接触 Quasar,所以这些步骤非常基本,但它们应该让你有个良好的开端。

单独在 JVM 中运行 Quasar 的 javaagent

只有使用以下命令才能在没有任何 Scala/sbt jar 的情况下运行 Quasar 的 javaagent。

java -cp /Users/jacek/.ivy2/cache/org.ow2.asm/asm-util/jars/asm-util-5.0.1.jar:/Users/jacek/.ivy2/cache/org.ow2.asm /asm-commons/jars/asm-commons-5.0.1.jar:/Users/jacek/.ivy2/cache/org.ow2.asm/asm/jars/asm-5.0.1.jar -javaagent:/Users/ jacek/.ivy2/cache/co.paralleluniverse/quasar-core/jars/quasar-core-0.5.0.jar -help

它似乎不是独立的,需要在CLASSPATH.

使用 scala 运行 Quasar 的 javaagent

以下命令使scalaQuasar 的 javaagent 高兴。

scala -debug -usebootcp -toolcp /Users/jacek/.ivy2/cache/org.ow2.asm/asm-tree/jars/asm-tree-5.0.1.jar:/Users/jacek/.ivy2/cache/org .ow2.asm/asm-util/jars/asm-util-5.0.1.jar:/Users/jacek/.ivy2/cache/org.ow2.asm/asm-commons/jars/asm-commons-5.0.1 .jar:/Users/jacek/.ivy2/cache/org.ow2.asm/asm/jars/asm-5.0.1.jar:/Users/jacek/.ivy2/cache/org.ow2.asm/asm-analysis /jars/asm-analysis-5.0.1.jar -J-javaagent:/Users/jacek/.ivy2/cache/co.paralleluniverse/quasar-core/jars/quasar-core-0.5.0.jar

使用 sbt 运行 Quasar 的初步尝试

在以下位置添加 Quasar 的必要依赖项build.sbt

val quasarDeps = Seq(
  "quasar-core",
  "quasar-actors",
  "quasar-galaxy"
)

libraryDependencies := quasarDeps.map("co.paralleluniverse" % _ % "0.5.0")

它归结为增加libraryDependencies必要的罐子。我选择了所有可用的。

我在 Scalaconsole中检查了这些库是否可用。

➜  quasar-sbt  xsbt
[info] Loading global plugins from /Users/jacek/.sbt/0.13/plugins
[info] Set current project to quasar-sbt (in build file:/Users/jacek/sandbox/quasar-sbt/)
> console
Welcome to Scala version 2.10.4 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_60).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import co.paralleluniverse.fibers._
import co.paralleluniverse.fibers._

scala> Fiber.DEFAULT_STACK_SIZE
res0: Int = 32

您还必须添加 java 代理。将以下内容添加到build.sbt

// FIXME the path to quasar-core should be simpler
javaOptions in Test += "-javaagent:/Users/jacek/.ivy2/cache/co.paralleluniverse/quasar-core/jars/quasar-core-0.5.0.jar"

fork in Test := true

// only needed for Specs2 so I could test the test setup
libraryDependencies += "org.specs2" %% "specs2" % "2.3.13" % Test

scalacOptions in Test ++= Seq("-Yrangepos")

reload构建更改。

> reload
[info] Loading global plugins from /Users/jacek/.sbt/0.13/plugins
[info] Set current project to quasar-sbt (in build file:/Users/jacek/sandbox/quasar-sbt/)

编写测试以确保所有设置正确。我使用了HelloWorldSpecfrom[Specs2][2]进行了一些更改:

import org.specs2.mutable._

import co.paralleluniverse.fibers._

class HelloWorldSpec extends Specification {

  "Quasar" should {
    "Fiber.DEFAULT_STACK_SIZE defaults to 32" in {
      Fiber.DEFAULT_STACK_SIZE === 32
    }
  }
}

这不是一个非常广泛的测试,因此请根据您的需要进行调整。将文件保存在src/test/scala.

执行test

> test
objc[27427]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.7.0_60.jdk/Contents/Home/jre/bin/java and /Library/Java/JavaVirtualMachines/jdk1.7.0_60.jdk/Contents/Home/jre/lib/libinstrument.dylib. One of the two will be used. Which one is undefined.
[info] HelloWorldSpec
[info]
[info] Quasar should
[info] + Fiber.DEFAULT_STACK_SIZE defaults to 32
[info]
[info] Total for specification HelloWorldSpec
[info] Finished in 40 ms
[info] 1 example, 0 failure, 0 error
[info] Passed: Total 1, Failed 0, Errors 0, Passed 1
[success] Total time: 4 s, completed Jul 13, 2014 2:04:13 PM

有用!

于 2014-07-13T12:07:40.223 回答