本文整理汇总了Scala中scala.collection.JavaConverters类的典型用法代码示例。如果您正苦于以下问题:Scala JavaConverters类的具体用法?Scala JavaConverters怎么用?Scala JavaConverters使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了JavaConverters类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: JsTools
//设置package包名称以及导入依赖的类
package io.really.js
import javax.script.{ ScriptEngine, ScriptContext, Invocable, Bindings }
import _root_.io.really.jshooks.API
import jdk.nashorn.api.scripting.NashornScriptEngineFactory
import play.api.libs.json._
import scala.collection.JavaConverters
object JsTools {
def translateJsonToJava(a: JsValue): java.lang.Object = {
a match {
case JsNull => null
case JsString(a) => a.asInstanceOf[java.lang.String]
case JsNumber(a) => a.toDouble.asInstanceOf[java.lang.Double]
case JsArray(a) => a.map(translateJsonToJava)
case JsBoolean(a) => a.asInstanceOf[java.lang.Boolean]
case JsObject(a) => JavaConverters.mapAsJavaMapConverter(
a.map { case (k: String, v: JsValue) => (k, translateJsonToJava(v)) }
)
}
}
val factory = new NashornScriptEngineFactory
def injectSDK(b: Bindings): Unit = {
b.put("cancel", API.cancel)
b.put("print", API.print)
}
def getBindings(engine: ScriptEngine): Bindings = engine.getContext.getBindings(ScriptContext.ENGINE_SCOPE)
def newEngine(): ScriptEngine = factory.getScriptEngine("-strict", "--no-java", "--no-syntax-extensions")
def newEngineWithSDK(): ScriptEngine = {
val engine = newEngine()
injectSDK(getBindings(engine))
engine
}
}