免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版
打開APP
未登錄
開通VIP,暢享免費電子書等14項超值服
開通VIP
首頁
好書
留言交流
下載APP
聯(lián)系客服
java執(zhí)行JavaScript代碼
muyable
>《java》
2015.04.08
關(guān)注
學(xué)習(xí)資料
http://jnotnull.iteye.com/blog/262384
Java代碼
import
java.util.ArrayList;
import
java.util.HashMap;
import
java.util.List;
import
java.util.Map;
import
java.util.Set;
import
java.util.Map.Entry;
import
javax.script.Invocable;
import
javax.script.ScriptEngine;
import
javax.script.ScriptEngineManager;
import
javax.script.ScriptException;
import
sun.org.mozilla.javascript.internal.NativeArray;
import
sun.org.mozilla.javascript.internal.NativeObject;
public
class
TestScript {
public
static
void
main(String[] args)
throws
Exception {
ScriptEngineManager factory =
new
ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName(
"JavaScript"
);
// engine.eval("print('hello')");
// testScriptVariables(engine);// 演示如何暴露Java對象為腳本語言的全局變量
// testInvokeScriptMethod(engine);// 演示如何在Java中調(diào)用腳本語言的方法
// testScriptInterface(engine);// 演示腳本語言如何實現(xiàn)Java的接口
// testUsingJDKClasses(engine);// 演示腳本語言如何使用JDK平臺下的類
test(engine);
}
public
static
void
testScriptVariables(ScriptEngine engine)
throws
ScriptException {
Map<String, Object> map =
new
HashMap<String, Object>();
engine.put(
"map"
, map);
engine.eval(
"map.put('s',new Date);"
);
System.out.println(map.get(
"s"
).getClass());
}
public
static
void
testInvokeScriptMethod(ScriptEngine engine)
throws
Exception {
String script =
"function hello(name) { return 'Hello,' + name;}"
;
engine.eval(script);
Invocable inv = (Invocable) engine;
String res = (String) inv.invokeFunction(
"hello"
,
"Scripting"
);
System.out.println(
"res:"
+ res);
}
public
static
void
testScriptInterface(ScriptEngine engine)
throws
ScriptException {
String script =
"var obj = new Object();obj.run = function() { println('run method called');}"
;
engine.eval(script);
Object obj = engine.get(
"obj"
);
Invocable inv = (Invocable) engine;
Runnable r = inv.getInterface(obj, Runnable.
class
);
Thread th =
new
Thread(r);
th.start();
}
public
static
void
testUsingJDKClasses(ScriptEngine engine)
throws
Exception {
// Packages是腳本語言里的一個全局變量,專用于訪問JDK的package
// String js = "function doSwing(t){var f=new
// Packages.javax.swing.JFrame(t);f.setSize(400,300);f.setVisible(true);}";
String js =
"function doSwing(t){var f=Packages.java.util.UUID.randomUUID();print(f)}"
;
engine.eval(js);
Invocable inv = (Invocable) engine;
inv.invokeFunction(
"doSwing"
,
"Scripting Swing"
);
}
/**
* 我能想到的應(yīng)用場景,將前臺傳過來的json數(shù)組構(gòu)造成java的List<Map<String,
* Object>>,然后就可以隨心所欲的使用該list了 當(dāng)然可以使用第三方j(luò)ar采用json to
* bean的方式,而且這種方案更優(yōu)雅,但是需要引入第三方庫
*
* @throws NoSuchMethodException
* @throws ScriptException
*/
public
static
void
test(ScriptEngine engine)
throws
ScriptException,
NoSuchMethodException {
// String json =
// "{'key1':'a','son':{'dd':'dd','a':8},'ran':Math.random()},{'key3':'xor'}";
String json =
"{'key1':'a','son':[{'dd':'dd'},{'dd1':'dd1'}],'ran':Math.random()},{'key3':'xor'}"
;
NativeArray json2array = json2array(engine,
"["
+ json +
"]"
);
List<Map<String, Object>> list = array2list(engine, json2array);
System.out.println(list);
}
public
static
NativeArray json2array(ScriptEngine engine, String json)
throws
ScriptException, NoSuchMethodException {
String script =
"function hello() { return "
+ json +
";}"
;
engine.eval(script);
Invocable inv = (Invocable) engine;
Object obj = inv.invokeFunction(
"hello"
);
// System.out.println(obj);
return
(NativeArray) obj;
}
public
static
List<Map<String, Object>> array2list(ScriptEngine engine,
NativeArray nativeArray)
throws
ScriptException,
NoSuchMethodException {
List<Map<String, Object>> list =
new
ArrayList<Map<String, Object>>();
engine.put(
"list"
, list);
engine.put(
"arr"
, nativeArray);
String script =
" function dosomething(){ "
+
" "
+
" for (n=0; n< arr.length; n++){ "
+
" var map=new Packages.java.util.HashMap(); "
+
" for (i in arr[n]){ "
+
" map.put(i,arr[n][i]); "
+
" } "
+
" list.add(map); "
+
" } "
+
" } "
;
engine.eval(script);
Invocable inv = (Invocable) engine;
inv.invokeFunction(
"dosomething"
);
for
(Map<String, Object> map : list) {
Set<Entry<String, Object>> entrySet = map.entrySet();
for
(Entry<String, Object> entry : entrySet) {
Object object = entry.getValue();
if
(object
instanceof
NativeArray) {
map.put(entry.getKey(), array2list(engine,
(NativeArray) object));
}
else
if
(object
instanceof
NativeObject) {
map.put(entry.getKey(), obj2map(engine,
(NativeObject) object));
}
}
}
return
list;
}
public
static
Map<String, Object> obj2map(ScriptEngine engine,
Object nativeObject)
throws
ScriptException, NoSuchMethodException {
Map<String, Object> map =
new
HashMap<String, Object>();
engine.put(
"map"
, map);
engine.put(
"obj"
, nativeObject);
String script =
" function dosomething(){ "
+
" for (i in obj){ "
+
" map.put(i,obj[i]); "
+
" } "
+
" } "
;
engine.eval(script);
Invocable inv = (Invocable) engine;
inv.invokeFunction(
"dosomething"
);
return
map;
}
}
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點擊舉報
。
打開APP,閱讀全文并永久保存
查看更多類似文章
猜你喜歡
類似文章
JAVA運行腳本語言(Scripting)
Java中運行動態(tài)腳本 如Groovy
Java SE 6 新特性: 對腳本語言的支持
給 Java SE 注入腳本語言的活力
Java SE 6之腳本引擎 讓程序如虎添翼
在Java中使用正則表達式進行后向引用($1,$2...)
更多類似文章 >>
生活服務(wù)
首頁
萬象
文化
人生
生活
健康
教育
職場
理財
娛樂
藝術(shù)
上網(wǎng)
留言交流
回頂部
聯(lián)系我們
分享
收藏
點擊這里,查看已保存的文章
導(dǎo)長圖
關(guān)注
一鍵復(fù)制
下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!
聯(lián)系客服
微信登錄中...
請勿關(guān)閉此頁面
先別劃走!
送你5元優(yōu)惠券,購買VIP限時立減!
5
元
優(yōu)惠券
優(yōu)惠券還有
10:00
過期
馬上使用
×